@adaas/a-utils 0.0.8 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/.nvmrc +1 -1
  2. package/dist/index.d.ts +21 -14
  3. package/dist/index.js +53 -20
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/constants/errors.constants.d.ts +0 -65
  6. package/dist/src/constants/errors.constants.js +0 -72
  7. package/dist/src/constants/errors.constants.js.map +1 -1
  8. package/dist/src/lib/A-Channel/A-Channel.component.d.ts +3 -0
  9. package/dist/src/lib/A-Channel/A-Channel.component.js +8 -0
  10. package/dist/src/lib/A-Channel/A-Channel.component.js.map +1 -0
  11. package/dist/src/lib/A-Channel/A-Channel.error.d.ts +3 -0
  12. package/dist/src/lib/A-Channel/A-Channel.error.js +8 -0
  13. package/dist/src/lib/A-Channel/A-Channel.error.js.map +1 -0
  14. package/dist/src/lib/A-Channel/A-Channel.types.d.ts +0 -0
  15. package/dist/src/lib/A-Channel/A-Channel.types.js +2 -0
  16. package/dist/src/lib/A-Channel/A-Channel.types.js.map +1 -0
  17. package/dist/src/lib/A-Command/A-Command.constants.d.ts +22 -0
  18. package/dist/src/lib/A-Command/A-Command.constants.js +28 -0
  19. package/dist/src/lib/A-Command/A-Command.constants.js.map +1 -0
  20. package/dist/src/lib/A-Command/A-Command.entity.d.ts +133 -0
  21. package/dist/src/lib/A-Command/A-Command.entity.js +273 -0
  22. package/dist/src/lib/A-Command/A-Command.entity.js.map +1 -0
  23. package/dist/src/lib/A-Command/A-Command.error.d.ts +3 -0
  24. package/dist/src/lib/A-Command/A-Command.error.js +8 -0
  25. package/dist/src/lib/A-Command/A-Command.error.js.map +1 -0
  26. package/dist/src/lib/A-Command/A-Command.types.d.ts +73 -0
  27. package/dist/src/lib/A-Command/A-Command.types.js +4 -0
  28. package/dist/src/lib/A-Command/A-Command.types.js.map +1 -0
  29. package/dist/src/lib/A-Config/A-Config.constants.d.ts +3 -0
  30. package/dist/src/lib/A-Config/A-Config.constants.js +6 -0
  31. package/dist/src/lib/A-Config/A-Config.constants.js.map +1 -0
  32. package/dist/src/lib/A-Config/A-Config.container.d.ts +8 -0
  33. package/dist/src/lib/A-Config/A-Config.container.js +75 -0
  34. package/dist/src/lib/A-Config/A-Config.container.js.map +1 -0
  35. package/dist/src/lib/A-Config/A-Config.context.d.ts +29 -0
  36. package/dist/src/lib/A-Config/A-Config.context.js +63 -0
  37. package/dist/src/lib/A-Config/A-Config.context.js.map +1 -0
  38. package/dist/src/lib/A-Config/A-Config.error.d.ts +4 -0
  39. package/dist/src/lib/A-Config/A-Config.error.js +9 -0
  40. package/dist/src/lib/A-Config/A-Config.error.js.map +1 -0
  41. package/dist/src/lib/A-Config/A-Config.types.d.ts +19 -0
  42. package/dist/src/lib/A-Config/A-Config.types.js +7 -0
  43. package/dist/src/lib/A-Config/A-Config.types.js.map +1 -0
  44. package/dist/src/lib/A-Config/components/ConfigReader.component.d.ts +30 -0
  45. package/dist/src/lib/A-Config/components/ConfigReader.component.js +85 -0
  46. package/dist/src/lib/A-Config/components/ConfigReader.component.js.map +1 -0
  47. package/dist/src/lib/A-Config/components/ENVConfigReader.component.d.ts +10 -0
  48. package/dist/src/lib/A-Config/components/ENVConfigReader.component.js +37 -0
  49. package/dist/src/lib/A-Config/components/ENVConfigReader.component.js.map +1 -0
  50. package/dist/src/lib/A-Config/components/FileConfigReader.component.d.ts +11 -0
  51. package/dist/src/lib/A-Config/components/FileConfigReader.component.js +47 -0
  52. package/dist/src/lib/A-Config/components/FileConfigReader.component.js.map +1 -0
  53. package/dist/src/lib/A-Logger/A-Logger.component.d.ts +29 -0
  54. package/dist/src/lib/A-Logger/A-Logger.component.js +152 -0
  55. package/dist/src/lib/A-Logger/A-Logger.component.js.map +1 -0
  56. package/dist/src/lib/A-Logger/A-Logger.types.d.ts +0 -0
  57. package/dist/src/lib/A-Logger/A-Logger.types.js +2 -0
  58. package/dist/src/lib/A-Logger/A-Logger.types.js.map +1 -0
  59. package/dist/src/lib/A-Memory/A-Memory.context.d.ts +64 -0
  60. package/dist/src/lib/A-Memory/A-Memory.context.js +105 -0
  61. package/dist/src/lib/A-Memory/A-Memory.context.js.map +1 -0
  62. package/dist/src/lib/A-Polyfill/A-Polyfill.component.d.ts +20 -0
  63. package/dist/src/lib/A-Polyfill/A-Polyfill.component.js +53 -0
  64. package/dist/src/lib/A-Polyfill/A-Polyfill.component.js.map +1 -0
  65. package/dist/src/{global/A_Polyfills.d.ts → lib/A-Polyfill/A-Polyfill.types.d.ts} +1 -13
  66. package/dist/src/lib/A-Polyfill/A-Polyfill.types.js +2 -0
  67. package/dist/src/lib/A-Polyfill/A-Polyfill.types.js.map +1 -0
  68. package/dist/src/lib/A-Polyfill/A-Polyfills.class.d.ts +10 -0
  69. package/dist/src/{global/A_Polyfills.js → lib/A-Polyfill/A-Polyfills.class.js} +4 -4
  70. package/dist/src/lib/A-Polyfill/A-Polyfills.class.js.map +1 -0
  71. package/dist/src/{global/A_Deferred.class.d.ts → lib/A-Schedule/A-Deferred.class.d.ts} +4 -0
  72. package/dist/src/{global/A_Deferred.class.js → lib/A-Schedule/A-Deferred.class.js} +5 -1
  73. package/dist/src/lib/A-Schedule/A-Deferred.class.js.map +1 -0
  74. package/dist/src/lib/A-Schedule/A-Schedule.component.d.ts +57 -0
  75. package/dist/src/lib/A-Schedule/A-Schedule.component.js +49 -0
  76. package/dist/src/lib/A-Schedule/A-Schedule.component.js.map +1 -0
  77. package/dist/src/{types/A_ScheduleObject.types.d.ts → lib/A-Schedule/A-Schedule.types.d.ts} +2 -1
  78. package/dist/src/{types/ASEID.types.js → lib/A-Schedule/A-Schedule.types.js} +1 -3
  79. package/dist/src/lib/A-Schedule/A-Schedule.types.js.map +1 -0
  80. package/dist/src/lib/A-Schedule/A-ScheduleObject.class.d.ts +29 -0
  81. package/dist/src/{global/A_ScheduleObject.class.js → lib/A-Schedule/A-ScheduleObject.class.js} +25 -5
  82. package/dist/src/lib/A-Schedule/A-ScheduleObject.class.js.map +1 -0
  83. package/index.ts +23 -47
  84. package/jest.config.ts +3 -3
  85. package/package.json +6 -6
  86. package/src/constants/errors.constants.ts +0 -78
  87. package/src/lib/A-Channel/A-Channel.component.ts +8 -0
  88. package/src/lib/A-Channel/A-Channel.error.ts +6 -0
  89. package/src/lib/A-Channel/A-Channel.types.ts +0 -0
  90. package/src/lib/A-Command/A-Command.constants.ts +31 -0
  91. package/src/lib/A-Command/A-Command.entity.ts +327 -0
  92. package/src/lib/A-Command/A-Command.error.ts +6 -0
  93. package/src/lib/A-Command/A-Command.types.ts +100 -0
  94. package/src/lib/A-Config/A-Config.constants.ts +13 -0
  95. package/src/lib/A-Config/A-Config.container.ts +63 -0
  96. package/src/lib/A-Config/A-Config.context.ts +117 -0
  97. package/src/lib/A-Config/A-Config.error.ts +7 -0
  98. package/src/lib/A-Config/A-Config.types.ts +26 -0
  99. package/src/lib/A-Config/components/ConfigReader.component.ts +67 -0
  100. package/src/lib/A-Config/components/ENVConfigReader.component.ts +31 -0
  101. package/src/lib/A-Config/components/FileConfigReader.component.ts +42 -0
  102. package/src/lib/A-Logger/A-Logger.component.ts +190 -0
  103. package/src/lib/A-Logger/A-Logger.types.ts +0 -0
  104. package/src/lib/A-Memory/A-Memory.context.ts +115 -0
  105. package/src/lib/A-Polyfill/A-Polyfill.component.ts +45 -0
  106. package/src/lib/A-Polyfill/A-Polyfill.types.ts +10 -0
  107. package/src/{global/A_Polyfills.ts → lib/A-Polyfill/A-Polyfills.class.ts} +2 -16
  108. package/src/{global/A_Deferred.class.ts → lib/A-Schedule/A-Deferred.class.ts} +4 -0
  109. package/src/lib/A-Schedule/A-Schedule.component.ts +89 -0
  110. package/src/{types/A_ScheduleObject.types.ts → lib/A-Schedule/A-Schedule.types.ts} +7 -2
  111. package/src/{global/A_ScheduleObject.class.ts → lib/A-Schedule/A-ScheduleObject.class.ts} +25 -7
  112. package/tests/A-Channel.test.ts +16 -0
  113. package/tests/A-Command.test.ts +133 -0
  114. package/tests/A-Config.test.ts +185 -0
  115. package/tests/A-Polyfill.test.ts +67 -0
  116. package/tests/A-Schedule.test.ts +84 -0
  117. package/tests/jest.setup.ts +35 -0
  118. package/tsconfig.build.json +56 -0
  119. package/tsconfig.json +2 -2
  120. package/dist/src/global/ASEID.class.d.ts +0 -77
  121. package/dist/src/global/ASEID.class.js +0 -129
  122. package/dist/src/global/ASEID.class.js.map +0 -1
  123. package/dist/src/global/A_Deferred.class.js.map +0 -1
  124. package/dist/src/global/A_Entity.class.d.ts +0 -49
  125. package/dist/src/global/A_Entity.class.js +0 -94
  126. package/dist/src/global/A_Entity.class.js.map +0 -1
  127. package/dist/src/global/A_Error.class.d.ts +0 -13
  128. package/dist/src/global/A_Error.class.js +0 -63
  129. package/dist/src/global/A_Error.class.js.map +0 -1
  130. package/dist/src/global/A_Polyfills.js.map +0 -1
  131. package/dist/src/global/A_ScheduleObject.class.d.ts +0 -9
  132. package/dist/src/global/A_ScheduleObject.class.js.map +0 -1
  133. package/dist/src/global/A_ServerError.class.d.ts +0 -13
  134. package/dist/src/global/A_ServerError.class.js +0 -57
  135. package/dist/src/global/A_ServerError.class.js.map +0 -1
  136. package/dist/src/helpers/A_Common.helper.d.ts +0 -42
  137. package/dist/src/helpers/A_Common.helper.js +0 -214
  138. package/dist/src/helpers/A_Common.helper.js.map +0 -1
  139. package/dist/src/helpers/A_Schedule.helper.d.ts +0 -6
  140. package/dist/src/helpers/A_Schedule.helper.js +0 -21
  141. package/dist/src/helpers/A_Schedule.helper.js.map +0 -1
  142. package/dist/src/types/ASEID.types.d.ts +0 -65
  143. package/dist/src/types/ASEID.types.js.map +0 -1
  144. package/dist/src/types/A_Common.types.d.ts +0 -59
  145. package/dist/src/types/A_Common.types.js +0 -3
  146. package/dist/src/types/A_Common.types.js.map +0 -1
  147. package/dist/src/types/A_Entity.types.d.ts +0 -13
  148. package/dist/src/types/A_Entity.types.js +0 -4
  149. package/dist/src/types/A_Entity.types.js.map +0 -1
  150. package/dist/src/types/A_Error.type.d.ts +0 -7
  151. package/dist/src/types/A_Error.type.js +0 -3
  152. package/dist/src/types/A_Error.type.js.map +0 -1
  153. package/dist/src/types/A_Error.types.d.ts +0 -7
  154. package/dist/src/types/A_Error.types.js +0 -3
  155. package/dist/src/types/A_Error.types.js.map +0 -1
  156. package/dist/src/types/A_ScheduleObject.types.js +0 -3
  157. package/dist/src/types/A_ScheduleObject.types.js.map +0 -1
  158. package/dist/src/types/A_ServerError.types.d.ts +0 -4
  159. package/dist/src/types/A_ServerError.types.js +0 -3
  160. package/dist/src/types/A_ServerError.types.js.map +0 -1
  161. package/src/global/ASEID.class.ts +0 -208
  162. package/src/global/A_Error.class.ts +0 -88
  163. package/src/global/A_ServerError.class.ts +0 -70
  164. package/src/helpers/A_Common.helper.ts +0 -265
  165. package/src/helpers/A_Schedule.helper.ts +0 -25
  166. package/src/types/ASEID.types.ts +0 -86
  167. package/src/types/A_Common.types.ts +0 -111
  168. package/src/types/A_Entity.types.ts +0 -17
  169. package/src/types/A_Error.types.ts +0 -10
  170. package/src/types/A_ServerError.types.ts +0 -7
  171. package/tests/default.test.ts +0 -160
  172. package/tests/polyfill.test.ts +0 -37
