@adaas/a-utils 0.1.21 → 0.1.23
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.
- package/dist/index.cjs +5 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +136 -54
- package/dist/index.d.ts +136 -54
- package/dist/index.mjs +5 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +6 -0
- package/src/lib/A-Channel/A-Channel.constants.ts +12 -12
- package/src/lib/A-Command/A-Command.constants.ts +60 -18
- package/src/lib/A-Command/A-Command.entity.ts +33 -16
- package/src/lib/A-Command/A-Command.types.ts +21 -13
- package/src/lib/A-Config/A-Config.context.ts +38 -59
- package/src/lib/A-Config/components/ConfigReader.component.ts +17 -15
- package/src/lib/A-Execution/A-Execution.context.ts +67 -0
- package/src/lib/A-Execution/A-Execution.types.ts +0 -0
- package/src/lib/A-Memory/A-Memory.component.ts +1 -1
- package/src/lib/A-Memory/A-Memory.constants.ts +10 -10
- package/src/lib/A-Memory/A-Memory.context.ts +18 -9
- package/src/lib/A-Memory/A-Memory.types.ts +12 -1
- package/src/lib/A-Operation/A-Operation.context.ts +3 -2
- package/src/lib/A-StateMachine/A-StateMachine.constants.ts +4 -4
- package/tests/A-Command.test.ts +6 -6
- package/tests/A-Config.test.ts +5 -11
- package/tests/A-Manifest.test.ts +3 -3
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import { A_Component, A_Concept, A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY, A_Container, A_Feature, A_Inject } from "@adaas/a-concept";
|
|
1
|
+
import { A_Component, A_Concept, A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY, A_Container, A_Context, A_Dependency, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
|
|
2
2
|
import { A_Config } from "../A-Config.context";
|
|
3
3
|
import { A_CONSTANTS__CONFIG_ENV_VARIABLES_ARRAY } from "../A-Config.constants";
|
|
4
4
|
import { A_Polyfill } from "../../A-Polyfill/A-Polyfill.component";
|
|
5
|
+
import { A_Memory } from "../../A-Memory/A-Memory.component";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Config Reader
|
|
8
9
|
*/
|
|
9
10
|
export class ConfigReader extends A_Component {
|
|
10
11
|
|
|
12
|
+
protected DEFAULT_ALLOWED_TO_READ_PROPERTIES = [
|
|
13
|
+
...A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY,
|
|
14
|
+
...A_CONSTANTS__CONFIG_ENV_VARIABLES_ARRAY
|
|
15
|
+
];
|
|
16
|
+
|
|
11
17
|
constructor(
|
|
18
|
+
@A_Dependency.Required()
|
|
12
19
|
@A_Inject(A_Polyfill) protected polyfill: A_Polyfill,
|
|
13
20
|
) {
|
|
14
21
|
super();
|
|
@@ -17,15 +24,11 @@ export class ConfigReader extends A_Component {
|
|
|
17
24
|
@A_Concept.Load()
|
|
18
25
|
async attachContext(
|
|
19
26
|
@A_Inject(A_Container) container: A_Container,
|
|
20
|
-
@A_Inject(
|
|
27
|
+
@A_Inject(A_Scope) context: A_Scope,
|
|
21
28
|
@A_Inject(A_Config) config?: A_Config<any>,
|
|
22
29
|
) {
|
|
23
30
|
if (!config) {
|
|
24
|
-
config= new A_Config({
|
|
25
|
-
variables: [
|
|
26
|
-
...A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY,
|
|
27
|
-
...A_CONSTANTS__CONFIG_ENV_VARIABLES_ARRAY
|
|
28
|
-
] as const,
|
|
31
|
+
config = new A_Config({
|
|
29
32
|
defaults: {}
|
|
30
33
|
});
|
|
31
34
|
|
|
@@ -39,19 +42,16 @@ export class ConfigReader extends A_Component {
|
|
|
39
42
|
|
|
40
43
|
@A_Concept.Load()
|
|
41
44
|
async initialize(
|
|
45
|
+
@A_Dependency.Required()
|
|
42
46
|
@A_Inject(A_Config) config: A_Config,
|
|
43
47
|
) {
|
|
44
|
-
const data = await this.read(
|
|
45
|
-
...config.CONFIG_PROPERTIES,
|
|
46
|
-
...A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY,
|
|
47
|
-
...A_CONSTANTS__CONFIG_ENV_VARIABLES_ARRAY
|
|
48
|
-
]);
|
|
48
|
+
const data = await this.read();
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
for (const key in data) {
|
|
51
|
+
config.set(key, data[key]);
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
|
|
54
|
-
|
|
55
55
|
/**
|
|
56
56
|
* Get the configuration property by Name
|
|
57
57
|
* @param property
|
|
@@ -79,6 +79,8 @@ export class ConfigReader extends A_Component {
|
|
|
79
79
|
* @returns {string|null} - The path to the root directory or null if package.json is not found
|
|
80
80
|
*/
|
|
81
81
|
protected async getProjectRoot(startPath = __dirname) {
|
|
82
|
+
const process = await this.polyfill.process();
|
|
83
|
+
|
|
82
84
|
return process.cwd();
|
|
83
85
|
}
|
|
84
86
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { A_Fragment, A_Meta, A_TYPES__Fragment_Serialized } from "@adaas/a-concept";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class A_ExecutionContext<
|
|
6
|
+
_MetaType extends Record<string, any> = Record<string, any>,
|
|
7
|
+
_SerializedType extends Record<string, any> = Record<string, any>
|
|
8
|
+
> extends A_Fragment {
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
protected _meta: A_Meta<_MetaType, _SerializedType>;
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
name: string,
|
|
15
|
+
defaults?: Partial<_MetaType>,
|
|
16
|
+
) {
|
|
17
|
+
super({ name });
|
|
18
|
+
this._meta = new A_Meta<_MetaType, _SerializedType>();
|
|
19
|
+
|
|
20
|
+
for (const key in defaults) {
|
|
21
|
+
this._meta.set(key as keyof _MetaType, defaults[key as keyof _MetaType] as _MetaType[keyof _MetaType]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
[Symbol.iterator](): Iterator<[keyof _MetaType, _MetaType[keyof _MetaType]]> {
|
|
27
|
+
return this._meta[Symbol.iterator]();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
get meta(): A_Meta<_MetaType> {
|
|
32
|
+
return this._meta;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
get(key: keyof _MetaType): _MetaType[keyof _MetaType] | undefined {
|
|
37
|
+
return this._meta.get(key);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
set(key: keyof _MetaType, value: _MetaType[keyof _MetaType]): void {
|
|
41
|
+
this._meta.set(key, value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
has(key: keyof _MetaType): boolean {
|
|
45
|
+
return this._meta.has(key);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
drop(key: keyof _MetaType): void {
|
|
49
|
+
this._meta.delete(key);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
clear(): void {
|
|
53
|
+
this._meta.clear();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
toRaw(): _SerializedType {
|
|
58
|
+
return this._meta.toJSON();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
toJSON(): A_TYPES__Fragment_Serialized {
|
|
62
|
+
return {
|
|
63
|
+
name: this.name,
|
|
64
|
+
...this.meta.toJSON(),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
File without changes
|
|
@@ -4,46 +4,46 @@ export enum A_MemoryFeatures {
|
|
|
4
4
|
/**
|
|
5
5
|
* Allows to extend initialization logic and behavior
|
|
6
6
|
*/
|
|
7
|
-
onInit = '
|
|
7
|
+
onInit = '_A_Memory_onInit',
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Allows to extend destruction logic and behavior
|
|
11
11
|
*/
|
|
12
|
-
onDestroy = '
|
|
12
|
+
onDestroy = '_A_Memory_onDestroy',
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Allows to extend expiration logic and behavior
|
|
16
16
|
*/
|
|
17
|
-
onExpire = '
|
|
17
|
+
onExpire = '_A_Memory_onExpire',
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Allows to extend error handling logic and behavior
|
|
21
21
|
*/
|
|
22
|
-
onError = '
|
|
22
|
+
onError = '_A_Memory_onError',
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Allows to extend serialization logic and behavior
|
|
26
26
|
*/
|
|
27
|
-
onSerialize = '
|
|
27
|
+
onSerialize = '_A_Memory_onSerialize',
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Allows to extend set operation logic and behavior
|
|
31
31
|
*/
|
|
32
|
-
onSet = '
|
|
32
|
+
onSet = '_A_Memory_onSet',
|
|
33
33
|
/**
|
|
34
34
|
* Allows to extend get operation logic and behavior
|
|
35
35
|
*/
|
|
36
|
-
onGet = '
|
|
36
|
+
onGet = '_A_Memory_onGet',
|
|
37
37
|
/**
|
|
38
38
|
* Allows to extend drop operation logic and behavior
|
|
39
39
|
*/
|
|
40
|
-
onDrop = '
|
|
40
|
+
onDrop = '_A_Memory_onDrop',
|
|
41
41
|
/**
|
|
42
42
|
* Allows to extend clear operation logic and behavior
|
|
43
43
|
*/
|
|
44
|
-
onClear = '
|
|
44
|
+
onClear = '_A_Memory_onClear',
|
|
45
45
|
/**
|
|
46
46
|
* Allows to extend has operation logic and behavior
|
|
47
47
|
*/
|
|
48
|
-
onHas = '
|
|
48
|
+
onHas = '_A_Memory_onHas',
|
|
49
49
|
}
|
|
@@ -7,20 +7,29 @@ import { error } from "console";
|
|
|
7
7
|
export class A_MemoryContext<
|
|
8
8
|
_MemoryType extends Record<string, any> = Record<string, any>,
|
|
9
9
|
_SerializedType extends A_TYPES__Fragment_Serialized = A_TYPES__Fragment_Serialized
|
|
10
|
-
> extends A_Fragment
|
|
10
|
+
> extends A_Fragment {
|
|
11
|
+
protected _storage: Map<keyof _MemoryType, _MemoryType[keyof _MemoryType]> = new Map();
|
|
12
|
+
|
|
13
|
+
set<K extends keyof _MemoryType>(param: K, value: _MemoryType[K]): void {
|
|
14
|
+
this._storage.set(param, value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
get<K extends keyof _MemoryType>(param: K): _MemoryType[K] | undefined {
|
|
19
|
+
return this._storage.get(param);
|
|
20
|
+
}
|
|
11
21
|
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
set<K extends keyof _MemoryType>(param: K | 'error', value: _MemoryType[K]): void {
|
|
16
|
-
super.set(param , value);
|
|
23
|
+
delete<K extends keyof _MemoryType>(param: K): void {
|
|
24
|
+
this._storage.delete(param);
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
get<K extends keyof _MemoryType>(param: K | 'error'): _MemoryType[K] | undefined {
|
|
23
|
-
return super.get(param);
|
|
28
|
+
has<K extends keyof _MemoryType>(param: K): boolean {
|
|
29
|
+
return this._storage.has(param);
|
|
24
30
|
}
|
|
25
31
|
|
|
32
|
+
clear(): void {
|
|
33
|
+
this._storage.clear();
|
|
34
|
+
}
|
|
26
35
|
}
|
|
@@ -18,4 +18,15 @@ export type A_MemoryOperationContext<T extends any = any> = A_OperationContext<
|
|
|
18
18
|
A_MemoryOperations,
|
|
19
19
|
{ key: string, value?: any },
|
|
20
20
|
T
|
|
21
|
-
>;
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export type A_MemoryOperationContextMeta<
|
|
25
|
+
T extends any = any,
|
|
26
|
+
I extends any = any
|
|
27
|
+
> = {
|
|
28
|
+
result: T,
|
|
29
|
+
operation: A_MemoryOperations,
|
|
30
|
+
key: string,
|
|
31
|
+
value?: I,
|
|
32
|
+
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { A_Error, A_Fragment } from "@adaas/a-concept";
|
|
2
2
|
import { A_Operation_Serialized, A_Operation_Storage } from "./A-Operation.types";
|
|
3
|
+
import { A_ExecutionContext } from "../A-Execution/A-Execution.context";
|
|
3
4
|
|
|
4
5
|
export class A_OperationContext<
|
|
5
6
|
_AllowedOperations extends string = string,
|
|
6
7
|
_ParamsType extends Record<string, any> = Record<string, any>,
|
|
7
8
|
_ResultType = any,
|
|
8
9
|
_StorageType extends A_Operation_Storage<_ResultType, _ParamsType> = A_Operation_Storage<_ResultType, _ParamsType>
|
|
9
|
-
> extends
|
|
10
|
+
> extends A_ExecutionContext<
|
|
10
11
|
_StorageType,
|
|
11
12
|
A_Operation_Serialized<_ResultType, _ParamsType>
|
|
12
13
|
> {
|
|
@@ -15,7 +16,7 @@ export class A_OperationContext<
|
|
|
15
16
|
operation: _AllowedOperations,
|
|
16
17
|
params?: _ParamsType
|
|
17
18
|
) {
|
|
18
|
-
super();
|
|
19
|
+
super('operation-context');
|
|
19
20
|
|
|
20
21
|
this.meta.set('name', operation);
|
|
21
22
|
this.meta.set('params', params || {} as _ParamsType);
|
|
@@ -2,17 +2,17 @@ export enum A_StateMachineFeatures {
|
|
|
2
2
|
/**
|
|
3
3
|
* Allows to extend error handling logic and behavior
|
|
4
4
|
*/
|
|
5
|
-
onError = '
|
|
5
|
+
onError = '_A_StateMachine_onError',
|
|
6
6
|
/**
|
|
7
7
|
* Allows to extend initialization logic and behavior
|
|
8
8
|
*/
|
|
9
|
-
onInitialize = '
|
|
9
|
+
onInitialize = '_A_StateMachine_onInitialize',
|
|
10
10
|
/**
|
|
11
11
|
* Allows to extend transition validation logic and behavior
|
|
12
12
|
*/
|
|
13
|
-
onBeforeTransition = '
|
|
13
|
+
onBeforeTransition = '_A_StateMachine_onBeforeTransition',
|
|
14
14
|
/**
|
|
15
15
|
* Allows to extend post-transition logic and behavior
|
|
16
16
|
*/
|
|
17
|
-
onAfterTransition = '
|
|
17
|
+
onAfterTransition = '_A_StateMachine_onAfterTransition',
|
|
18
18
|
}
|
package/tests/A-Command.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { A_Command } from '@adaas/a-utils/lib/A-Command/A-Command.entity';
|
|
2
2
|
import { A_CommandError } from '@adaas/a-utils/lib/A-Command/A-Command.error';
|
|
3
|
-
import { A_Command_Status, A_CommandFeatures } from '@adaas/a-utils/lib/A-Command/A-Command.constants';
|
|
3
|
+
import { A_Command_Status, A_CommandEvent, A_CommandFeatures } from '@adaas/a-utils/lib/A-Command/A-Command.constants';
|
|
4
4
|
import { A_TYPES__Command_Serialized } from '@adaas/a-utils/lib/A-Command/A-Command.types';
|
|
5
5
|
import { A_StateMachine } from '@adaas/a-utils/lib/A-StateMachine/A-StateMachine.component';
|
|
6
6
|
import { A_Memory } from '@adaas/a-utils/lib/A-Memory/A-Memory.component';
|
|
@@ -73,8 +73,8 @@ describe('A-Command tests', () => {
|
|
|
73
73
|
const command = new TestCommand({ userId: '123', action: 'create' });
|
|
74
74
|
const listener = jest.fn();
|
|
75
75
|
|
|
76
|
-
command.on(
|
|
77
|
-
command.emit(
|
|
76
|
+
command.on('onComplete', listener);
|
|
77
|
+
command.emit(A_CommandEvent.onComplete);
|
|
78
78
|
|
|
79
79
|
expect(listener).toHaveBeenCalledWith(command);
|
|
80
80
|
});
|
|
@@ -83,9 +83,9 @@ describe('A-Command tests', () => {
|
|
|
83
83
|
const command = new TestCommand({ userId: '123', action: 'create' });
|
|
84
84
|
const listener = jest.fn();
|
|
85
85
|
|
|
86
|
-
command.on(
|
|
87
|
-
command.off(
|
|
88
|
-
command.emit(
|
|
86
|
+
command.on(A_CommandEvent.onComplete, listener);
|
|
87
|
+
command.off(A_CommandEvent.onComplete, listener);
|
|
88
|
+
command.emit(A_CommandEvent.onComplete);
|
|
89
89
|
|
|
90
90
|
expect(listener).not.toHaveBeenCalled();
|
|
91
91
|
});
|
package/tests/A-Config.test.ts
CHANGED
|
@@ -32,17 +32,11 @@ describe('A-Config tests', () => {
|
|
|
32
32
|
expect(config.get('TEST_VAR2')).toBeUndefined();
|
|
33
33
|
});
|
|
34
34
|
it('Should Allow to create a config object with ENV values', async () => {
|
|
35
|
+
|
|
35
36
|
process.env['TEST_VAR1'] = 'env1';
|
|
36
37
|
process.env['TEST_VAR2'] = 'env2';
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
// It does automatically by jest.setup.ts script
|
|
40
|
-
const { A_ConfigLoader } = await import('@adaas/a-utils/lib/A-Config/A-Config.container');
|
|
41
|
-
const { FileConfigReader } = await import('@adaas/a-utils/lib/A-Config/components/FileConfigReader.component');
|
|
42
|
-
const { ENVConfigReader } = await import('@adaas/a-utils/lib/A-Config/components/ENVConfigReader.component');
|
|
43
|
-
|
|
44
|
-
const config = new A_Config({
|
|
45
|
-
variables: ['TEST_VAR1', 'TEST_VAR2'] as const,
|
|
39
|
+
const config = new A_Config<['TEST_VAR1', 'TEST_VAR2']>({
|
|
46
40
|
defaults: {
|
|
47
41
|
TEST_VAR1: 'default1',
|
|
48
42
|
}
|
|
@@ -130,7 +124,7 @@ describe('A-Config tests', () => {
|
|
|
130
124
|
const configLoader = new A_ConfigLoader({
|
|
131
125
|
name: 'test-config-loader',
|
|
132
126
|
fragments: [config],
|
|
133
|
-
components: [A_Logger,A_Polyfill, FileConfigReader]
|
|
127
|
+
components: [A_Logger, A_Polyfill, FileConfigReader]
|
|
134
128
|
})
|
|
135
129
|
|
|
136
130
|
const concept = new A_Concept({
|
|
@@ -147,7 +141,7 @@ describe('A-Config tests', () => {
|
|
|
147
141
|
fs.unlinkSync('a-concept.conf.json');
|
|
148
142
|
});
|
|
149
143
|
it('Should Allow to create a config object with variables from File with different variable cases', async () => {
|
|
150
|
-
|
|
144
|
+
|
|
151
145
|
// 1. create a config file
|
|
152
146
|
fs.writeFileSync('a-concept.conf.json', JSON.stringify({
|
|
153
147
|
testVar2: 'env2'
|
|
@@ -163,7 +157,7 @@ describe('A-Config tests', () => {
|
|
|
163
157
|
const configLoader = new A_ConfigLoader({
|
|
164
158
|
name: 'test-config-loader',
|
|
165
159
|
fragments: [config],
|
|
166
|
-
components: [A_Logger,A_Polyfill, FileConfigReader]
|
|
160
|
+
components: [A_Logger, A_Polyfill, FileConfigReader]
|
|
167
161
|
})
|
|
168
162
|
|
|
169
163
|
const concept = new A_Concept({
|
package/tests/A-Manifest.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A_Component } from "@adaas/a-concept";
|
|
1
|
+
import { A_Component, A_Entity } from "@adaas/a-concept";
|
|
2
2
|
import { A_Manifest } from "../src/lib/A-Manifest/A-Manifest.context";
|
|
3
3
|
|
|
4
4
|
describe('A_Manifest', () => {
|
|
@@ -84,8 +84,8 @@ describe('A_Manifest', () => {
|
|
|
84
84
|
put() { return 'user.put'; }
|
|
85
85
|
delete() { return 'user.delete'; }
|
|
86
86
|
}
|
|
87
|
-
class GuestUser extends
|
|
88
|
-
class RegisteredUser extends
|
|
87
|
+
class GuestUser extends A_Entity {}
|
|
88
|
+
class RegisteredUser extends A_Entity {}
|
|
89
89
|
|
|
90
90
|
const manifest = new A_Manifest([
|
|
91
91
|
{
|