@tsed/di 6.68.1 → 6.69.0-rc.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/lib/index.js +2537 -23
- package/lib/index.js.map +1 -1
- package/lib/index.modern.js +2474 -0
- package/lib/index.modern.js.map +1 -0
- package/lib/registries/GlobalProviders.d.ts +1 -1
- package/package.json +11 -8
- package/lib/constants/index.js +0 -7
- package/lib/constants/index.js.map +0 -1
- package/lib/decorators/configuration.js +0 -24
- package/lib/decorators/configuration.js.map +0 -1
- package/lib/decorators/constant.js +0 -55
- package/lib/decorators/constant.js.map +0 -1
- package/lib/decorators/index.js +0 -17
- package/lib/decorators/index.js.map +0 -1
- package/lib/decorators/inject.js +0 -60
- package/lib/decorators/inject.js.map +0 -1
- package/lib/decorators/injectable.js +0 -33
- package/lib/decorators/injectable.js.map +0 -1
- package/lib/decorators/intercept.js +0 -28
- package/lib/decorators/intercept.js.map +0 -1
- package/lib/decorators/interceptor.js +0 -21
- package/lib/decorators/interceptor.js.map +0 -1
- package/lib/decorators/module.js +0 -31
- package/lib/decorators/module.js.map +0 -1
- package/lib/decorators/opts.js +0 -54
- package/lib/decorators/opts.js.map +0 -1
- package/lib/decorators/overrideProvider.js +0 -17
- package/lib/decorators/overrideProvider.js.map +0 -1
- package/lib/decorators/scope.js +0 -16
- package/lib/decorators/scope.js.map +0 -1
- package/lib/decorators/service.js +0 -18
- package/lib/decorators/service.js.map +0 -1
- package/lib/decorators/useOpts.js +0 -65
- package/lib/decorators/useOpts.js.map +0 -1
- package/lib/decorators/value.js +0 -55
- package/lib/decorators/value.js.map +0 -1
- package/lib/domain/Container.js +0 -69
- package/lib/domain/Container.js.map +0 -1
- package/lib/domain/ContextLogger.js +0 -92
- package/lib/domain/ContextLogger.js.map +0 -1
- package/lib/domain/DIContext.js +0 -45
- package/lib/domain/DIContext.js.map +0 -1
- package/lib/domain/InjectablePropertyType.js +0 -12
- package/lib/domain/InjectablePropertyType.js.map +0 -1
- package/lib/domain/LocalsContainer.js +0 -55
- package/lib/domain/LocalsContainer.js.map +0 -1
- package/lib/domain/Provider.js +0 -173
- package/lib/domain/Provider.js.map +0 -1
- package/lib/domain/ProviderScope.js +0 -10
- package/lib/domain/ProviderScope.js.map +0 -1
- package/lib/domain/ProviderType.js +0 -15
- package/lib/domain/ProviderType.js.map +0 -1
- package/lib/domain/index.js +0 -12
- package/lib/domain/index.js.map +0 -1
- package/lib/errors/InjectionError.js +0 -59
- package/lib/errors/InjectionError.js.map +0 -1
- package/lib/errors/UndefinedTokenError.js +0 -11
- package/lib/errors/UndefinedTokenError.js.map +0 -1
- package/lib/interfaces/DIConfigurationOptions.js +0 -3
- package/lib/interfaces/DIConfigurationOptions.js.map +0 -1
- package/lib/interfaces/DILogger.js +0 -3
- package/lib/interfaces/DILogger.js.map +0 -1
- package/lib/interfaces/DIResolver.js +0 -3
- package/lib/interfaces/DIResolver.js.map +0 -1
- package/lib/interfaces/IProvider.js +0 -3
- package/lib/interfaces/IProvider.js.map +0 -1
- package/lib/interfaces/InjectableProperties.js +0 -3
- package/lib/interfaces/InjectableProperties.js.map +0 -1
- package/lib/interfaces/InterceptorContext.js +0 -3
- package/lib/interfaces/InterceptorContext.js.map +0 -1
- package/lib/interfaces/InterceptorMethods.js +0 -3
- package/lib/interfaces/InterceptorMethods.js.map +0 -1
- package/lib/interfaces/InvokeOptions.js +0 -3
- package/lib/interfaces/InvokeOptions.js.map +0 -1
- package/lib/interfaces/OnDestroy.js +0 -3
- package/lib/interfaces/OnDestroy.js.map +0 -1
- package/lib/interfaces/OnInit.js +0 -3
- package/lib/interfaces/OnInit.js.map +0 -1
- package/lib/interfaces/RegistrySettings.js +0 -3
- package/lib/interfaces/RegistrySettings.js.map +0 -1
- package/lib/interfaces/TokenProvider.js +0 -3
- package/lib/interfaces/TokenProvider.js.map +0 -1
- package/lib/interfaces/index.js +0 -17
- package/lib/interfaces/index.js.map +0 -1
- package/lib/registries/GlobalProviders.js +0 -124
- package/lib/registries/GlobalProviders.js.map +0 -1
- package/lib/registries/ProviderRegistry.js +0 -181
- package/lib/registries/ProviderRegistry.js.map +0 -1
- package/lib/services/DIConfiguration.js +0 -129
- package/lib/services/DIConfiguration.js.map +0 -1
- package/lib/services/DILogger.js +0 -13
- package/lib/services/DILogger.js.map +0 -1
- package/lib/services/DITest.js +0 -100
- package/lib/services/DITest.js.map +0 -1
- package/lib/services/InjectorService.js +0 -543
- package/lib/services/InjectorService.js.map +0 -1
- package/lib/utils/cleanGlobPatterns.js +0 -30
- package/lib/utils/cleanGlobPatterns.js.map +0 -1
- package/lib/utils/createContainer.js +0 -14
- package/lib/utils/createContainer.js.map +0 -1
- package/lib/utils/data/Test1.d.ts +0 -3
- package/lib/utils/data/Test1.js +0 -8
- package/lib/utils/data/Test1.js.map +0 -1
- package/lib/utils/data/Test2.d.ts +0 -3
- package/lib/utils/data/Test2.js +0 -8
- package/lib/utils/data/Test2.js.map +0 -1
- package/lib/utils/getConfiguration.js +0 -20
- package/lib/utils/getConfiguration.js.map +0 -1
- package/lib/utils/importComponents.js +0 -50
- package/lib/utils/importComponents.js.map +0 -1
- package/lib/utils/importFiles.js +0 -28
- package/lib/utils/importFiles.js.map +0 -1
- package/lib/utils/importProviders.js +0 -29
- package/lib/utils/importProviders.js.map +0 -1
- package/lib/utils/setLoggerLevel.js +0 -16
- package/lib/utils/setLoggerLevel.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalProviders.js","sourceRoot":"","sources":["../../src/registries/GlobalProviders.ts"],"names":[],"mappings":";;;;;AAAA,qCAAkD;AAElD,sCAAiD;AAEjD,MAAa,sBAAuB,SAAQ,GAA4B;IAAxE;;QACE,oBAA2C,IAAI,GAAG,EAAE,EAAC;IA+HvD,CAAC;IA7HC;;;;OAIG;IACH,GAAG,CAAC,GAAkB;QACpB,OAAO,KAAK,CAAC,GAAG,CAAC,uBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC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AAI,CAAC,CAAC;SACrB;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;IACxB,CAAC;CACF;AAhID,wDAgIC;;AAED;;;GAGG;AACH,0CAA0C;AAC7B,QAAA,eAAe,GAAG,IAAI,sBAAsB,EAAE,CAAC","sourcesContent":["import {getClassOrSymbol, Type} from \"@tsed/core\";\nimport type {IProvider, RegistrySettings, TokenProvider} from \"../interfaces\";\nimport {Provider, ProviderType} from \"../domain\";\n\nexport class GlobalProviderRegistry extends Map<TokenProvider, Provider> {\n #settings: Map<string, RegistrySettings> = new Map();\n\n /**\n * The get() method returns a specified element from a Map object.\n * @param key Required. The key of the element to return from the Map object.\n * @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.\n */\n get(key: TokenProvider): Provider | undefined {\n return super.get(getClassOrSymbol(key));\n }\n\n /**\n * The has() method returns a boolean indicating whether an element with the specified key exists or not.\n * @param key\n * @returns {boolean}\n */\n has(key: TokenProvider): boolean {\n return super.has(getClassOrSymbol(key));\n }\n\n /**\n * The set() method adds or updates an element with a specified key and value to a Map object.\n * @param key Required. The key of the element to add to the Map object.\n * @param metadata Required. The value of the element to add to the Map object.\n */\n set(key: TokenProvider, metadata: Provider): this {\n super.set(getClassOrSymbol(key), metadata);\n\n return this;\n }\n\n /**\n *\n * @param target\n * @param options\n */\n merge(target: TokenProvider, options: Partial<IProvider>): void {\n const meta = this.createIfNotExists(target, options);\n\n Object.keys(options).forEach((key) => {\n meta[key] = (options as any)[key];\n });\n\n this.set(target, meta);\n }\n\n /**\n * The delete() method removes the specified element from a Map object.\n * @param key Required. The key of the element to remove from the Map object.\n * @returns {boolean} Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.\n */\n delete(key: TokenProvider): boolean {\n return super.delete(getClassOrSymbol(key));\n }\n\n createRegistry(type: string, model: Type<Provider>, options: Partial<RegistrySettings> = {injectable: true}) {\n const defaultOptions = this.getRegistrySettings(type);\n\n options = Object.assign(defaultOptions, {\n ...options,\n model\n });\n\n this.#settings.set(type, options);\n\n return this;\n }\n\n getRegistrySettings(target: string | TokenProvider): RegistrySettings {\n let type: string = \"provider\";\n\n if (typeof target === \"string\") {\n type = target;\n } else {\n const provider = this.get(target);\n if (provider) {\n type = provider.type;\n }\n }\n\n return (\n this.#settings.get(type) || {\n model: Provider,\n injectable: true\n }\n );\n }\n\n createRegisterFn(type: string) {\n return (provider: any | IProvider, instance?: any): void => {\n if (!provider.provide) {\n provider = {\n provide: provider\n };\n }\n\n provider = Object.assign({instance}, provider, {type});\n this.merge(provider.provide, provider);\n };\n }\n\n /**\n * @deprecated\n */\n // istanbul ignore next\n getRegistry(target: string | TokenProvider) {\n return this;\n }\n\n /**\n *\n * @param key\n * @param options\n */\n protected createIfNotExists(key: TokenProvider, options: Partial<IProvider>): Provider {\n const type = options.type || ProviderType.PROVIDER;\n\n if (!this.has(key)) {\n const {model = Provider} = this.#settings.get(type) || {};\n\n const item = new model(key);\n\n this.set(key, item);\n }\n\n return this.get(key)!;\n }\n}\n\n/**\n *\n * @type {GlobalProviders}\n */\n// tslint:disable-next-line: variable-name\nexport const GlobalProviders = new GlobalProviderRegistry();\n"]}
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.registerController = exports.registerService = exports.registerValue = exports.registerFactory = exports.registerProvider = void 0;
|
|
4
|
-
const Provider_1 = require("../domain/Provider");
|
|
5
|
-
const domain_1 = require("../domain");
|
|
6
|
-
const GlobalProviders_1 = require("./GlobalProviders");
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
*/
|
|
10
|
-
GlobalProviders_1.GlobalProviders.createRegistry(domain_1.ProviderType.CONTROLLER, Provider_1.Provider, {
|
|
11
|
-
injectable: false
|
|
12
|
-
});
|
|
13
|
-
/**
|
|
14
|
-
* Register a provider configuration.
|
|
15
|
-
* @param {IProvider<any>} provider
|
|
16
|
-
*/
|
|
17
|
-
function registerProvider(provider) {
|
|
18
|
-
if (!provider.provide) {
|
|
19
|
-
throw new Error("Provider.provide is required");
|
|
20
|
-
}
|
|
21
|
-
GlobalProviders_1.GlobalProviders.merge(provider.provide, provider);
|
|
22
|
-
}
|
|
23
|
-
exports.registerProvider = registerProvider;
|
|
24
|
-
/**
|
|
25
|
-
* Add a new factory in the `ProviderRegistry`.
|
|
26
|
-
*
|
|
27
|
-
* #### Example with symbol definition
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* ```typescript
|
|
31
|
-
* import {registerFactory, InjectorService} from "@tsed/di";
|
|
32
|
-
*
|
|
33
|
-
* export interface IMyFooFactory {
|
|
34
|
-
* getFoo(): string;
|
|
35
|
-
* }
|
|
36
|
-
*
|
|
37
|
-
* export type MyFooFactory = IMyFooFactory;
|
|
38
|
-
* export const MyFooFactory = Symbol("MyFooFactory");
|
|
39
|
-
*
|
|
40
|
-
* registerFactory(MyFooFactory, {
|
|
41
|
-
* getFoo: () => "test"
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* // or
|
|
45
|
-
*
|
|
46
|
-
* registerFactory({provide: MyFooFactory, instance: {
|
|
47
|
-
* getFoo: () => "test"
|
|
48
|
-
* }});
|
|
49
|
-
*
|
|
50
|
-
* @Service()
|
|
51
|
-
* export class OtherService {
|
|
52
|
-
* constructor(@Inject(MyFooFactory) myFooFactory: MyFooFactory){
|
|
53
|
-
* console.log(myFooFactory.getFoo()); /// "test"
|
|
54
|
-
* }
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* > Note: When you use the factory method with Symbol definition, you must use the `@Inject()`
|
|
59
|
-
* decorator to retrieve your factory in another Service. Advice: By convention all factory class name will be prefixed by
|
|
60
|
-
* `Factory`.
|
|
61
|
-
*
|
|
62
|
-
* #### Example with class
|
|
63
|
-
*
|
|
64
|
-
* ```typescript
|
|
65
|
-
* import {InjectorService, registerFactory} from "@tsed/di";
|
|
66
|
-
*
|
|
67
|
-
* export class MyFooService {
|
|
68
|
-
* constructor(){}
|
|
69
|
-
* getFoo() {
|
|
70
|
-
* return "test";
|
|
71
|
-
* }
|
|
72
|
-
* }
|
|
73
|
-
*
|
|
74
|
-
* registerFactory(MyFooService, new MyFooService());
|
|
75
|
-
* // or
|
|
76
|
-
* registerFactory({provider: MyFooService, instance: new MyFooService()});
|
|
77
|
-
*
|
|
78
|
-
* @Service()
|
|
79
|
-
* export class OtherService {
|
|
80
|
-
* constructor(myFooService: MyFooService){
|
|
81
|
-
* console.log(myFooFactory.getFoo()); /// "test"
|
|
82
|
-
* }
|
|
83
|
-
* }
|
|
84
|
-
* ```
|
|
85
|
-
* @deprecated Since 2021-05-15. Use registerProvider instead.
|
|
86
|
-
*/
|
|
87
|
-
exports.registerFactory = registerProvider;
|
|
88
|
-
/**
|
|
89
|
-
* Add a new value in the `ProviderRegistry`.
|
|
90
|
-
*
|
|
91
|
-
* #### Example with symbol definition
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* ```typescript
|
|
95
|
-
* import {registerValue, InjectorService} from "@tsed/di";
|
|
96
|
-
*
|
|
97
|
-
* const MyValue = Symbol.from("MyValue")
|
|
98
|
-
*
|
|
99
|
-
* registerValue({token: MyValue, useValue: "myValue"});
|
|
100
|
-
*
|
|
101
|
-
* @Service()
|
|
102
|
-
* export class OtherService {
|
|
103
|
-
* constructor(@Inject(MyValue) myValue: string){
|
|
104
|
-
* console.log(myValue); /// "myValue"
|
|
105
|
-
* }
|
|
106
|
-
* }
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
const registerValue = (provider, value) => {
|
|
110
|
-
if (!provider.provide) {
|
|
111
|
-
provider = {
|
|
112
|
-
provide: provider
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
provider = Object.assign({
|
|
116
|
-
scope: domain_1.ProviderScope.SINGLETON,
|
|
117
|
-
useValue: value
|
|
118
|
-
}, provider, { type: domain_1.ProviderType.VALUE });
|
|
119
|
-
GlobalProviders_1.GlobalProviders.merge(provider.provide, provider);
|
|
120
|
-
};
|
|
121
|
-
exports.registerValue = registerValue;
|
|
122
|
-
/**
|
|
123
|
-
* Add a new service in the `ProviderRegistry`. This service will be built when `InjectorService` will be loaded.
|
|
124
|
-
*
|
|
125
|
-
* #### Example
|
|
126
|
-
*
|
|
127
|
-
* ```typescript
|
|
128
|
-
* import {registerService, InjectorService} from "@tsed/di";
|
|
129
|
-
*
|
|
130
|
-
* export default class MyFooService {
|
|
131
|
-
* constructor(){}
|
|
132
|
-
* getFoo() {
|
|
133
|
-
* return "test";
|
|
134
|
-
* }
|
|
135
|
-
* }
|
|
136
|
-
*
|
|
137
|
-
* registerService({provide: MyFooService});
|
|
138
|
-
* // or
|
|
139
|
-
* registerService(MyFooService);
|
|
140
|
-
*
|
|
141
|
-
* const injector = new InjectorService();
|
|
142
|
-
* injector.load();
|
|
143
|
-
*
|
|
144
|
-
* const myFooService = injector.get<MyFooService>(MyFooService);
|
|
145
|
-
* myFooService.getFoo(); // test
|
|
146
|
-
* ```
|
|
147
|
-
*
|
|
148
|
-
* @param provider Provider configuration.
|
|
149
|
-
* @deprecated Since 2021-05-15. Use registerProvider instead.
|
|
150
|
-
*/
|
|
151
|
-
exports.registerService = GlobalProviders_1.GlobalProviders.createRegisterFn(domain_1.ProviderType.SERVICE);
|
|
152
|
-
/**
|
|
153
|
-
* Add a new controller in the `ProviderRegistry`. This controller will be built when `InjectorService` will be loaded.
|
|
154
|
-
*
|
|
155
|
-
* #### Example
|
|
156
|
-
*
|
|
157
|
-
* ```typescript
|
|
158
|
-
* import {registerController, InjectorService} from "@tsed/di";
|
|
159
|
-
*
|
|
160
|
-
* export default class MyController {
|
|
161
|
-
* constructor(){}
|
|
162
|
-
* transform() {
|
|
163
|
-
* return "test";
|
|
164
|
-
* }
|
|
165
|
-
* }
|
|
166
|
-
*
|
|
167
|
-
* registerController({provide: MyController});
|
|
168
|
-
* // or
|
|
169
|
-
* registerController(MyController);
|
|
170
|
-
*
|
|
171
|
-
* const injector = new InjectorService();
|
|
172
|
-
* injector.load();
|
|
173
|
-
*
|
|
174
|
-
* const myController = injector.get<MyController>(MyController);
|
|
175
|
-
* myController.getFoo(); // test
|
|
176
|
-
* ```
|
|
177
|
-
*
|
|
178
|
-
* @param provider Provider configuration.
|
|
179
|
-
*/
|
|
180
|
-
exports.registerController = GlobalProviders_1.GlobalProviders.createRegisterFn(domain_1.ProviderType.CONTROLLER);
|
|
181
|
-
//# sourceMappingURL=ProviderRegistry.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ProviderRegistry.js","sourceRoot":"","sources":["../../src/registries/ProviderRegistry.ts"],"names":[],"mappings":";;;AAAA,iDAA4C;AAE5C,sCAAsD;AACtD,uDAAkD;AAElD;;GAEG;AACH,iCAAe,CAAC,cAAc,CAAC,qBAAY,CAAC,UAAU,EAAE,mBAAQ,EAAE;IAChE,UAAU,EAAE,KAAK;CAClB,CAAC,CAAC;AAEH;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,QAAiC;IAChE,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE;QACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,iCAAe,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACpD,CAAC;AAND,4CAMC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACU,QAAA,eAAe,GAAG,gBAAgB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;GAoBG;AACI,MAAM,aAAa,GAAG,CAAC,QAA8B,EAAE,KAAW,EAAQ,EAAE;IACjF,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE;QACrB,QAAQ,GAAG;YACT,OAAO,EAAE,QAAQ;SAClB,CAAC;KACH;IAED,QAAQ,GAAG,MAAM,CAAC,MAAM,CACtB;QACE,KAAK,EAAE,sBAAa,CAAC,SAAS;QAC9B,QAAQ,EAAE,KAAK;KAChB,EACD,QAAQ,EACR,EAAC,IAAI,EAAE,qBAAY,CAAC,KAAK,EAAC,CAC3B,CAAC;IACF,iCAAe,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACpD,CAAC,CAAC;AAhBW,QAAA,aAAa,iBAgBxB;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACU,QAAA,eAAe,GAAG,iCAAe,CAAC,gBAAgB,CAAC,qBAAY,CAAC,OAAO,CAAC,CAAC;AACtF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACU,QAAA,kBAAkB,GAAG,iCAAe,CAAC,gBAAgB,CAAC,qBAAY,CAAC,UAAU,CAAC,CAAC","sourcesContent":["import {Provider} from \"../domain/Provider\";\nimport type {IProvider} from \"../interfaces\";\nimport {ProviderScope, ProviderType} from \"../domain\";\nimport {GlobalProviders} from \"./GlobalProviders\";\n\n/**\n *\n */\nGlobalProviders.createRegistry(ProviderType.CONTROLLER, Provider, {\n injectable: false\n});\n\n/**\n * Register a provider configuration.\n * @param {IProvider<any>} provider\n */\nexport function registerProvider(provider: Partial<IProvider<any>>): void {\n if (!provider.provide) {\n throw new Error(\"Provider.provide is required\");\n }\n\n GlobalProviders.merge(provider.provide, provider);\n}\n\n/**\n * Add a new factory in the `ProviderRegistry`.\n *\n * #### Example with symbol definition\n *\n *\n * ```typescript\n * import {registerFactory, InjectorService} from \"@tsed/di\";\n *\n * export interface IMyFooFactory {\n * getFoo(): string;\n * }\n *\n * export type MyFooFactory = IMyFooFactory;\n * export const MyFooFactory = Symbol(\"MyFooFactory\");\n *\n * registerFactory(MyFooFactory, {\n * getFoo: () => \"test\"\n * });\n *\n * // or\n *\n * registerFactory({provide: MyFooFactory, instance: {\n * getFoo: () => \"test\"\n * }});\n *\n * @Service()\n * export class OtherService {\n * constructor(@Inject(MyFooFactory) myFooFactory: MyFooFactory){\n * console.log(myFooFactory.getFoo()); /// \"test\"\n * }\n * }\n * ```\n *\n * > Note: When you use the factory method with Symbol definition, you must use the `@Inject()`\n * decorator to retrieve your factory in another Service. Advice: By convention all factory class name will be prefixed by\n * `Factory`.\n *\n * #### Example with class\n *\n * ```typescript\n * import {InjectorService, registerFactory} from \"@tsed/di\";\n *\n * export class MyFooService {\n * constructor(){}\n * getFoo() {\n * return \"test\";\n * }\n * }\n *\n * registerFactory(MyFooService, new MyFooService());\n * // or\n * registerFactory({provider: MyFooService, instance: new MyFooService()});\n *\n * @Service()\n * export class OtherService {\n * constructor(myFooService: MyFooService){\n * console.log(myFooFactory.getFoo()); /// \"test\"\n * }\n * }\n * ```\n * @deprecated Since 2021-05-15. Use registerProvider instead.\n */\nexport const registerFactory = registerProvider;\n\n/**\n * Add a new value in the `ProviderRegistry`.\n *\n * #### Example with symbol definition\n *\n *\n * ```typescript\n * import {registerValue, InjectorService} from \"@tsed/di\";\n *\n * const MyValue = Symbol.from(\"MyValue\")\n *\n * registerValue({token: MyValue, useValue: \"myValue\"});\n *\n * @Service()\n * export class OtherService {\n * constructor(@Inject(MyValue) myValue: string){\n * console.log(myValue); /// \"myValue\"\n * }\n * }\n * ```\n */\nexport const registerValue = (provider: any | IProvider<any>, value?: any): void => {\n if (!provider.provide) {\n provider = {\n provide: provider\n };\n }\n\n provider = Object.assign(\n {\n scope: ProviderScope.SINGLETON,\n useValue: value\n },\n provider,\n {type: ProviderType.VALUE}\n );\n GlobalProviders.merge(provider.provide, provider);\n};\n\n/**\n * Add a new service in the `ProviderRegistry`. This service will be built when `InjectorService` will be loaded.\n *\n * #### Example\n *\n * ```typescript\n * import {registerService, InjectorService} from \"@tsed/di\";\n *\n * export default class MyFooService {\n * constructor(){}\n * getFoo() {\n * return \"test\";\n * }\n * }\n *\n * registerService({provide: MyFooService});\n * // or\n * registerService(MyFooService);\n *\n * const injector = new InjectorService();\n * injector.load();\n *\n * const myFooService = injector.get<MyFooService>(MyFooService);\n * myFooService.getFoo(); // test\n * ```\n *\n * @param provider Provider configuration.\n * @deprecated Since 2021-05-15. Use registerProvider instead.\n */\nexport const registerService = GlobalProviders.createRegisterFn(ProviderType.SERVICE);\n/**\n * Add a new controller in the `ProviderRegistry`. This controller will be built when `InjectorService` will be loaded.\n *\n * #### Example\n *\n * ```typescript\n * import {registerController, InjectorService} from \"@tsed/di\";\n *\n * export default class MyController {\n * constructor(){}\n * transform() {\n * return \"test\";\n * }\n * }\n *\n * registerController({provide: MyController});\n * // or\n * registerController(MyController);\n *\n * const injector = new InjectorService();\n * injector.load();\n *\n * const myController = injector.get<MyController>(MyController);\n * myController.getFoo(); // test\n * ```\n *\n * @param provider Provider configuration.\n */\nexport const registerController = GlobalProviders.createRegisterFn(ProviderType.CONTROLLER);\n"]}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DIConfiguration = void 0;
|
|
4
|
-
const core_1 = require("@tsed/core");
|
|
5
|
-
class DIConfiguration {
|
|
6
|
-
constructor(initialProps = {}) {
|
|
7
|
-
this.default = new Map();
|
|
8
|
-
this.map = new Map();
|
|
9
|
-
Object.entries({
|
|
10
|
-
scopes: {},
|
|
11
|
-
resolvers: [],
|
|
12
|
-
imports: [],
|
|
13
|
-
...initialProps
|
|
14
|
-
}).forEach(([key, value]) => {
|
|
15
|
-
this.default.set(key, value);
|
|
16
|
-
});
|
|
17
|
-
return core_1.proxyDelegation(this, {
|
|
18
|
-
ownKeys(target) {
|
|
19
|
-
return [...target.default.keys(), ...target.map.keys()];
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
get scopes() {
|
|
24
|
-
return this.getRaw("scopes");
|
|
25
|
-
}
|
|
26
|
-
set scopes(value) {
|
|
27
|
-
this.setRaw("scopes", value);
|
|
28
|
-
}
|
|
29
|
-
get resolvers() {
|
|
30
|
-
return this.getRaw("resolvers");
|
|
31
|
-
}
|
|
32
|
-
set resolvers(resolvers) {
|
|
33
|
-
this.setRaw("resolvers", resolvers);
|
|
34
|
-
}
|
|
35
|
-
get imports() {
|
|
36
|
-
return this.getRaw("imports");
|
|
37
|
-
}
|
|
38
|
-
set imports(imports) {
|
|
39
|
-
this.setRaw("imports", imports);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
*
|
|
43
|
-
* @param callbackfn
|
|
44
|
-
* @param thisArg
|
|
45
|
-
*/
|
|
46
|
-
forEach(callbackfn, thisArg) {
|
|
47
|
-
return new Set([...Array.from(this.default.keys()), ...Array.from(this.map.keys())]).forEach((key) => {
|
|
48
|
-
callbackfn(this.getRaw(key), key, this.map);
|
|
49
|
-
}, thisArg);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
*
|
|
53
|
-
* @param propertyKey
|
|
54
|
-
* @param value
|
|
55
|
-
*/
|
|
56
|
-
set(propertyKey, value) {
|
|
57
|
-
if (typeof propertyKey === "string") {
|
|
58
|
-
this.setRaw(propertyKey, value);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
Object.entries(propertyKey).forEach(([key, value]) => {
|
|
62
|
-
this[key] = value;
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
setRaw(propertyKey, value) {
|
|
68
|
-
core_1.setValue(this.map, propertyKey, value);
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
*
|
|
73
|
-
* @param propertyKey
|
|
74
|
-
* @param defaultValue
|
|
75
|
-
* @returns {undefined|any}
|
|
76
|
-
*/
|
|
77
|
-
get(propertyKey, defaultValue) {
|
|
78
|
-
return this.resolve(this.getRaw(propertyKey, defaultValue));
|
|
79
|
-
}
|
|
80
|
-
getRaw(propertyKey, defaultValue) {
|
|
81
|
-
const value = core_1.getValue(this.map, propertyKey);
|
|
82
|
-
if (value !== undefined) {
|
|
83
|
-
return value;
|
|
84
|
-
}
|
|
85
|
-
return core_1.getValue(this.default, propertyKey, defaultValue);
|
|
86
|
-
}
|
|
87
|
-
merge(obj) {
|
|
88
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
89
|
-
const descriptor = Object.getOwnPropertyDescriptor(DIConfiguration.prototype, key);
|
|
90
|
-
const originalValue = this.get(key);
|
|
91
|
-
value = core_1.deepMerge(value, originalValue);
|
|
92
|
-
if (descriptor && !["default", "set", "map", "get"].includes(key)) {
|
|
93
|
-
this[key] = value;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
*
|
|
99
|
-
* @param value
|
|
100
|
-
* @returns {any}
|
|
101
|
-
*/
|
|
102
|
-
resolve(value) {
|
|
103
|
-
if (typeof value === "object" && value !== null) {
|
|
104
|
-
if (![Array, Object].includes(core_1.classOf(value))) {
|
|
105
|
-
return value;
|
|
106
|
-
}
|
|
107
|
-
return Object.entries(value).reduce((o, [k, v]) => {
|
|
108
|
-
// @ts-ignore
|
|
109
|
-
o[k] = this.resolve(v);
|
|
110
|
-
return o;
|
|
111
|
-
}, Array.isArray(value) ? [] : {});
|
|
112
|
-
}
|
|
113
|
-
if (typeof value === "string") {
|
|
114
|
-
const replacer = (match, key) => core_1.getValue(this.map, key);
|
|
115
|
-
return value
|
|
116
|
-
.replace(/\${([\w.]+)}/gi, replacer)
|
|
117
|
-
.replace(/<([\w.]+)>/gi, replacer)
|
|
118
|
-
.replace(/{{([\w.]+)}}/gi, replacer);
|
|
119
|
-
}
|
|
120
|
-
return value;
|
|
121
|
-
}
|
|
122
|
-
build() {
|
|
123
|
-
this.forEach((value, key) => this.map.set(key, this.resolve(value)));
|
|
124
|
-
this.set = this.setRaw;
|
|
125
|
-
this.get = this.getRaw = (propertyKey, defaultValue) => core_1.getValue(this.map, propertyKey, defaultValue);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
exports.DIConfiguration = DIConfiguration;
|
|
129
|
-
//# sourceMappingURL=DIConfiguration.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../src/services/DIConfiguration.ts"],"names":[],"mappings":";;;AAAA,qCAAmF;AAInF,MAAa,eAAe;IAM1B,YAAY,YAAY,GAAG,EAAE;QALpB,YAAO,GAAqB,IAAI,GAAG,EAAE,CAAC;QACrC,QAAG,GAAqB,IAAI,GAAG,EAAE,CAAC;QAK1C,MAAM,CAAC,OAAO,CAAC;YACb,MAAM,EAAE,EAAE;YACV,SAAS,EAAE,EAAE;YACb,OAAO,EAAE,EAAE;YACX,GAAG,YAAY;SAChB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YAC1B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,sBAAe,CAAkB,IAAI,EAAE;YAC5C,OAAO,CAAC,MAAM;gBACZ,OAAO,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;YAC1D,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI,MAAM,CAAC,KAAqC;QAC9C,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,SAAS,CAAC,SAAuB;QACnC,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;IACtC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,I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AC,EAAE,CAAC,CAAC,EAAE,EAAE;gBACZ,aAAa;gBACb,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;gBAEvB,OAAO,CAAC,CAAC;YACX,CAAC,EACD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAC/B,CAAC;SACH;QAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YAC7B,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,GAAW,EAAE,EAAE,CAAC,eAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;YACzE,OAAO,KAAK;iBACT,OAAO,CAAC,gBAAgB,EAAE,QAAQ,CAAC;iBACnC,OAAO,CAAC,cAAc,EAAE,QAAQ,CAAC;iBACjC,OAAO,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC;SACxC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK;QACH,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAErE,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,WAAmB,EAAE,YAAkB,EAAE,EAAE,CAAC,eAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IACtH,CAAC;CACF;AAxJD,0CAwJC","sourcesContent":["import {classOf, deepMerge, getValue, proxyDelegation, setValue} from \"@tsed/core\";\nimport {ProviderScope} from \"../domain\";\nimport type {DIResolver, TokenProvider} from \"../interfaces\";\n\nexport class DIConfiguration {\n readonly default: Map<string, any> = new Map();\n protected map: Map<string, any> = new Map();\n\n [key: string]: any;\n\n constructor(initialProps = {}) {\n Object.entries({\n scopes: {},\n resolvers: [],\n imports: [],\n ...initialProps\n }).forEach(([key, value]) => {\n this.default.set(key, value);\n });\n\n return proxyDelegation<DIConfiguration>(this, {\n ownKeys(target) {\n return [...target.default.keys(), ...target.map.keys()];\n }\n });\n }\n\n get scopes(): {[key: string]: ProviderScope} {\n return this.getRaw(\"scopes\");\n }\n\n set scopes(value: {[key: string]: ProviderScope}) {\n this.setRaw(\"scopes\", value);\n }\n\n get resolvers(): DIResolver[] {\n return this.getRaw(\"resolvers\");\n }\n\n set resolvers(resolvers: DIResolver[]) {\n this.setRaw(\"resolvers\", resolvers);\n }\n\n get imports(): TokenProvider[] {\n return this.getRaw(\"imports\");\n }\n\n set imports(imports: TokenProvider[]) {\n this.setRaw(\"imports\", imports);\n }\n\n /**\n *\n * @param callbackfn\n * @param thisArg\n */\n forEach(callbackfn: (value: any, index: string, map: Map<string, any>) => void, thisArg?: any) {\n return new Set([...Array.from(this.default.keys()), ...Array.from(this.map.keys())]).forEach((key) => {\n callbackfn(this.getRaw(key), key, this.map);\n }, thisArg);\n }\n\n /**\n *\n * @param propertyKey\n * @param value\n */\n set(propertyKey: string | Partial<TsED.Configuration>, value?: any): this {\n if (typeof propertyKey === \"string\") {\n this.setRaw(propertyKey, value);\n } else {\n Object.entries(propertyKey).forEach(([key, value]) => {\n this[key] = value;\n });\n }\n\n return this;\n }\n\n setRaw(propertyKey: string, value: any) {\n setValue(this.map, propertyKey, value);\n\n return this;\n }\n\n /**\n *\n * @param propertyKey\n * @param defaultValue\n * @returns {undefined|any}\n */\n get<T = any>(propertyKey: string, defaultValue?: T): T {\n return this.resolve(this.getRaw(propertyKey, defaultValue));\n }\n\n getRaw(propertyKey: string, defaultValue?: any): any {\n const value = getValue(this.map, propertyKey);\n\n if (value !== undefined) {\n return value;\n }\n\n return getValue(this.default, propertyKey, defaultValue);\n }\n\n merge(obj: Partial<TsED.Configuration>) {\n Object.entries(obj).forEach(([key, value]) => {\n const descriptor = Object.getOwnPropertyDescriptor(DIConfiguration.prototype, key);\n const originalValue = this.get(key);\n value = deepMerge(value, originalValue);\n\n if (descriptor && ![\"default\", \"set\", \"map\", \"get\"].includes(key)) {\n this[key] = value;\n }\n });\n }\n\n /**\n *\n * @param value\n * @returns {any}\n */\n resolve(value: any) {\n if (typeof value === \"object\" && value !== null) {\n if (![Array, Object].includes(classOf(value))) {\n return value;\n }\n\n return Object.entries(value).reduce(\n (o, [k, v]) => {\n // @ts-ignore\n o[k] = this.resolve(v);\n\n return o;\n },\n Array.isArray(value) ? [] : {}\n );\n }\n\n if (typeof value === \"string\") {\n const replacer = (match: string, key: string) => getValue(this.map, key);\n return value\n .replace(/\\${([\\w.]+)}/gi, replacer)\n .replace(/<([\\w.]+)>/gi, replacer)\n .replace(/{{([\\w.]+)}}/gi, replacer);\n }\n\n return value;\n }\n\n build() {\n this.forEach((value, key) => this.map.set(key, this.resolve(value)));\n\n this.set = this.setRaw;\n this.get = this.getRaw = (propertyKey: string, defaultValue?: any) => getValue(this.map, propertyKey, defaultValue);\n }\n}\n"]}
|
package/lib/services/DILogger.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const logger_1 = require("@tsed/logger");
|
|
4
|
-
const ProviderRegistry_1 = require("../registries/ProviderRegistry");
|
|
5
|
-
const InjectorService_1 = require("./InjectorService");
|
|
6
|
-
ProviderRegistry_1.registerProvider({
|
|
7
|
-
provide: logger_1.Logger,
|
|
8
|
-
deps: [InjectorService_1.InjectorService],
|
|
9
|
-
useFactory(injector) {
|
|
10
|
-
return injector.logger;
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
//# sourceMappingURL=DILogger.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../src/services/DILogger.ts"],"names":[],"mappings":";;AAAA,yCAAoC;AACpC,qEAAgE;AAChE,uDAAkD;AAElD,mCAAgB,CAAC;IACf,OAAO,EAAE,eAAM;IACf,IAAI,EAAE,CAAC,iCAAe,CAAC;IACvB,UAAU,CAAC,QAAyB;QAClC,OAAO,QAAQ,CAAC,MAAM,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Logger} from \"@tsed/logger\";\nimport {registerProvider} from \"../registries/ProviderRegistry\";\nimport {InjectorService} from \"./InjectorService\";\n\nregisterProvider({\n provide: Logger,\n deps: [InjectorService],\n useFactory(injector: InjectorService) {\n return injector.logger;\n }\n});\n"]}
|
package/lib/services/DITest.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DITest = void 0;
|
|
4
|
-
const core_1 = require("@tsed/core");
|
|
5
|
-
const logger_1 = require("@tsed/logger");
|
|
6
|
-
const Container_1 = require("../domain/Container");
|
|
7
|
-
const LocalsContainer_1 = require("../domain/LocalsContainer");
|
|
8
|
-
const setLoggerLevel_1 = require("../utils/setLoggerLevel");
|
|
9
|
-
const InjectorService_1 = require("./InjectorService");
|
|
10
|
-
/**
|
|
11
|
-
* Tool to run test with lightweight DI sandbox.
|
|
12
|
-
*/
|
|
13
|
-
class DITest {
|
|
14
|
-
static get injector() {
|
|
15
|
-
if (DITest._injector) {
|
|
16
|
-
return DITest._injector;
|
|
17
|
-
}
|
|
18
|
-
/* istanbul ignore next */
|
|
19
|
-
throw new Error("PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\n" +
|
|
20
|
-
"Example:\n" +
|
|
21
|
-
"before(async () => {\n" +
|
|
22
|
-
" await PlatformTest.create()\n" +
|
|
23
|
-
" await PlatformTest.invoke(MyService, [])\n" +
|
|
24
|
-
"})");
|
|
25
|
-
}
|
|
26
|
-
static set injector(injector) {
|
|
27
|
-
DITest._injector = injector;
|
|
28
|
-
}
|
|
29
|
-
static hasInjector() {
|
|
30
|
-
return !!DITest._injector;
|
|
31
|
-
}
|
|
32
|
-
static async create(settings = {}) {
|
|
33
|
-
DITest.injector = DITest.createInjector(settings);
|
|
34
|
-
const container = new Container_1.Container();
|
|
35
|
-
DITest.injector.bootstrap(container);
|
|
36
|
-
await DITest.injector.load(container);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Create a new injector with the right default services
|
|
40
|
-
*/
|
|
41
|
-
static createInjector(settings = {}) {
|
|
42
|
-
const injector = new InjectorService_1.InjectorService();
|
|
43
|
-
injector.logger = logger_1.$log;
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
injector.settings.set(DITest.configure(settings));
|
|
46
|
-
setLoggerLevel_1.setLoggerLevel(injector);
|
|
47
|
-
return injector;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
|
|
51
|
-
*/
|
|
52
|
-
static async reset() {
|
|
53
|
-
if (DITest.hasInjector()) {
|
|
54
|
-
await DITest.injector.destroy();
|
|
55
|
-
DITest._injector = null;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Invoke a provider and return a fresh instance
|
|
60
|
-
* @param target
|
|
61
|
-
* @param providers
|
|
62
|
-
*/
|
|
63
|
-
static invoke(target, providers = []) {
|
|
64
|
-
const locals = new LocalsContainer_1.LocalsContainer();
|
|
65
|
-
providers.forEach((p) => {
|
|
66
|
-
locals.set(p.token, p.use);
|
|
67
|
-
});
|
|
68
|
-
locals.set(InjectorService_1.InjectorService, DITest.injector);
|
|
69
|
-
const instance = DITest.injector.invoke(target, locals, { rebuild: true });
|
|
70
|
-
if (instance && instance.$onInit) {
|
|
71
|
-
// await instance.$onInit();
|
|
72
|
-
const result = instance.$onInit();
|
|
73
|
-
if (result instanceof Promise) {
|
|
74
|
-
return result.then(() => instance);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return instance;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Return the instance from injector registry
|
|
81
|
-
* @param target
|
|
82
|
-
* @param options
|
|
83
|
-
*/
|
|
84
|
-
static get(target, options = {}) {
|
|
85
|
-
return DITest.injector.get(target, options);
|
|
86
|
-
}
|
|
87
|
-
static configure(settings = {}) {
|
|
88
|
-
return {
|
|
89
|
-
...settings,
|
|
90
|
-
env: core_1.getValue(settings, "env", core_1.Env.TEST),
|
|
91
|
-
logger: {
|
|
92
|
-
...core_1.getValue(settings, "logger", {}),
|
|
93
|
-
level: core_1.getValue(settings, "logger.level", "off")
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
exports.DITest = DITest;
|
|
99
|
-
DITest._injector = null;
|
|
100
|
-
//# sourceMappingURL=DITest.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../src/services/DITest.ts"],"names":[],"mappings":";;;AAAA,qCAAyC;AACzC,yCAAkC;AAClC,mDAA8C;AAC9C,+DAA0D;AAG1D,4DAAuD;AACvD,uDAAkD;AAOlD;;GAEG;AACH,MAAa,MAAM;IAGjB,MAAM,KAAK,QAAQ;QACjB,IAAI,MAAM,CAAC,SAAS,EAAE;YACpB,OAAO,MAAM,CAAC,SAAU,CAAC;SAC1B;QAED,0BAA0B;QAC1B,MAAM,IAAI,KAAK,CACb,uIAAuI;YACrI,YAAY;YACZ,wBAAwB;YACxB,kCAAkC;YAClC,+CAA+C;YAC/C,IAAI,CACP,CAAC;IACJ,CAAC;IAED,MAAM,KAAK,QAAQ,CAAC,QAAyB;QAC3C,MAAM,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC9B,CAAC;IAED,MAAM,CAAC,WAAW;QAChB,OAAO,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;IAC5B,CAAC;IAED,MAA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AM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAC,OAAO,EAAE,IAAI,EAAC,CAAC,CAAC;QAEjF,IAAI,QAAQ,IAAI,QAAQ,CAAC,OAAO,EAAE;YAChC,4BAA4B;YAC5B,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;YAClC,IAAI,MAAM,YAAY,OAAO,EAAE;gBAC7B,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAe,CAAC,CAAC;aAC3C;SACF;QAED,OAAO,QAAe,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAG,CAAU,MAAqB,EAAE,UAAe,EAAE;QAC1D,OAAO,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAI,MAAM,EAAE,OAAO,CAAE,CAAC;IAClD,CAAC;IAES,MAAM,CAAC,SAAS,CAAC,WAAwC,EAAE;QACnE,OAAO;YACL,GAAG,QAAQ;YACX,GAAG,EAAE,eAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAG,CAAC,IAAI,CAAC;YACxC,MAAM,EAAE;gBACN,GAAG,eAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,CAAC;gBACnC,KAAK,EAAE,eAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,CAAC;aACjD;SACF,CAAC;IACJ,CAAC;;AAzGH,wBA0GC;AAzGkB,gBAAS,GAA2B,IAAI,CAAC","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {$log} from \"@tsed/logger\";\nimport {Container} from \"../domain/Container\";\nimport {LocalsContainer} from \"../domain/LocalsContainer\";\nimport {OnInit} from \"../interfaces/OnInit\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {setLoggerLevel} from \"../utils/setLoggerLevel\";\nimport {InjectorService} from \"./InjectorService\";\n\nexport interface DITestInvokeOptions {\n token?: TokenProvider;\n use: any;\n}\n\n/**\n * Tool to run test with lightweight DI sandbox.\n */\nexport class DITest {\n protected static _injector: InjectorService | null = null;\n\n static get injector(): InjectorService {\n if (DITest._injector) {\n return DITest._injector!;\n }\n\n /* istanbul ignore next */\n throw new Error(\n \"PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\\n\" +\n \"Example:\\n\" +\n \"before(async () => {\\n\" +\n \" await PlatformTest.create()\\n\" +\n \" await PlatformTest.invoke(MyService, [])\\n\" +\n \"})\"\n );\n }\n\n static set injector(injector: InjectorService) {\n DITest._injector = injector;\n }\n\n static hasInjector() {\n return !!DITest._injector;\n }\n\n static async create(settings: Partial<TsED.Configuration> = {}) {\n DITest.injector = DITest.createInjector(settings);\n\n const container = new Container();\n DITest.injector.bootstrap(container);\n\n await DITest.injector.load(container);\n }\n\n /**\n * Create a new injector with the right default services\n */\n static createInjector(settings: any = {}): InjectorService {\n const injector = new InjectorService();\n injector.logger = $log;\n\n // @ts-ignore\n injector.settings.set(DITest.configure(settings));\n\n setLoggerLevel(injector);\n\n return injector;\n }\n\n /**\n * Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.\n */\n static async reset() {\n if (DITest.hasInjector()) {\n await DITest.injector.destroy();\n DITest._injector = null;\n }\n }\n\n /**\n * Invoke a provider and return a fresh instance\n * @param target\n * @param providers\n */\n static invoke<T = any>(target: TokenProvider, providers: DITestInvokeOptions[] = []): T | Promise<T> {\n const locals = new LocalsContainer();\n providers.forEach((p) => {\n locals.set(p.token, p.use);\n });\n\n locals.set(InjectorService, DITest.injector);\n\n const instance: OnInit = DITest.injector.invoke(target, locals, {rebuild: true});\n\n if (instance && instance.$onInit) {\n // await instance.$onInit();\n const result = instance.$onInit();\n if (result instanceof Promise) {\n return result.then(() => instance as any);\n }\n }\n\n return instance as any;\n }\n\n /**\n * Return the instance from injector registry\n * @param target\n * @param options\n */\n static get<T = any>(target: TokenProvider, options: any = {}): T {\n return DITest.injector.get<T>(target, options)!;\n }\n\n protected static configure(settings: Partial<TsED.Configuration> = {}): Partial<TsED.Configuration> {\n return {\n ...settings,\n env: getValue(settings, \"env\", Env.TEST),\n logger: {\n ...getValue(settings, \"logger\", {}),\n level: getValue(settings, \"logger.level\", \"off\")\n }\n };\n }\n}\n"]}
|
|
@@ -1,543 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var InjectorService_1;
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.InjectorService = void 0;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
|
-
const core_1 = require("@tsed/core");
|
|
7
|
-
const constants_1 = require("../constants");
|
|
8
|
-
const configuration_1 = require("../decorators/configuration");
|
|
9
|
-
const injectable_1 = require("../decorators/injectable");
|
|
10
|
-
const domain_1 = require("../domain");
|
|
11
|
-
const Container_1 = require("../domain/Container");
|
|
12
|
-
const LocalsContainer_1 = require("../domain/LocalsContainer");
|
|
13
|
-
const Provider_1 = require("../domain/Provider");
|
|
14
|
-
const InjectionError_1 = require("../errors/InjectionError");
|
|
15
|
-
const UndefinedTokenError_1 = require("../errors/UndefinedTokenError");
|
|
16
|
-
const GlobalProviders_1 = require("../registries/GlobalProviders");
|
|
17
|
-
const createContainer_1 = require("../utils/createContainer");
|
|
18
|
-
const DIConfiguration_1 = require("./DIConfiguration");
|
|
19
|
-
/**
|
|
20
|
-
* This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
|
|
21
|
-
*
|
|
22
|
-
* ### Example:
|
|
23
|
-
*
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import {InjectorService} from "@tsed/di";
|
|
26
|
-
*
|
|
27
|
-
* // Import the services (all services are decorated with @Service()";
|
|
28
|
-
* import MyService1 from "./services/service1";
|
|
29
|
-
* import MyService2 from "./services/service2";
|
|
30
|
-
* import MyService3 from "./services/service3";
|
|
31
|
-
*
|
|
32
|
-
* // When all services is imported you can load InjectorService.
|
|
33
|
-
* const injector = new InjectorService()
|
|
34
|
-
*
|
|
35
|
-
* await injector.load();
|
|
36
|
-
*
|
|
37
|
-
* const myService1 = injector.get<MyService1>(MyServcice1);
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
let InjectorService = InjectorService_1 = class InjectorService extends Container_1.Container {
|
|
41
|
-
constructor() {
|
|
42
|
-
super();
|
|
43
|
-
this.settings = new DIConfiguration_1.DIConfiguration();
|
|
44
|
-
this.logger = console;
|
|
45
|
-
this.resolvedConfiguration = false;
|
|
46
|
-
const provider = this.addProvider(InjectorService_1).getProvider(InjectorService_1);
|
|
47
|
-
provider.instance = this;
|
|
48
|
-
}
|
|
49
|
-
get resolvers() {
|
|
50
|
-
return this.settings.resolvers;
|
|
51
|
-
}
|
|
52
|
-
get scopes() {
|
|
53
|
-
return this.settings.scopes || {};
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Retrieve default scope for a given provider.
|
|
57
|
-
* @param provider
|
|
58
|
-
*/
|
|
59
|
-
scopeOf(provider) {
|
|
60
|
-
return provider.scope || this.scopes[provider.type] || domain_1.ProviderScope.SINGLETON;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
|
|
64
|
-
* @param token
|
|
65
|
-
* @param settings
|
|
66
|
-
*/
|
|
67
|
-
forkProvider(token, settings = {}) {
|
|
68
|
-
if (!this.hasProvider(token)) {
|
|
69
|
-
this.addProvider(token);
|
|
70
|
-
}
|
|
71
|
-
const provider = this.getProvider(token);
|
|
72
|
-
Object.assign(provider, settings);
|
|
73
|
-
provider.instance = this.invoke(token);
|
|
74
|
-
return provider;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Return a list of instance build by the injector.
|
|
78
|
-
*/
|
|
79
|
-
toArray() {
|
|
80
|
-
return super.toArray().map((provider) => provider.instance);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Get a service or factory already constructed from his symbol or class.
|
|
84
|
-
*
|
|
85
|
-
* #### Example
|
|
86
|
-
*
|
|
87
|
-
* ```typescript
|
|
88
|
-
* import {InjectorService} from "@tsed/di";
|
|
89
|
-
* import MyService from "./services";
|
|
90
|
-
*
|
|
91
|
-
* class OtherService {
|
|
92
|
-
* constructor(injectorService: InjectorService) {
|
|
93
|
-
* const myService = injectorService.get<MyService>(MyService);
|
|
94
|
-
* }
|
|
95
|
-
* }
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* @param token The class or symbol registered in InjectorService.
|
|
99
|
-
* @param options
|
|
100
|
-
* @returns {boolean}
|
|
101
|
-
*/
|
|
102
|
-
get(token, options = {}) {
|
|
103
|
-
var _a;
|
|
104
|
-
const instance = (_a = super.get(core_1.getClassOrSymbol(token))) === null || _a === void 0 ? void 0 : _a.instance;
|
|
105
|
-
if (instance !== undefined) {
|
|
106
|
-
return instance;
|
|
107
|
-
}
|
|
108
|
-
if (!this.hasProvider(token)) {
|
|
109
|
-
for (const resolver of this.resolvers) {
|
|
110
|
-
const result = resolver.get(token, options);
|
|
111
|
-
if (result !== undefined) {
|
|
112
|
-
return result;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* The has() method returns a boolean indicating whether an element with the specified key exists or not.
|
|
119
|
-
* @returns {boolean}
|
|
120
|
-
* @param token
|
|
121
|
-
*/
|
|
122
|
-
has(token) {
|
|
123
|
-
return super.has(core_1.getClassOrSymbol(token)) && this.get(token) !== undefined;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Invoke the class and inject all services that required by the class constructor.
|
|
127
|
-
*
|
|
128
|
-
* #### Example
|
|
129
|
-
*
|
|
130
|
-
* ```typescript
|
|
131
|
-
* import {InjectorService} from "@tsed/di";
|
|
132
|
-
* import MyService from "./services";
|
|
133
|
-
*
|
|
134
|
-
* class OtherService {
|
|
135
|
-
* constructor(injectorService: InjectorService) {
|
|
136
|
-
* const myService = injectorService.invoke<MyService>(MyService);
|
|
137
|
-
* }
|
|
138
|
-
* }
|
|
139
|
-
* ```
|
|
140
|
-
*
|
|
141
|
-
* @param token The injectable class to invoke. Class parameters are injected according constructor signature.
|
|
142
|
-
* @param locals Optional object. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
|
|
143
|
-
* @param options
|
|
144
|
-
* @returns {T} The class constructed.
|
|
145
|
-
*/
|
|
146
|
-
invoke(token, locals = new LocalsContainer_1.LocalsContainer(), options = {}) {
|
|
147
|
-
const provider = this.ensureProvider(token);
|
|
148
|
-
let instance;
|
|
149
|
-
!locals.has(configuration_1.Configuration) && locals.set(configuration_1.Configuration, this.settings);
|
|
150
|
-
if (locals.has(token)) {
|
|
151
|
-
return locals.get(token);
|
|
152
|
-
}
|
|
153
|
-
if (token === constants_1.DI_PARAM_OPTIONS) {
|
|
154
|
-
return {};
|
|
155
|
-
}
|
|
156
|
-
if (!provider || options.rebuild) {
|
|
157
|
-
instance = this.resolve(token, locals, options);
|
|
158
|
-
this.hasProvider(token) && (this.getProvider(token).instance = instance);
|
|
159
|
-
return instance;
|
|
160
|
-
}
|
|
161
|
-
switch (this.scopeOf(provider)) {
|
|
162
|
-
case domain_1.ProviderScope.SINGLETON:
|
|
163
|
-
if (!this.has(token)) {
|
|
164
|
-
provider.instance = this.resolve(token, locals, options);
|
|
165
|
-
if (provider.isAsync()) {
|
|
166
|
-
provider.instance.then((instance) => {
|
|
167
|
-
provider.instance = instance;
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
instance = this.get(token);
|
|
172
|
-
break;
|
|
173
|
-
case domain_1.ProviderScope.REQUEST:
|
|
174
|
-
instance = this.resolve(token, locals, options);
|
|
175
|
-
locals.set(token, instance);
|
|
176
|
-
break;
|
|
177
|
-
case domain_1.ProviderScope.INSTANCE:
|
|
178
|
-
instance = this.resolve(provider.provide, locals, options);
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
return instance;
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Build only providers which are asynchronous.
|
|
185
|
-
*/
|
|
186
|
-
async loadAsync(locals = new LocalsContainer_1.LocalsContainer()) {
|
|
187
|
-
for (const [, provider] of this) {
|
|
188
|
-
if (!locals.has(provider.token)) {
|
|
189
|
-
if (provider.isAsync()) {
|
|
190
|
-
await this.invoke(provider.token, locals);
|
|
191
|
-
}
|
|
192
|
-
if (provider.instance) {
|
|
193
|
-
locals.set(provider.token, provider.instance);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return locals;
|
|
198
|
-
}
|
|
199
|
-
loadSync(locals = new LocalsContainer_1.LocalsContainer()) {
|
|
200
|
-
for (const [, provider] of this) {
|
|
201
|
-
if (!locals.has(provider.token) && this.scopeOf(provider) === domain_1.ProviderScope.SINGLETON) {
|
|
202
|
-
this.invoke(provider.token, locals);
|
|
203
|
-
}
|
|
204
|
-
if (provider.instance !== undefined) {
|
|
205
|
-
locals.set(provider.token, provider.instance);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return locals;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Boostrap injector from container and resolve configuration.
|
|
212
|
-
*
|
|
213
|
-
* @param container
|
|
214
|
-
*/
|
|
215
|
-
bootstrap(container = createContainer_1.createContainer()) {
|
|
216
|
-
// Clone all providers in the container
|
|
217
|
-
this.addProviders(container);
|
|
218
|
-
// Resolve all configuration
|
|
219
|
-
this.resolveConfiguration();
|
|
220
|
-
return this;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
|
|
224
|
-
*
|
|
225
|
-
* @param container
|
|
226
|
-
* @param rootModule
|
|
227
|
-
*/
|
|
228
|
-
async load(container = createContainer_1.createContainer(), rootModule) {
|
|
229
|
-
this.bootstrap(container);
|
|
230
|
-
// build async and sync provider
|
|
231
|
-
let locals = await this.loadAsync();
|
|
232
|
-
if (rootModule) {
|
|
233
|
-
await this.invoke(rootModule);
|
|
234
|
-
}
|
|
235
|
-
// load sync provider
|
|
236
|
-
locals = this.loadSync(locals);
|
|
237
|
-
await locals.emit("$onInit");
|
|
238
|
-
return locals;
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Load all configurations registered on providers
|
|
242
|
-
*/
|
|
243
|
-
resolveConfiguration() {
|
|
244
|
-
if (this.resolvedConfiguration) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
const mergedConfiguration = new Map();
|
|
248
|
-
super.forEach((provider) => {
|
|
249
|
-
if (provider.configuration) {
|
|
250
|
-
Object.entries(provider.configuration).forEach(([key, value]) => {
|
|
251
|
-
value = mergedConfiguration.has(key) ? core_1.deepMerge(mergedConfiguration.get(key), value) : core_1.deepClone(value);
|
|
252
|
-
mergedConfiguration.set(key, value);
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
if (provider.resolvers) {
|
|
256
|
-
this.resolvers.push(...provider.resolvers);
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
mergedConfiguration.forEach((value, key) => {
|
|
260
|
-
this.settings[key] = core_1.deepMerge(value, this.settings[key]);
|
|
261
|
-
});
|
|
262
|
-
this.settings.build();
|
|
263
|
-
this.resolvedConfiguration = true;
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
*
|
|
267
|
-
* @param instance
|
|
268
|
-
* @param locals
|
|
269
|
-
* @param options
|
|
270
|
-
*/
|
|
271
|
-
bindInjectableProperties(instance, locals, options) {
|
|
272
|
-
const properties = core_1.ancestorsOf(core_1.classOf(instance)).reduce((properties, target) => {
|
|
273
|
-
const store = core_1.Store.from(target);
|
|
274
|
-
return {
|
|
275
|
-
...properties,
|
|
276
|
-
...(store.get(constants_1.INJECTABLE_PROP) || {})
|
|
277
|
-
};
|
|
278
|
-
}, {});
|
|
279
|
-
Object.values(properties).forEach((definition) => {
|
|
280
|
-
switch (definition.bindingType) {
|
|
281
|
-
case domain_1.InjectablePropertyType.METHOD:
|
|
282
|
-
this.bindMethod(instance, definition);
|
|
283
|
-
break;
|
|
284
|
-
case domain_1.InjectablePropertyType.PROPERTY:
|
|
285
|
-
this.bindProperty(instance, definition, locals, options);
|
|
286
|
-
break;
|
|
287
|
-
case domain_1.InjectablePropertyType.CONSTANT:
|
|
288
|
-
this.bindConstant(instance, definition);
|
|
289
|
-
break;
|
|
290
|
-
case domain_1.InjectablePropertyType.VALUE:
|
|
291
|
-
this.bindValue(instance, definition);
|
|
292
|
-
break;
|
|
293
|
-
case domain_1.InjectablePropertyType.INTERCEPTOR:
|
|
294
|
-
this.bindInterceptor(instance, definition);
|
|
295
|
-
break;
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
*
|
|
301
|
-
* @param instance
|
|
302
|
-
* @param {string} propertyKey
|
|
303
|
-
*/
|
|
304
|
-
bindMethod(instance, { propertyKey }) {
|
|
305
|
-
const target = core_1.classOf(instance);
|
|
306
|
-
const originalMethod = instance[propertyKey];
|
|
307
|
-
const deps = core_1.Metadata.getParamTypes(core_1.prototypeOf(target), propertyKey);
|
|
308
|
-
instance[propertyKey] = () => {
|
|
309
|
-
const services = deps.map((dependency) => this.get(dependency));
|
|
310
|
-
return originalMethod.call(instance, ...services);
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Create an injectable property.
|
|
315
|
-
*
|
|
316
|
-
* @param instance
|
|
317
|
-
* @param {string} propertyKey
|
|
318
|
-
* @param {any} useType
|
|
319
|
-
* @param onGet
|
|
320
|
-
* @param options
|
|
321
|
-
* @param locals
|
|
322
|
-
* @param invokeOptions
|
|
323
|
-
*/
|
|
324
|
-
bindProperty(instance, { propertyKey, useType, onGet = (f) => f, options }, locals, invokeOptions) {
|
|
325
|
-
invokeOptions = { ...invokeOptions };
|
|
326
|
-
locals.set(constants_1.DI_PARAM_OPTIONS, { ...options });
|
|
327
|
-
let bean = this.invoke(useType, locals, invokeOptions);
|
|
328
|
-
locals.delete(constants_1.DI_PARAM_OPTIONS);
|
|
329
|
-
if (core_1.isPromise(bean)) {
|
|
330
|
-
bean.then((result) => {
|
|
331
|
-
bean = result;
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
Object.defineProperty(instance, propertyKey, {
|
|
335
|
-
get: () => onGet(bean)
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
*
|
|
340
|
-
* @param instance
|
|
341
|
-
* @param {string} propertyKey
|
|
342
|
-
* @param {any} useType
|
|
343
|
-
*/
|
|
344
|
-
bindValue(instance, { propertyKey, expression, defaultValue }) {
|
|
345
|
-
const descriptor = {
|
|
346
|
-
get: () => this.settings.get(expression) || defaultValue,
|
|
347
|
-
set: (value) => this.settings.set(expression, value),
|
|
348
|
-
enumerable: true,
|
|
349
|
-
configurable: true
|
|
350
|
-
};
|
|
351
|
-
Object.defineProperty(instance, propertyKey, descriptor);
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
*
|
|
355
|
-
* @param instance
|
|
356
|
-
* @param {string} propertyKey
|
|
357
|
-
* @param {any} useType
|
|
358
|
-
*/
|
|
359
|
-
bindConstant(instance, { propertyKey, expression, defaultValue }) {
|
|
360
|
-
const clone = (o) => {
|
|
361
|
-
if (o) {
|
|
362
|
-
return Object.freeze(core_1.deepClone(o));
|
|
363
|
-
}
|
|
364
|
-
return defaultValue;
|
|
365
|
-
};
|
|
366
|
-
const descriptor = {
|
|
367
|
-
get: () => clone(this.settings.get(expression)),
|
|
368
|
-
enumerable: true,
|
|
369
|
-
configurable: true
|
|
370
|
-
};
|
|
371
|
-
Object.defineProperty(instance, propertyKey, descriptor);
|
|
372
|
-
return descriptor;
|
|
373
|
-
}
|
|
374
|
-
/**
|
|
375
|
-
*
|
|
376
|
-
* @param instance
|
|
377
|
-
* @param propertyKey
|
|
378
|
-
* @param useType
|
|
379
|
-
* @param options
|
|
380
|
-
*/
|
|
381
|
-
bindInterceptor(instance, { propertyKey, useType, options }) {
|
|
382
|
-
const target = core_1.classOf(instance);
|
|
383
|
-
const originalMethod = instance[propertyKey];
|
|
384
|
-
instance[propertyKey] = (...args) => {
|
|
385
|
-
const next = (err) => {
|
|
386
|
-
if (!err) {
|
|
387
|
-
return originalMethod.apply(instance, args);
|
|
388
|
-
}
|
|
389
|
-
throw err;
|
|
390
|
-
};
|
|
391
|
-
const context = {
|
|
392
|
-
target,
|
|
393
|
-
propertyKey,
|
|
394
|
-
args,
|
|
395
|
-
options,
|
|
396
|
-
next
|
|
397
|
-
};
|
|
398
|
-
const interceptor = this.get(useType);
|
|
399
|
-
return interceptor.intercept({
|
|
400
|
-
...context,
|
|
401
|
-
options
|
|
402
|
-
}, next);
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Allow handler hack for AsyncHookContext plugin.
|
|
407
|
-
* @param ctx
|
|
408
|
-
* @param cb
|
|
409
|
-
* @protected
|
|
410
|
-
*/
|
|
411
|
-
runInContext(ctx, cb) {
|
|
412
|
-
return cb();
|
|
413
|
-
}
|
|
414
|
-
ensureProvider(token) {
|
|
415
|
-
if (!this.hasProvider(token) && GlobalProviders_1.GlobalProviders.has(token)) {
|
|
416
|
-
this.addProvider(token);
|
|
417
|
-
}
|
|
418
|
-
return this.getProvider(token);
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Invoke a class method and inject service.
|
|
422
|
-
*
|
|
423
|
-
* #### IInjectableMethod options
|
|
424
|
-
*
|
|
425
|
-
* * **target**: Optional. The class instance.
|
|
426
|
-
* * **methodName**: `string` Optional. The method name.
|
|
427
|
-
* * **designParamTypes**: `any[]` Optional. List of injectable types.
|
|
428
|
-
* * **locals**: `Map<Function, any>` Optional. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
|
|
429
|
-
*
|
|
430
|
-
* #### Example
|
|
431
|
-
*
|
|
432
|
-
* @param target
|
|
433
|
-
* @param locals
|
|
434
|
-
* @param options
|
|
435
|
-
* @private
|
|
436
|
-
*/
|
|
437
|
-
resolve(target, locals, options = {}) {
|
|
438
|
-
const { token, deps, construct, isBindable, imports, provider } = this.mapInvokeOptions(target, locals, options);
|
|
439
|
-
if (provider) {
|
|
440
|
-
if (!provider.injectable && options.parent) {
|
|
441
|
-
throw new InjectionError_1.InjectionError(token, `${core_1.nameOf(token)} ${provider.type} is not injectable to another provider`);
|
|
442
|
-
}
|
|
443
|
-
const { onInvoke } = GlobalProviders_1.GlobalProviders.getRegistrySettings(target);
|
|
444
|
-
if (onInvoke) {
|
|
445
|
-
onInvoke(provider, locals, deps);
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
let instance;
|
|
449
|
-
let currentDependency = false;
|
|
450
|
-
try {
|
|
451
|
-
const invokeDependency = (parent) => (token, index) => {
|
|
452
|
-
var _a;
|
|
453
|
-
currentDependency = { token, index, deps };
|
|
454
|
-
if (token !== constants_1.DI_PARAM_OPTIONS) {
|
|
455
|
-
const options = (_a = provider === null || provider === void 0 ? void 0 : provider.store) === null || _a === void 0 ? void 0 : _a.get(`${constants_1.DI_PARAM_OPTIONS}:${index}`);
|
|
456
|
-
locals.set(constants_1.DI_PARAM_OPTIONS, options || {});
|
|
457
|
-
}
|
|
458
|
-
return core_1.isInheritedFrom(token, Provider_1.Provider, 1) ? provider : this.invoke(token, locals, { parent });
|
|
459
|
-
};
|
|
460
|
-
// Invoke manually imported providers
|
|
461
|
-
imports.forEach(invokeDependency());
|
|
462
|
-
// Inject dependencies
|
|
463
|
-
const services = deps.map(invokeDependency(token));
|
|
464
|
-
currentDependency = false;
|
|
465
|
-
instance = construct(services);
|
|
466
|
-
}
|
|
467
|
-
catch (error) {
|
|
468
|
-
InjectionError_1.InjectionError.throwInjectorError(token, currentDependency, error);
|
|
469
|
-
}
|
|
470
|
-
if (instance === undefined) {
|
|
471
|
-
throw new InjectionError_1.InjectionError(token, `Unable to create new instance from undefined value. Check your provider declaration for ${core_1.nameOf(token)}`);
|
|
472
|
-
}
|
|
473
|
-
if (instance && isBindable) {
|
|
474
|
-
this.bindInjectableProperties(instance, locals, options);
|
|
475
|
-
}
|
|
476
|
-
return instance;
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Create options to invoke a provider or class.
|
|
480
|
-
* @param token
|
|
481
|
-
* @param locals
|
|
482
|
-
* @param options
|
|
483
|
-
*/
|
|
484
|
-
mapInvokeOptions(token, locals, options) {
|
|
485
|
-
let imports = options.imports;
|
|
486
|
-
let deps = options.deps;
|
|
487
|
-
let scope = options.scope;
|
|
488
|
-
let construct;
|
|
489
|
-
let isBindable = false;
|
|
490
|
-
if (!token) {
|
|
491
|
-
throw new UndefinedTokenError_1.UndefinedTokenError();
|
|
492
|
-
}
|
|
493
|
-
let provider;
|
|
494
|
-
if (!this.hasProvider(token)) {
|
|
495
|
-
provider = new Provider_1.Provider(token);
|
|
496
|
-
this.resolvers.forEach((resolver) => {
|
|
497
|
-
const result = resolver.get(token, locals.get(constants_1.DI_PARAM_OPTIONS));
|
|
498
|
-
if (result !== undefined) {
|
|
499
|
-
provider.useFactory = () => result;
|
|
500
|
-
}
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
else {
|
|
504
|
-
provider = this.getProvider(token);
|
|
505
|
-
}
|
|
506
|
-
scope = scope || this.scopeOf(provider);
|
|
507
|
-
deps = deps || provider.deps;
|
|
508
|
-
imports = imports || provider.imports;
|
|
509
|
-
if (provider.useValue !== undefined) {
|
|
510
|
-
construct = () => (core_1.isFunction(provider.useValue) ? provider.useValue() : provider.useValue);
|
|
511
|
-
}
|
|
512
|
-
else if (provider.useFactory) {
|
|
513
|
-
construct = (deps) => provider.useFactory(...deps);
|
|
514
|
-
}
|
|
515
|
-
else if (provider.useAsyncFactory) {
|
|
516
|
-
construct = (deps) => provider.useAsyncFactory(...deps);
|
|
517
|
-
}
|
|
518
|
-
else {
|
|
519
|
-
// useClass
|
|
520
|
-
isBindable = true;
|
|
521
|
-
deps = deps || core_1.Metadata.getParamTypes(provider.useClass);
|
|
522
|
-
construct = (deps) => new provider.useClass(...deps);
|
|
523
|
-
}
|
|
524
|
-
return {
|
|
525
|
-
token,
|
|
526
|
-
scope: scope || core_1.Store.from(token).get("scope") || domain_1.ProviderScope.SINGLETON,
|
|
527
|
-
deps: deps || [],
|
|
528
|
-
imports: imports || [],
|
|
529
|
-
isBindable,
|
|
530
|
-
construct,
|
|
531
|
-
provider
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
InjectorService = InjectorService_1 = tslib_1.__decorate([
|
|
536
|
-
injectable_1.Injectable({
|
|
537
|
-
scope: domain_1.ProviderScope.SINGLETON,
|
|
538
|
-
global: true
|
|
539
|
-
}),
|
|
540
|
-
tslib_1.__metadata("design:paramtypes", [])
|
|
541
|
-
], InjectorService);
|
|
542
|
-
exports.InjectorService = InjectorService;
|
|
543
|
-
//# sourceMappingURL=InjectorService.js.map
|