@@ -1,13 +1,13 @@
1
- import { A_TYPES__ScheduleObjectConfig } from "../types/A_ScheduleObject.types";
2
- import { A_Deferred } from "./A_Deferred.class";
3
- import { A_Error } from "./A_Error.class";
1
+ import { A_Deferred } from "./A-Deferred.class";
2
+ import { A_Error } from "@adaas/a-concept";
3
+ import { A_UTILS_TYPES__ScheduleObjectCallback, A_UTILS_TYPES__ScheduleObjectConfig } from "./A-Schedule.types";
4
4
 
5
- export class A_ScheduleObject<T> {
5
+ export class A_ScheduleObject<T extends any = any> {
6
6
 
7
7
  private timeout!: NodeJS.Timeout;
8
8
  private deferred!: A_Deferred<T>;
9
9
 
10
- private config: A_TYPES__ScheduleObjectConfig = {
10
+ private config: A_UTILS_TYPES__ScheduleObjectConfig = {
11
11
  /**
12
12
  * If the timeout is cleared, should the promise resolve or reject?
13
13
  * BY Default it rejects
@@ -17,10 +17,28 @@ export class A_ScheduleObject<T> {
17
17
  resolveOnClear: false
18
18
  };
19
19
 
20
+
21
+ /**
22
+ * Creates a scheduled object that will execute the action after specified milliseconds
23
+ *
24
+ *
25
+ * @param ms - milliseconds to wait before executing the action
26
+ * @param action - the action to execute
27
+ * @param config - configuration options for the schedule object
28
+ */
20
29
  constructor(
30
+ /**
31
+ * Milliseconds to wait before executing the action
32
+ */
21
33
  ms: number,
22
- action: () => Promise<T>,
23
- config?: A_TYPES__ScheduleObjectConfig
34
+ /**
35
+ * The action to execute after the specified milliseconds
36
+ */
37
+ action: A_UTILS_TYPES__ScheduleObjectCallback<T>,
38
+ /**
39
+ * Configuration options for the schedule object
40
+ */
41
+ config?: A_UTILS_TYPES__ScheduleObjectConfig
24
42
  ) {
25
43
  if (config)
26
44
  this.config = { ...this.config, ...config };
@@ -0,0 +1,16 @@
1
+ import './jest.setup';
2
+ import { A_Context } from '@adaas/a-concept';
3
+
4
+ jest.retryTimes(0);
5
+
6
+ describe('A-Channel tests', () => {
7
+
8
+ it('Should Allow to create a channel', async () => {
9
+ const { A_Channel } = await import('@adaas/a-utils/lib/A-Channel/A-Channel.component');
10
+
11
+ const channel = new A_Channel();
12
+
13
+ const meta = A_Context.meta(channel);
14
+ });
15
+
16
+ });
@@ -0,0 +1,133 @@
1
+ import { A_Command } from '@adaas/a-utils/lib/A-Command/A-Command.entity';
2
+ import { A_CONSTANTS__A_Command_Status, A_CONSTANTS_A_Command_Features } from '@adaas/a-utils/lib/A-Command/A-Command.constants';
3
+ import { A_Component, A_Context, A_Error, A_Feature, A_Inject, A_Scope } from '@adaas/a-concept';
4
+ import { A_Memory } from '@adaas/a-utils/lib/A-Memory/A-Memory.context';
5
+
6
+ jest.retryTimes(0);
7
+
8
+ describe('A-Command tests', () => {
9
+
10
+ it('Should Allow to create a command', async () => {
11
+ const command = new A_Command({});
12
+ A_Context.root.register(command);
13
+
14
+
15
+ expect(command).toBeInstanceOf(A_Command);
16
+ expect(command.code).toBe('a-command');
17
+ expect(command.id).toBeDefined();
18
+ expect(command.aseid).toBeDefined();
19
+ expect(command.status).toBe(A_CONSTANTS__A_Command_Status.INITIALIZED);
20
+ expect(command.scope).toBeInstanceOf(A_Scope);
21
+ expect(command.scope.resolve(A_Memory)).toBeInstanceOf(A_Memory);
22
+ });
23
+ it('Should allow to execute a command', async () => {
24
+ const command = new A_Command({});
25
+ A_Context.root.register(command);
26
+
27
+ await command.execute();
28
+
29
+ expect(command.status).toBe(A_CONSTANTS__A_Command_Status.COMPLETED);
30
+ expect(command.startedAt).toBeInstanceOf(Date);
31
+ expect(command.endedAt).toBeInstanceOf(Date);
32
+ });
33
+ it('Should allow to serialize and deserialize a command', async () => {
34
+ const command = new A_Command({});
35
+ A_Context.root.register(command);
36
+
37
+ await command.execute();
38
+
39
+ const serialized = command.toJSON();
40
+ expect(serialized).toBeDefined();
41
+ expect(serialized.aseid).toBe(command.aseid.toString());
42
+ expect(serialized.code).toBe(command.code);
43
+ expect(serialized.status).toBe(command.status);
44
+ expect(serialized.startedAt).toBe(command.startedAt?.toISOString());
45
+ expect(serialized.duration).toBe(command.duration);
46
+
47
+
48
+ const deserializedCommand = new A_Command(serialized);
49
+ expect(deserializedCommand).toBeInstanceOf(A_Command);
50
+ expect(deserializedCommand.aseid.toString()).toBe(command.aseid.toString());
51
+ expect(deserializedCommand.code).toBe(command.code);
52
+ expect(deserializedCommand.status).toBe(command.status);
53
+ expect(deserializedCommand.startedAt?.toISOString()).toBe(command.startedAt?.toISOString());
54
+ expect(deserializedCommand.duration).toBe(command.duration);
55
+ });
56
+ it('Should allow to execute a command with custom logic', async () => {
57
+
58
+ // 1) create a scope
59
+ A_Context.reset();
60
+
61
+ // 2) create a new command
62
+ type resultType = { bar: string };
63
+ type invokeType = { foo: string };
64
+ class MyCommand extends A_Command<invokeType, resultType> { }
65
+
66
+ A_Context.root.register(MyCommand);
67
+
68
+ // 3) create a custom component with custom logic
69
+ class MyComponent extends A_Component {
70
+
71
+ @A_Feature.Extend({ scope: [MyCommand] })
72
+ async [A_CONSTANTS_A_Command_Features.EXECUTE](
73
+ @A_Inject(A_Memory) context: A_Memory<resultType>
74
+ ) {
75
+ context.set('bar', 'baz');
76
+ }
77
+ }
78
+
79
+ // 4) register component in the scope
80
+ A_Context.root.register(MyComponent);
81
+
82
+ // 5) create a new command instance within the scope
83
+ const command = new MyCommand({ foo: 'bar' });
84
+ A_Context.root.register(command);
85
+
86
+ // 6) execute the command
87
+ await command.execute();
88
+
89
+ // 7) verify that command was executed with custom logic from MyComponent
90
+ expect(command.status).toBe(A_CONSTANTS__A_Command_Status.COMPLETED);
91
+ expect(command.result).toBeDefined();
92
+ expect(command.result).toEqual({ bar: 'baz' });
93
+ })
94
+ it('Should allow to fail a command with custom logic', async () => {
95
+ // 1) reset context to have a clean scope
96
+ A_Context.reset();
97
+
98
+ // 2) create a new command
99
+ type resultType = { bar: string };
100
+ type invokeType = { foo: string };
101
+ class MyCommand extends A_Command<invokeType, resultType> { }
102
+
103
+ A_Context.root.register(MyCommand);
104
+
105
+ // 3) create a custom component with custom logic
106
+ class MyComponent extends A_Component {
107
+
108
+ @A_Feature.Extend({ scope: [MyCommand] })
109
+ async [A_CONSTANTS_A_Command_Features.EXECUTE](
110
+ @A_Inject(A_Memory) context: A_Memory<resultType>
111
+ ) {
112
+ context.error(new A_Error({ title: 'Test error' }));
113
+ // it's optional to throw an error here, as the command may contain multiple errors that also can be a result of async operations
114
+ throw new A_Error({ title: 'Test error thrown' });
115
+ }
116
+ }
117
+
118
+ // 4) register component in the scope
119
+ A_Context.root.register(MyComponent);
120
+ // 5) create a new command instance within the scope
121
+ const command = new MyCommand({ foo: 'bar' });
122
+ A_Context.root.register(command);
123
+
124
+ // 6) execute the command
125
+ await command.execute();
126
+
127
+ // 7) verify that command was executed with custom logic from MyComponent
128
+ expect(command.status).toBe(A_CONSTANTS__A_Command_Status.FAILED);
129
+ expect(command.errors).toBeDefined();
130
+ expect(command.errors?.size).toBe(1);
131
+ expect(Array.from(command.errors?.values() || [])[0].message).toBe('Test error');
132
+ });
133
+ });
@@ -0,0 +1,185 @@
1
+ import {
2
+ A_Concept,
3
+ A_CONSTANTS__DEFAULT_ENV_VARIABLES,
4
+ A_Context,
5
+ } from '@adaas/a-concept';
6
+ import fs from 'fs';
7
+ import { A_Config } from '@adaas/a-utils/lib/A-Config/A-Config.context';
8
+ import { A_Polyfill } from '@adaas/a-utils/lib/A-Polyfill/A-Polyfill.component';
9
+ import { A_ConfigLoader } from '@adaas/a-utils/lib/A-Config/A-Config.container';
10
+ import { ENVConfigReader } from '@adaas/a-utils/lib/A-Config/components/ENVConfigReader.component';
11
+ import { FileConfigReader } from '@adaas/a-utils/lib/A-Config/components/FileConfigReader.component';
12
+
13
+ jest.retryTimes(0);
14
+
15
+ describe('A-Config tests', () => {
16
+ it('Should Allow to create a config object', async () => {
17
+ const config = new A_Config({
18
+ variables: [],
19
+ defaults: {}
20
+ });
21
+ });
22
+ it('Should Allow to create a config object with default values', async () => {
23
+ const config = new A_Config({
24
+ variables: ['TEST_VAR1', 'TEST_VAR2'] as const,
25
+ defaults: {
26
+ TEST_VAR1: 'default1',
27
+ }
28
+ });
29
+
30
+ expect(config.get('TEST_VAR1')).toBe('default1');
31
+ expect(config.get('TEST_VAR2')).toBeUndefined();
32
+ });
33
+ it('Should Allow to create a config object with ENV values', async () => {
34
+ process.env['TEST_VAR1'] = 'env1';
35
+ process.env['TEST_VAR2'] = 'env2';
36
+
37
+ // Because we're resetting the context, we need to re-import the classes
38
+ // It does automatically by jest.setup.ts script
39
+ const { A_ConfigLoader } = await import('@adaas/a-utils/lib/A-Config/A-Config.container');
40
+ const { FileConfigReader } = await import('@adaas/a-utils/lib/A-Config/components/FileConfigReader.component');
41
+ const { ENVConfigReader } = await import('@adaas/a-utils/lib/A-Config/components/ENVConfigReader.component');
42
+
43
+ const config = new A_Config({
44
+ variables: ['TEST_VAR1', 'TEST_VAR2'] as const,
45
+ defaults: {
46
+ TEST_VAR1: 'default1',
47
+ }
48
+ });
49
+
50
+ const configLoader = new A_ConfigLoader({
51
+ name: 'test-config-loader',
52
+ fragments: [config],
53
+ components: [A_Polyfill, FileConfigReader, ENVConfigReader]
54
+ })
55
+
56
+ const concept = new A_Concept({
57
+ name: 'test-concept',
58
+ containers: [configLoader]
59
+ })
60
+
61
+ await concept.load();
62
+
63
+ expect(config.get('TEST_VAR1')).toBe('env1');
64
+ expect(config.get('TEST_VAR2')).toBe('env2');
65
+
66
+ delete process.env['TEST_VAR1'];
67
+ delete process.env['TEST_VAR2'];
68
+ });
69
+ it('Should not Change Names of default env variables', async () => {
70
+ process.env[A_CONSTANTS__DEFAULT_ENV_VARIABLES.A_CONCEPT_NAME] = 'my-project';
71
+ process.env['TEST_VAR2'] = 'env2';
72
+
73
+ const config = new A_Config({
74
+ variables: ['TEST_VAR1', 'TEST_VAR2'] as const,
75
+ defaults: {
76
+ TEST_VAR1: 'default1',
77
+ }
78
+ });
79
+
80
+ const configLoader = new A_ConfigLoader({
81
+ name: 'test-config-loader',
82
+ fragments: [config],
83
+ components: [A_Polyfill, ENVConfigReader]
84
+ })
85
+
86
+ const concept = new A_Concept({
87
+ name: 'test-concept',
88
+ containers: [configLoader]
89
+ })
90
+
91
+
92
+ await concept.load();
93
+
94
+ expect(config.get('TEST_VAR1')).toBe('default1');
95
+ expect(config.get('TEST_VAR2')).toBe('env2');
96
+ expect(config.get('A_CONCEPT_NAME')).toBe('my-project');
97
+
98
+ delete process.env[A_CONSTANTS__DEFAULT_ENV_VARIABLES.A_CONCEPT_NAME];
99
+ delete process.env['TEST_VAR2'];
100
+ });
101
+ it('Should Throw an error if strict is true and variable is not defined', async () => {
102
+ expect(() => {
103
+ const config = new A_Config({
104
+ strict: true,
105
+ variables: ['TEST_VAR1', 'TEST_VAR2'] as const,
106
+ defaults: {
107
+ TEST_VAR1: 'default1',
108
+ }
109
+ });
110
+
111
+ config.get('TEST_VAR3' as any);
112
+
113
+ }).toThrowError();
114
+ });
115
+ it('Should Allow to create a config object with variables from File', async () => {
116
+ // 1. create a config file
117
+ fs.writeFileSync('a-concept.conf.json', JSON.stringify({
118
+ testVar1: 'env1',
119
+ testVar2: 'env2'
120
+ }, null, 4));
121
+
122
+ const config = new A_Config({
123
+ variables: ['TEST_VAR1', 'TEST_VAR2'] as const,
124
+ defaults: {
125
+ TEST_VAR1: 'default1',
126
+ }
127
+ });
128
+
129
+ const configLoader = new A_ConfigLoader({
130
+ name: 'test-config-loader',
131
+ fragments: [config],
132
+ components: [A_Polyfill, FileConfigReader]
133
+ })
134
+
135
+ const concept = new A_Concept({
136
+ name: 'test-concept',
137
+ containers: [configLoader]
138
+ })
139
+
140
+ await concept.load();
141
+
142
+
143
+
144
+ expect(config.get('TEST_VAR1')).toBe('env1');
145
+ expect(config.get('TEST_VAR1')).toBe('env1');
146
+
147
+ // 3. delete the config file
148
+ fs.unlinkSync('a-concept.conf.json');
149
+ });
150
+ it('Should Allow to create a config object with variables from File with different variable cases', async () => {
151
+
152
+ // 1. create a config file
153
+ fs.writeFileSync('a-concept.conf.json', JSON.stringify({
154
+ testVar2: 'env2'
155
+ }, null, 4));
156
+
157
+ const config = new A_Config({
158
+ variables: ['testVar1', 'testVar2'] as const,
159
+ defaults: {
160
+ testVar1: 'default1',
161
+ }
162
+ });
163
+
164
+ const configLoader = new A_ConfigLoader({
165
+ name: 'test-config-loader',
166
+ fragments: [config],
167
+ components: [A_Polyfill, FileConfigReader]
168
+ })
169
+
170
+ const concept = new A_Concept({
171
+ name: 'test-concept',
172
+ containers: [configLoader]
173
+ })
174
+
175
+ await concept.load();
176
+
177
+
178
+
179
+ expect(config.get('testVar1')).toBe('default1');
180
+ expect(config.get('testVar2')).toBe('env2');
181
+
182
+ // 3. delete the config file
183
+ fs.unlinkSync('a-concept.conf.json');
184
+ });
185
+ });
@@ -0,0 +1,67 @@
1
+ import './jest.setup';
2
+ import { A_Scope } from '@adaas/a-concept';
3
+ import { A_Polyfill } from '@adaas/a-utils/lib/A-Polyfill/A-Polyfill.component';
4
+ import { config } from 'dotenv';
5
+ config();
6
+ jest.retryTimes(0);
7
+
8
+ describe('A-Polyfill Tests', () => {
9
+ it('It Should allow to create A-polyfill component', async () => {
10
+ const testScope = new A_Scope({
11
+ components: [A_Polyfill]
12
+ });
13
+
14
+ const polyfill = testScope.resolve(A_Polyfill);
15
+
16
+ await polyfill.load();
17
+
18
+ expect(polyfill).toBeInstanceOf(A_Polyfill);
19
+
20
+ });
21
+
22
+ it('It Should return fs', async () => {
23
+
24
+ const testScope = new A_Scope({
25
+ components: [A_Polyfill]
26
+ });
27
+
28
+ const polyfill = testScope.resolve(A_Polyfill);
29
+ await polyfill.load();
30
+
31
+ const fs = await polyfill.fs();
32
+
33
+ expect(fs).toBeDefined();
34
+ });
35
+ it('It Should return crypto', async () => {
36
+
37
+ const testScope = new A_Scope({
38
+ components: [A_Polyfill]
39
+ });
40
+
41
+ const polyfill = testScope.resolve(A_Polyfill);
42
+ await polyfill.load();
43
+
44
+ const crypto = await polyfill.crypto();
45
+
46
+ expect(crypto).toBeDefined();
47
+ });
48
+
49
+ it('Crypto should calculate Hash', async () => {
50
+
51
+ const testScope = new A_Scope({
52
+ components: [A_Polyfill]
53
+ });
54
+
55
+ const polyfill = testScope.resolve(A_Polyfill);
56
+ await polyfill.load();
57
+
58
+ const crypto = await polyfill.crypto();
59
+
60
+ const hash = await crypto.createFileHash('./index.ts', 'sha-256');
61
+
62
+ expect(hash).toBeDefined();
63
+
64
+ });
65
+
66
+
67
+ });
@@ -0,0 +1,84 @@
1
+ import './jest.setup';
2
+ import { A_Scope } from "@adaas/a-concept";
3
+ import { A_Schedule } from "@adaas/a-utils/lib/A-Schedule/A-Schedule.component";
4
+
5
+ jest.retryTimes(0);
6
+
7
+ describe('A-Schedule Tests', () => {
8
+ it('Schedule allow to create schedule component', async () => {
9
+ const testScope = new A_Scope({
10
+ components: [A_Schedule]
11
+ });
12
+
13
+ const schedule = testScope.resolve(A_Schedule);
14
+
15
+ expect(schedule).toBeInstanceOf(A_Schedule);
16
+ });
17
+
18
+ it('Schedule Should execute promise and await it ', async () => {
19
+
20
+ const testScope = new A_Scope({
21
+ components: [A_Schedule]
22
+ });
23
+
24
+ const schedule = testScope.resolve(A_Schedule);
25
+
26
+ const start = Date.now();
27
+ let res = '';
28
+
29
+ try {
30
+ const scheduler = await schedule.delay(3000, async () => {
31
+ return 'RESOLVED';
32
+ });
33
+
34
+
35
+ res = await scheduler.promise;
36
+
37
+ } catch (error) {
38
+ // Handle error if any
39
+ } finally {
40
+ const end = Date.now();
41
+ const duration = end - start;
42
+
43
+ expect(res).toBe('RESOLVED');
44
+ // Check if the duration exceeds 3 seconds
45
+ expect(duration).toBeGreaterThan(3000);
46
+ }
47
+
48
+ });
49
+
50
+ it('Schedule Should be canceled and rejected', async () => {
51
+
52
+ const testScope = new A_Scope({
53
+ components: [A_Schedule]
54
+ });
55
+
56
+ const schedule = testScope.resolve(A_Schedule);
57
+
58
+ expect(schedule).toBeInstanceOf(A_Schedule);
59
+
60
+
61
+ const start = Date.now();
62
+ let res = '';
63
+
64
+ try {
65
+ const scheduler = await schedule.delay(3000, async () => {
66
+ return 'RESOLVED';
67
+ });
68
+
69
+
70
+ scheduler.clear();
71
+ res = await scheduler.promise;
72
+ } catch (error) {
73
+ // Handle error if any
74
+ } finally {
75
+ const end = Date.now();
76
+ const duration = end - start;
77
+
78
+ expect(res).toBe('');
79
+ // Check if the duration exceeds 3 seconds
80
+ expect(duration).toBeLessThan(3000);
81
+ }
82
+
83
+ });
84
+ });
@@ -0,0 +1,35 @@
1
+ import { A_Context } from '@adaas/a-concept';
2
+ import fs from 'fs';
3
+
4
+ // ==========================================================
5
+ // ====================Error Handlers========================
6
+
7
+ /**
8
+ * Base hooks for tests
9
+ */
10
+ beforeAll(async () => {
11
+
12
+ return Promise.resolve();
13
+ });
14
+
15
+ afterAll(async () => {
16
+ try {
17
+ fs.unlinkSync(`a-concept.conf.json`);
18
+
19
+ } catch (error) {
20
+
21
+ }
22
+ return Promise.resolve();
23
+ });
24
+
25
+ beforeEach(async () => {
26
+ A_Context.reset();
27
+ return Promise.resolve();
28
+ });
29
+
30
+ afterEach(async () => {
31
+ A_Context.reset();
32
+ return Promise.resolve();
33
+ });
34
+
35
+
@@ -0,0 +1,56 @@
1
+ {
2
+ "compileOnSave": false,
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "strict": true,
6
+ "noImplicitAny": false,
7
+ "noFallthroughCasesInSwitch": true,
8
+ "downlevelIteration": true,
9
+ "strictFunctionTypes": true,
10
+ "strictNullChecks": true,
11
+ "strictPropertyInitialization": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "resolveJsonModule": true,
14
+ "noErrorTruncation": true,
15
+ "jsx": "react",
16
+ "target": "es2015",
17
+ "module": "commonjs",
18
+ "moduleResolution": "node",
19
+ "allowSyntheticDefaultImports": true,
20
+ "esModuleInterop": true,
21
+ "noUnusedLocals": false,
22
+ "noUnusedParameters": false,
23
+ "removeComments": false,
24
+ "preserveConstEnums": true,
25
+ "sourceMap": true,
26
+ "baseUrl": ".",
27
+ "experimentalDecorators": true,
28
+ "skipLibCheck": true,
29
+ "typeRoots": [
30
+ "node_modules/@types"
31
+ ],
32
+ "paths": {
33
+ "@adaas/a-utils/constants/*": ["src/constants/*"],
34
+ "@adaas/a-utils/lib/*": ["src/lib/*"],
35
+ "@adaas/a-utils/types/*": ["src/types/*"],
36
+ "@adaas/a-utils/helpers/*": ["src/helpers/*"],
37
+ "@adaas/a-utils/utils/*": ["src/utils/*"]
38
+ },
39
+ "lib": [
40
+ "dom",
41
+ "esnext.asynciterable",
42
+ "es2015",
43
+ "es2016",
44
+ "es2017.object"
45
+ ]
46
+ },
47
+ "include": [
48
+ "src/**/*",
49
+ "index.ts"
50
+ ],
51
+ "exclude": [
52
+ "node_modules/**/*",
53
+ "dist/**/*",
54
+ "node_modules/@types/mocha/index.d.ts"
55
+ ]
56
+ }
package/tsconfig.json CHANGED
@@ -31,10 +31,10 @@
31
31
  ],
32
32
  "paths": {
33
33
  "@adaas/a-utils/constants/*": ["src/constants/*"],
34
- "@adaas/a-utils/global/*": ["src/global/*"],
34
+ "@adaas/a-utils/lib/*": ["src/lib/*"],
35
35
  "@adaas/a-utils/types/*": ["src/types/*"],
36
36
  "@adaas/a-utils/helpers/*": ["src/helpers/*"],
37
- "@adaas/a-utils/decorators/*": ["src/decorators/*"]
37
+ "@adaas/a-utils/utils/*": ["src/utils/*"]
38
38
  },
39
39
  "lib": [
40
40
  "dom",