@adaas/a-concept 0.3.2 → 0.3.3
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/browser/index.d.mts +40 -3
- package/dist/browser/index.mjs +2 -2
- package/dist/browser/index.mjs.map +1 -1
- package/dist/node/index.cjs +38 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.mts +40 -3
- package/dist/node/index.d.ts +40 -3
- package/dist/node/index.mjs +38 -1
- package/dist/node/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/lib/A-Context/A-Context.class.ts +3 -3
- package/src/lib/A-Meta/A-Meta.class.ts +22 -26
- package/src/lib/A-Scope/A-Scope.class.ts +59 -6
- package/tests/A-Meta.test.ts +44 -1
- package/tests/A-Scope.test.ts +17 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaas/a-concept",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "A-Concept is a framework of the new generation that is tailored to use AI, enabling developers to create AI-powered applications with ease. It provides a structured approach to building, managing, and deploying AI-driven solutions.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -364,11 +364,11 @@ export class A_Context {
|
|
|
364
364
|
*/
|
|
365
365
|
entity: A_TYPES__Entity_Constructor,
|
|
366
366
|
): T
|
|
367
|
-
static meta<T extends A_EntityMeta>(
|
|
367
|
+
static meta<T extends A_EntityMeta, E extends A_Entity>(
|
|
368
368
|
/**
|
|
369
369
|
* Get meta for the specific entity instance.
|
|
370
370
|
*/
|
|
371
|
-
entity:
|
|
371
|
+
entity: E,
|
|
372
372
|
): T
|
|
373
373
|
static meta<T extends A_ComponentMeta>(
|
|
374
374
|
/**
|
|
@@ -545,7 +545,7 @@ export class A_Context {
|
|
|
545
545
|
if (!inheritedMeta)
|
|
546
546
|
inheritedMeta = new metaType();
|
|
547
547
|
|
|
548
|
-
instance._metaStorage.set(property,
|
|
548
|
+
instance._metaStorage.set(property, inheritedMeta.clone());
|
|
549
549
|
}
|
|
550
550
|
|
|
551
551
|
// Return the meta for the property
|
|
@@ -56,8 +56,17 @@ export class A_Meta<
|
|
|
56
56
|
|
|
57
57
|
return this;
|
|
58
58
|
}
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Allows to create a copy of the meta object with the same values, this is needed to ensure that when we inherit meta from the parent component, we create a copy of it, not a reference to the same object. This allows us to modify the meta of the child component without affecting the meta of the parent component.
|
|
61
|
+
*
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
clone(): A_Meta<_StorageItems> {
|
|
65
|
+
const ctor = this.constructor as new () => this;
|
|
66
|
+
const copy = new ctor();
|
|
67
|
+
copy.meta = new Map(this.meta);
|
|
68
|
+
return copy;
|
|
69
|
+
}
|
|
61
70
|
/**
|
|
62
71
|
* Method to set values in the map
|
|
63
72
|
*
|
|
@@ -83,9 +92,6 @@ export class A_Meta<
|
|
|
83
92
|
this.meta.set(key, value);
|
|
84
93
|
|
|
85
94
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
95
|
/**
|
|
90
96
|
* Method to get values from the map
|
|
91
97
|
*
|
|
@@ -95,8 +101,6 @@ export class A_Meta<
|
|
|
95
101
|
get<K extends keyof _StorageItems>(key: K): _StorageItems[K] | undefined {
|
|
96
102
|
return this.meta.get(key) as _StorageItems[K];
|
|
97
103
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
104
|
/**
|
|
101
105
|
* Method to delete values from the map
|
|
102
106
|
*
|
|
@@ -106,8 +110,6 @@ export class A_Meta<
|
|
|
106
110
|
delete(key: keyof _StorageItems): boolean {
|
|
107
111
|
return this.meta.delete(key);
|
|
108
112
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
113
|
/**
|
|
112
114
|
* Method to get the size of the map
|
|
113
115
|
*
|
|
@@ -116,8 +118,6 @@ export class A_Meta<
|
|
|
116
118
|
size(): number {
|
|
117
119
|
return this.meta.size;
|
|
118
120
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
121
|
/**
|
|
122
122
|
* This method is needed to convert the key to a regular expression and cover cases like:
|
|
123
123
|
*
|
|
@@ -135,8 +135,6 @@ export class A_Meta<
|
|
|
135
135
|
? key
|
|
136
136
|
: new RegExp(key);
|
|
137
137
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
138
|
/**
|
|
141
139
|
* Method to find values in the map by name.
|
|
142
140
|
*
|
|
@@ -154,8 +152,6 @@ export class A_Meta<
|
|
|
154
152
|
}
|
|
155
153
|
return results;
|
|
156
154
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
155
|
/**
|
|
160
156
|
* Method to find values in the map by regular expression
|
|
161
157
|
*
|
|
@@ -173,8 +169,6 @@ export class A_Meta<
|
|
|
173
169
|
}
|
|
174
170
|
return results;
|
|
175
171
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
172
|
/**
|
|
179
173
|
* Method to check if the map has a specific key
|
|
180
174
|
*
|
|
@@ -184,8 +178,6 @@ export class A_Meta<
|
|
|
184
178
|
has(key: keyof _StorageItems): boolean {
|
|
185
179
|
return this.meta.has(key);
|
|
186
180
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
181
|
/**
|
|
190
182
|
* Method to get the size of the map
|
|
191
183
|
*
|
|
@@ -194,21 +186,26 @@ export class A_Meta<
|
|
|
194
186
|
entries(): IterableIterator<[keyof _StorageItems, _StorageItems[keyof _StorageItems]]> {
|
|
195
187
|
return this.meta.entries();
|
|
196
188
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
189
|
/**
|
|
200
190
|
* Method to clear the map
|
|
201
191
|
*/
|
|
202
192
|
clear(): void {
|
|
203
193
|
this.meta.clear();
|
|
204
194
|
}
|
|
205
|
-
|
|
206
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Method to convert the meta to an array of key-value pairs
|
|
197
|
+
*
|
|
198
|
+
* @returns
|
|
199
|
+
*/
|
|
207
200
|
toArray(): Array<[keyof _StorageItems, _StorageItems[keyof _StorageItems]]> {
|
|
208
201
|
return Array.from(this.meta.entries());
|
|
209
202
|
}
|
|
210
|
-
|
|
211
|
-
|
|
203
|
+
/**
|
|
204
|
+
* Helper method to recursively convert the meta object to a JSON-compatible format. It handles nested A_Meta instances, Maps, Arrays, and plain objects.
|
|
205
|
+
*
|
|
206
|
+
* @param value
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
212
209
|
protected recursiveToJSON(value: any): any {
|
|
213
210
|
switch (true) {
|
|
214
211
|
case value instanceof A_Meta:
|
|
@@ -235,7 +232,6 @@ export class A_Meta<
|
|
|
235
232
|
return value;
|
|
236
233
|
}
|
|
237
234
|
}
|
|
238
|
-
|
|
239
235
|
/**
|
|
240
236
|
* Serializes the meta to a JSON object
|
|
241
237
|
* Uses internal storage to convert to JSON
|
|
@@ -19,9 +19,9 @@ import {
|
|
|
19
19
|
A_Entity,
|
|
20
20
|
A_TYPES__Entity_Constructor
|
|
21
21
|
} from "@adaas/a-concept/a-entity";
|
|
22
|
-
import { A_TypeGuards} from "@adaas/a-concept/helpers/A_TypeGuards.helper";
|
|
23
|
-
import { A_FormatterHelper} from "@adaas/a-concept/helpers/A_Formatter.helper";
|
|
24
|
-
import { A_CommonHelper} from "@adaas/a-concept/helpers/A_Common.helper";
|
|
22
|
+
import { A_TypeGuards } from "@adaas/a-concept/helpers/A_TypeGuards.helper";
|
|
23
|
+
import { A_FormatterHelper } from "@adaas/a-concept/helpers/A_Formatter.helper";
|
|
24
|
+
import { A_CommonHelper } from "@adaas/a-concept/helpers/A_Common.helper";
|
|
25
25
|
import {
|
|
26
26
|
A_Error,
|
|
27
27
|
A_TYPES__Error_Constructor
|
|
@@ -179,7 +179,7 @@ export class A_Scope<
|
|
|
179
179
|
get parent(): A_Scope | undefined {
|
|
180
180
|
return this._parent;
|
|
181
181
|
}
|
|
182
|
-
|
|
182
|
+
|
|
183
183
|
/**
|
|
184
184
|
* A_Scope is a unique A-Concept Structure that allows to operate with A-Concept Primitives and Models in a specific context and with specific rules.
|
|
185
185
|
* It refers to the visibility and accessibility of :
|
|
@@ -839,8 +839,61 @@ export class A_Scope<
|
|
|
839
839
|
*/
|
|
840
840
|
name: string
|
|
841
841
|
): A_TYPES__Fragment_Constructor<T>
|
|
842
|
-
resolveConstructor<T extends
|
|
843
|
-
|
|
842
|
+
resolveConstructor<T extends A_Component>(
|
|
843
|
+
/**
|
|
844
|
+
* Provide the component constructor or its name to retrieve its constructor
|
|
845
|
+
*/
|
|
846
|
+
component: A_TYPES__Ctor<T>
|
|
847
|
+
): A_TYPES__Component_Constructor<T> | undefined
|
|
848
|
+
resolveConstructor<T extends A_Entity>(
|
|
849
|
+
/**
|
|
850
|
+
* Provide the entity constructor or its name to retrieve its constructor
|
|
851
|
+
*/
|
|
852
|
+
entity: A_TYPES__Ctor<T>
|
|
853
|
+
): A_TYPES__Entity_Constructor<T> | undefined
|
|
854
|
+
resolveConstructor<T extends A_Fragment>(
|
|
855
|
+
/**
|
|
856
|
+
* Provide the fragment constructor or its name to retrieve its constructor
|
|
857
|
+
*/
|
|
858
|
+
fragment: A_TYPES__Ctor<T>
|
|
859
|
+
): A_TYPES__Fragment_Constructor<T> | undefined
|
|
860
|
+
resolveConstructor<T extends A_Error>(
|
|
861
|
+
/**
|
|
862
|
+
* Provide the error constructor or its name to retrieve its constructor
|
|
863
|
+
*/
|
|
864
|
+
error: A_TYPES__Ctor<T>
|
|
865
|
+
): A_TYPES__Error_Constructor<T> | undefined
|
|
866
|
+
resolveConstructor<T extends A_TYPES__A_DependencyInjectable>(
|
|
867
|
+
name: string | A_TYPES__Ctor<T>
|
|
868
|
+
): A_TYPES__Entity_Constructor<T> | A_TYPES__Component_Constructor<T> | A_TYPES__Fragment_Constructor<T> | undefined
|
|
869
|
+
resolveConstructor<T extends A_TYPES__A_DependencyInjectable>(
|
|
870
|
+
name: string | A_TYPES__Ctor<T>
|
|
871
|
+
): A_TYPES__Entity_Constructor<T> | A_TYPES__Component_Constructor<T> | A_TYPES__Fragment_Constructor<T> | undefined {
|
|
872
|
+
// If it's a constructor, find any extension from the allowed constructors and return it
|
|
873
|
+
switch (true) {
|
|
874
|
+
case A_TypeGuards.isComponentConstructor(name):
|
|
875
|
+
return Array.from(this.allowedComponents)
|
|
876
|
+
.find((c) => A_CommonHelper.isInheritedFrom(c, name)) as A_TYPES__Component_Constructor<T> | undefined;
|
|
877
|
+
|
|
878
|
+
case A_TypeGuards.isEntityConstructor(name):
|
|
879
|
+
return Array.from(this.allowedEntities)
|
|
880
|
+
.find((e) => A_CommonHelper.isInheritedFrom(e, name)) as A_TYPES__Entity_Constructor<T> | undefined;
|
|
881
|
+
|
|
882
|
+
case A_TypeGuards.isFragmentConstructor(name):
|
|
883
|
+
return Array.from(this.allowedFragments)
|
|
884
|
+
.find((f) => A_CommonHelper.isInheritedFrom(f, name)) as A_TYPES__Fragment_Constructor<T> | undefined;
|
|
885
|
+
|
|
886
|
+
case A_TypeGuards.isErrorConstructor(name):
|
|
887
|
+
return Array.from(this.allowedErrors)
|
|
888
|
+
.find((e) => A_CommonHelper.isInheritedFrom(e, name)) as A_TYPES__Error_Constructor<T> | undefined;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
if(!A_TypeGuards.isString(name))
|
|
892
|
+
throw new A_ScopeError(
|
|
893
|
+
A_ScopeError.ResolutionError,
|
|
894
|
+
`Invalid constructor name provided: ${name}`
|
|
895
|
+
);
|
|
896
|
+
|
|
844
897
|
// 1) Check components
|
|
845
898
|
const component = Array.from(this.allowedComponents).find(
|
|
846
899
|
c => c.name === name
|
package/tests/A-Meta.test.ts
CHANGED
|
@@ -132,7 +132,7 @@ describe('A-Meta tests', () => {
|
|
|
132
132
|
expect(metaE.get(A_TYPES__ComponentMetaKey.EXTENSIONS)?.get('.*\\.testFeature$')?.map(e => e.handler)).toContain('test3');
|
|
133
133
|
expect(metaE.get(A_TYPES__ComponentMetaKey.EXTENSIONS)?.get('.*\\.testFeature$')?.map(e => e.handler)).not.toContain('test2');
|
|
134
134
|
})
|
|
135
|
-
it('Should allow to
|
|
135
|
+
it('Should allow to define a custom meta', async () => {
|
|
136
136
|
|
|
137
137
|
class InjectableComponent extends A_Component { }
|
|
138
138
|
|
|
@@ -170,6 +170,49 @@ describe('A-Meta tests', () => {
|
|
|
170
170
|
expect(meta).toBeInstanceOf(CustomComponentMeta);
|
|
171
171
|
expect(meta).toHaveProperty('customField');
|
|
172
172
|
|
|
173
|
+
expect(meta.get(A_TYPES__ComponentMetaKey.EXTENSIONS)?.size()).toBe(1);
|
|
174
|
+
expect(meta.get(A_TYPES__ComponentMetaKey.EXTENSIONS)?.has('.*\\.testFeature$')).toBe(true);
|
|
175
|
+
})
|
|
176
|
+
it('Should propagate a custom me', async () => {
|
|
177
|
+
|
|
178
|
+
class InjectableComponent extends A_Component { }
|
|
179
|
+
|
|
180
|
+
class CustomComponentMeta extends A_ComponentMeta<{ customField: string } & A_TYPES__ComponentMeta> {
|
|
181
|
+
|
|
182
|
+
get customField(): string | undefined {
|
|
183
|
+
return this.get('customField');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
@A_Meta.Define(CustomComponentMeta)
|
|
188
|
+
class CustomComponent extends A_Component {
|
|
189
|
+
|
|
190
|
+
static Test() {
|
|
191
|
+
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
constructor(
|
|
195
|
+
@A_Inject(InjectableComponent) public injectableComponent: InjectableComponent
|
|
196
|
+
) {
|
|
197
|
+
super();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
@A_Feature.Extend({
|
|
201
|
+
name: 'testFeature'
|
|
202
|
+
})
|
|
203
|
+
async feature() {
|
|
204
|
+
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
class SubCustomComponent extends CustomComponent {}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
const meta = A_Context.meta<CustomComponentMeta>(SubCustomComponent);
|
|
212
|
+
|
|
213
|
+
expect(meta).toBeInstanceOf(CustomComponentMeta);
|
|
214
|
+
expect(meta).toHaveProperty('customField');
|
|
215
|
+
|
|
173
216
|
expect(meta.get(A_TYPES__ComponentMetaKey.EXTENSIONS)?.size()).toBe(1);
|
|
174
217
|
expect(meta.get(A_TYPES__ComponentMetaKey.EXTENSIONS)?.has('.*\\.testFeature$')).toBe(true);
|
|
175
218
|
})
|
package/tests/A-Scope.test.ts
CHANGED
|
@@ -106,7 +106,24 @@ describe('A-Scope tests', () => {
|
|
|
106
106
|
const resolved = childScope.resolve(A_Component);
|
|
107
107
|
expect(resolved).toBe(component);
|
|
108
108
|
});
|
|
109
|
+
it('should resolve component constructor registered in scope', async () => {
|
|
110
|
+
class testComponent extends A_Component { }
|
|
109
111
|
|
|
112
|
+
const parentScope = new A_Scope({ name: 'ParentScope', components: [testComponent] });
|
|
113
|
+
|
|
114
|
+
const resolved = parentScope.resolveConstructor(testComponent);
|
|
115
|
+
expect(resolved).toBe(testComponent);
|
|
116
|
+
});
|
|
117
|
+
it('should resolve inherited component constructor in scope', async () => {
|
|
118
|
+
class testComponent extends A_Component { }
|
|
119
|
+
class inheritedComponent extends testComponent { }
|
|
120
|
+
|
|
121
|
+
const parentScope = new A_Scope({ name: 'ParentScope', components: [inheritedComponent] });
|
|
122
|
+
|
|
123
|
+
const resolved = parentScope.resolveConstructor(testComponent);
|
|
124
|
+
|
|
125
|
+
expect(resolved).toBe(inheritedComponent);
|
|
126
|
+
});
|
|
110
127
|
it('Should resolve only one entity if no query is provided', async () => {
|
|
111
128
|
class MyEntity extends A_Entity<{ bar: string }> {
|
|
112
129
|
public bar!: string;
|