@bool-ts/core 1.0.8 → 1.0.10
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/__test/controller.ts +1 -0
- package/dist/decorators/inject.d.ts +3 -1
- package/dist/decorators/inject.js +4 -4
- package/dist/decorators/injectable.js +1 -1
- package/dist/hooks/injector.d.ts +3 -1
- package/dist/hooks/injector.js +8 -8
- package/package.json +2 -2
- package/src/decorators/inject.ts +5 -5
- package/src/decorators/injectable.ts +1 -1
- package/src/hooks/injector.ts +10 -10
package/__test/controller.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare const injectKey = "design:paramtypes";
|
|
2
|
-
export declare const Inject: <T
|
|
2
|
+
export declare const Inject: <T extends Object>(classDefinition: {
|
|
3
|
+
new (...args: any[]): T;
|
|
4
|
+
}) => (target: Object, parameterName: string | Symbol | undefined, parameterIndex: number) => void;
|
|
3
5
|
export default Inject;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export const injectKey = "design:paramtypes";
|
|
2
|
-
export const Inject = (
|
|
2
|
+
export const Inject = (classDefinition) => {
|
|
3
3
|
return (target, parameterName, parameterIndex) => {
|
|
4
|
-
const designParameterTypes = Reflect.getMetadata(injectKey, target
|
|
5
|
-
designParameterTypes[parameterIndex] =
|
|
6
|
-
Reflect.defineMetadata(injectKey, designParameterTypes, target
|
|
4
|
+
const designParameterTypes = Reflect.getMetadata(injectKey, target) || [];
|
|
5
|
+
designParameterTypes[parameterIndex] = classDefinition;
|
|
6
|
+
Reflect.defineMetadata(injectKey, designParameterTypes, target);
|
|
7
7
|
};
|
|
8
8
|
};
|
|
9
9
|
export default Inject;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const injectableKey = "__bool:injectable__";
|
|
2
2
|
export const Injectable = () => (target, context) => {
|
|
3
|
-
Reflect.defineMetadata(injectableKey, undefined, target
|
|
3
|
+
Reflect.defineMetadata(injectableKey, undefined, target);
|
|
4
4
|
return target;
|
|
5
5
|
};
|
|
6
6
|
export default Injectable;
|
package/dist/hooks/injector.d.ts
CHANGED
package/dist/hooks/injector.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import { injectableKey } from "../decorators";
|
|
2
|
+
import { injectableKey, injectKey } from "../decorators";
|
|
3
3
|
export const Injector = new class {
|
|
4
4
|
_mapper = new Map();
|
|
5
5
|
/**
|
|
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)) {
|
|
11
|
+
return this._mapper.get(classDefinition);
|
|
12
12
|
}
|
|
13
|
-
const ownMetadataKeys = Reflect.
|
|
13
|
+
const ownMetadataKeys = Reflect.getMetadataKeys(classDefinition);
|
|
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(
|
|
18
|
+
const dependencies = Reflect.getOwnMetadata(injectKey, classDefinition) || [];
|
|
19
19
|
const injections = dependencies.map(dependency => Injector.get(dependency));
|
|
20
|
-
const instance =
|
|
21
|
-
this._mapper.set(
|
|
20
|
+
const instance = new classDefinition(...injections);
|
|
21
|
+
this._mapper.set(classDefinition, instance);
|
|
22
22
|
return instance;
|
|
23
23
|
}
|
|
24
24
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bool-ts/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "bun run __test/index.ts --watch",
|
|
8
8
|
"build": "tsc"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
package/src/decorators/inject.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
export const injectKey = "design:paramtypes";
|
|
2
2
|
|
|
3
|
-
export const Inject = <T>(
|
|
4
|
-
|
|
3
|
+
export const Inject = <T extends Object>(
|
|
4
|
+
classDefinition: { new(...args: any[]): T }
|
|
5
5
|
) => {
|
|
6
6
|
return (
|
|
7
7
|
target: Object,
|
|
8
8
|
parameterName: string | Symbol | undefined,
|
|
9
9
|
parameterIndex: number
|
|
10
10
|
) => {
|
|
11
|
-
const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target
|
|
11
|
+
const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target) || [];
|
|
12
12
|
|
|
13
|
-
designParameterTypes[parameterIndex] =
|
|
13
|
+
designParameterTypes[parameterIndex] = classDefinition;
|
|
14
14
|
|
|
15
|
-
Reflect.defineMetadata(injectKey, designParameterTypes, target
|
|
15
|
+
Reflect.defineMetadata(injectKey, designParameterTypes, target);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
package/src/hooks/injector.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
|
|
3
|
-
import { injectableKey } from "../decorators";
|
|
3
|
+
import { injectableKey, injectKey } from "../decorators";
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
interface IInjector {
|
|
7
|
-
get<T>(
|
|
7
|
+
get<T>(classDefinition: { new(...args: any[]): T }): T
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export const Injector: IInjector = new class {
|
|
@@ -15,25 +15,25 @@ export const Injector: IInjector = new class {
|
|
|
15
15
|
*
|
|
16
16
|
* @param constructor
|
|
17
17
|
*/
|
|
18
|
-
get<T
|
|
19
|
-
|
|
18
|
+
get<T>(
|
|
19
|
+
classDefinition: { new(...args: any[]): T }
|
|
20
20
|
) {
|
|
21
|
-
if (this._mapper.has(
|
|
22
|
-
return this._mapper.get(
|
|
21
|
+
if (this._mapper.has(classDefinition)) {
|
|
22
|
+
return this._mapper.get(classDefinition) as T;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const ownMetadataKeys = Reflect.
|
|
25
|
+
const ownMetadataKeys = Reflect.getMetadataKeys(classDefinition);
|
|
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(
|
|
32
|
+
const dependencies: any[] = Reflect.getOwnMetadata(injectKey, classDefinition) || [];
|
|
33
33
|
const injections: any[] = dependencies.map(dependency => Injector.get(dependency));
|
|
34
|
-
const instance =
|
|
34
|
+
const instance = new classDefinition(...injections);
|
|
35
35
|
|
|
36
|
-
this._mapper.set(
|
|
36
|
+
this._mapper.set(classDefinition, instance);
|
|
37
37
|
|
|
38
38
|
return instance;
|
|
39
39
|
}
|