@bool-ts/core 1.0.8 → 1.0.9
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.
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export declare const injectKey = "design:paramtypes";
|
|
2
|
-
export declare const Inject: <T
|
|
2
|
+
export declare const Inject: <T extends Object>(classDefinition: T) => (target: Object, parameterName: string | Symbol | undefined, parameterIndex: number) => void;
|
|
3
3
|
export default Inject;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export const injectKey = "design:paramtypes";
|
|
2
|
-
export const Inject = (
|
|
2
|
+
export const Inject = (classDefinition) => {
|
|
3
3
|
return (target, parameterName, parameterIndex) => {
|
|
4
4
|
const designParameterTypes = Reflect.getMetadata(injectKey, target.constructor) || [];
|
|
5
|
-
designParameterTypes[parameterIndex] = constructor;
|
|
5
|
+
designParameterTypes[parameterIndex] = classDefinition.constructor;
|
|
6
6
|
Reflect.defineMetadata(injectKey, designParameterTypes, target.constructor);
|
|
7
7
|
};
|
|
8
8
|
};
|
package/dist/hooks/injector.js
CHANGED
|
@@ -6,19 +6,19 @@ export const Injector = new class {
|
|
|
6
6
|
*
|
|
7
7
|
* @param constructor
|
|
8
8
|
*/
|
|
9
|
-
get(
|
|
10
|
-
if (this._mapper.has(
|
|
11
|
-
return this._mapper.get(
|
|
9
|
+
get(classDefinition) {
|
|
10
|
+
if (this._mapper.has(classDefinition.constructor)) {
|
|
11
|
+
return this._mapper.get(classDefinition.constructor);
|
|
12
12
|
}
|
|
13
|
-
const ownMetadataKeys = Reflect.getOwnMetadataKeys(
|
|
13
|
+
const ownMetadataKeys = Reflect.getOwnMetadataKeys(classDefinition.constructor);
|
|
14
14
|
if (!ownMetadataKeys.includes(injectableKey)) {
|
|
15
15
|
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
16
16
|
}
|
|
17
17
|
// Initialize dependencies injection
|
|
18
|
-
const dependencies = Reflect.getOwnMetadata("design:paramtypes",
|
|
18
|
+
const dependencies = Reflect.getOwnMetadata("design:paramtypes", classDefinition.constructor) || [];
|
|
19
19
|
const injections = dependencies.map(dependency => Injector.get(dependency));
|
|
20
|
-
const instance =
|
|
21
|
-
this._mapper.set(
|
|
20
|
+
const instance = classDefinition.constructor(...injections);
|
|
21
|
+
this._mapper.set(classDefinition.constructor, instance);
|
|
22
22
|
return instance;
|
|
23
23
|
}
|
|
24
24
|
};
|
package/package.json
CHANGED
package/src/decorators/inject.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const injectKey = "design:paramtypes";
|
|
2
2
|
|
|
3
|
-
export const Inject = <T>(
|
|
4
|
-
|
|
3
|
+
export const Inject = <T extends Object>(
|
|
4
|
+
classDefinition: T
|
|
5
5
|
) => {
|
|
6
6
|
return (
|
|
7
7
|
target: Object,
|
|
@@ -10,7 +10,7 @@ export const Inject = <T>(
|
|
|
10
10
|
) => {
|
|
11
11
|
const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target.constructor) || [];
|
|
12
12
|
|
|
13
|
-
designParameterTypes[parameterIndex] = constructor;
|
|
13
|
+
designParameterTypes[parameterIndex] = classDefinition.constructor;
|
|
14
14
|
|
|
15
15
|
Reflect.defineMetadata(injectKey, designParameterTypes, target.constructor);
|
|
16
16
|
}
|
package/src/hooks/injector.ts
CHANGED
|
@@ -16,24 +16,24 @@ export const Injector: IInjector = new class {
|
|
|
16
16
|
* @param constructor
|
|
17
17
|
*/
|
|
18
18
|
get<T extends Object>(
|
|
19
|
-
|
|
19
|
+
classDefinition: T
|
|
20
20
|
) {
|
|
21
|
-
if (this._mapper.has(
|
|
22
|
-
return this._mapper.get(
|
|
21
|
+
if (this._mapper.has(classDefinition.constructor)) {
|
|
22
|
+
return this._mapper.get(classDefinition.constructor) as T;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const ownMetadataKeys = Reflect.getOwnMetadataKeys(
|
|
25
|
+
const ownMetadataKeys = Reflect.getOwnMetadataKeys(classDefinition.constructor);
|
|
26
26
|
|
|
27
27
|
if (!ownMetadataKeys.includes(injectableKey)) {
|
|
28
28
|
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
// Initialize dependencies injection
|
|
32
|
-
const dependencies: any[] = Reflect.getOwnMetadata("design:paramtypes",
|
|
32
|
+
const dependencies: any[] = Reflect.getOwnMetadata("design:paramtypes", classDefinition.constructor) || [];
|
|
33
33
|
const injections: any[] = dependencies.map(dependency => Injector.get(dependency));
|
|
34
|
-
const instance =
|
|
34
|
+
const instance = classDefinition.constructor(...injections);
|
|
35
35
|
|
|
36
|
-
this._mapper.set(
|
|
36
|
+
this._mapper.set(classDefinition.constructor, instance);
|
|
37
37
|
|
|
38
38
|
return instance;
|
|
39
39
|
}
|