@tsed/di 7.0.0-alpha.2 → 7.0.0-beta.1
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/cjs/constants/index.js +7 -0
- package/lib/cjs/constants/index.js.map +1 -0
- package/lib/cjs/decorators/configuration.js +24 -0
- package/lib/cjs/decorators/configuration.js.map +1 -0
- package/lib/cjs/decorators/constant.js +55 -0
- package/lib/cjs/decorators/constant.js.map +1 -0
- package/lib/cjs/decorators/controller.js +53 -0
- package/lib/cjs/decorators/controller.js.map +1 -0
- package/lib/cjs/decorators/index.js +19 -0
- package/lib/cjs/decorators/index.js.map +1 -0
- package/lib/cjs/decorators/inject.js +79 -0
- package/lib/cjs/decorators/inject.js.map +1 -0
- package/lib/cjs/decorators/injectable.js +34 -0
- package/lib/cjs/decorators/injectable.js.map +1 -0
- package/lib/cjs/decorators/intercept.js +28 -0
- package/lib/cjs/decorators/intercept.js.map +1 -0
- package/lib/cjs/decorators/interceptor.js +21 -0
- package/lib/cjs/decorators/interceptor.js.map +1 -0
- package/lib/cjs/decorators/lazyInject.js +71 -0
- package/lib/cjs/decorators/lazyInject.js.map +1 -0
- package/lib/cjs/decorators/module.js +31 -0
- package/lib/cjs/decorators/module.js.map +1 -0
- package/lib/cjs/decorators/opts.js +54 -0
- package/lib/cjs/decorators/opts.js.map +1 -0
- package/lib/cjs/decorators/overrideProvider.js +17 -0
- package/lib/cjs/decorators/overrideProvider.js.map +1 -0
- package/lib/cjs/decorators/scope.js +16 -0
- package/lib/cjs/decorators/scope.js.map +1 -0
- package/lib/cjs/decorators/service.js +18 -0
- package/lib/cjs/decorators/service.js.map +1 -0
- package/lib/cjs/decorators/useOpts.js +65 -0
- package/lib/cjs/decorators/useOpts.js.map +1 -0
- package/lib/cjs/decorators/value.js +55 -0
- package/lib/cjs/decorators/value.js.map +1 -0
- package/lib/cjs/domain/Container.js +72 -0
- package/lib/cjs/domain/Container.js.map +1 -0
- package/lib/cjs/domain/ContextLogger.js +85 -0
- package/lib/cjs/domain/ContextLogger.js.map +1 -0
- package/lib/cjs/domain/DIContext.js +45 -0
- package/lib/cjs/domain/DIContext.js.map +1 -0
- package/lib/cjs/domain/InjectablePropertyType.js +12 -0
- package/lib/cjs/domain/InjectablePropertyType.js.map +1 -0
- package/lib/cjs/domain/LocalsContainer.js +55 -0
- package/lib/cjs/domain/LocalsContainer.js.map +1 -0
- package/lib/cjs/domain/Provider.js +122 -0
- package/lib/cjs/domain/Provider.js.map +1 -0
- package/lib/cjs/domain/ProviderScope.js +10 -0
- package/lib/cjs/domain/ProviderScope.js.map +1 -0
- package/lib/cjs/domain/ProviderType.js +15 -0
- package/lib/cjs/domain/ProviderType.js.map +1 -0
- package/lib/cjs/domain/index.js +12 -0
- package/lib/cjs/domain/index.js.map +1 -0
- package/lib/cjs/errors/InjectionError.js +58 -0
- package/lib/cjs/errors/InjectionError.js.map +1 -0
- package/lib/cjs/errors/UndefinedTokenError.js +11 -0
- package/lib/cjs/errors/UndefinedTokenError.js.map +1 -0
- package/lib/cjs/index.js +21 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/DIConfigurationOptions.js +3 -0
- package/lib/cjs/interfaces/DIConfigurationOptions.js.map +1 -0
- package/lib/cjs/interfaces/DILogger.js +3 -0
- package/lib/cjs/interfaces/DILogger.js.map +1 -0
- package/lib/cjs/interfaces/DIResolver.js +3 -0
- package/lib/cjs/interfaces/DIResolver.js.map +1 -0
- package/lib/cjs/interfaces/IProvider.js +3 -0
- package/lib/cjs/interfaces/IProvider.js.map +1 -0
- package/lib/cjs/interfaces/InjectableProperties.js +3 -0
- package/lib/cjs/interfaces/InjectableProperties.js.map +1 -0
- package/lib/cjs/interfaces/InterceptorContext.js +3 -0
- package/lib/cjs/interfaces/InterceptorContext.js.map +1 -0
- package/lib/cjs/interfaces/InterceptorMethods.js +3 -0
- package/lib/cjs/interfaces/InterceptorMethods.js.map +1 -0
- package/lib/cjs/interfaces/InvokeOptions.js +3 -0
- package/lib/cjs/interfaces/InvokeOptions.js.map +1 -0
- package/lib/cjs/interfaces/OnDestroy.js +3 -0
- package/lib/cjs/interfaces/OnDestroy.js.map +1 -0
- package/lib/cjs/interfaces/OnInit.js +3 -0
- package/lib/cjs/interfaces/OnInit.js.map +1 -0
- package/lib/cjs/interfaces/RegistrySettings.js +3 -0
- package/lib/cjs/interfaces/RegistrySettings.js.map +1 -0
- package/lib/cjs/interfaces/ResolvedInvokeOptions.js +3 -0
- package/lib/cjs/interfaces/ResolvedInvokeOptions.js.map +1 -0
- package/lib/cjs/interfaces/TokenProvider.js +3 -0
- package/lib/cjs/interfaces/TokenProvider.js.map +1 -0
- package/lib/cjs/interfaces/index.js +18 -0
- package/lib/cjs/interfaces/index.js.map +1 -0
- package/lib/cjs/registries/GlobalProviders.js +124 -0
- package/lib/cjs/registries/GlobalProviders.js.map +1 -0
- package/lib/cjs/registries/ProviderRegistry.js +85 -0
- package/lib/cjs/registries/ProviderRegistry.js.map +1 -0
- package/lib/cjs/services/DIConfiguration.js +129 -0
- package/lib/cjs/services/DIConfiguration.js.map +1 -0
- package/lib/cjs/services/DILogger.js +13 -0
- package/lib/cjs/services/DILogger.js.map +1 -0
- package/lib/cjs/services/DITest.js +108 -0
- package/lib/cjs/services/DITest.js.map +1 -0
- package/lib/cjs/services/InjectorService.js +581 -0
- package/lib/cjs/services/InjectorService.js.map +1 -0
- package/lib/cjs/utils/colors.js +10 -0
- package/lib/cjs/utils/colors.js.map +1 -0
- package/lib/cjs/utils/createContainer.js +14 -0
- package/lib/cjs/utils/createContainer.js.map +1 -0
- package/lib/cjs/utils/data/Test1.js +8 -0
- package/lib/cjs/utils/data/Test1.js.map +1 -0
- package/lib/cjs/utils/data/Test2.js +8 -0
- package/lib/cjs/utils/data/Test2.js.map +1 -0
- package/lib/cjs/utils/getConfiguration.js +20 -0
- package/lib/cjs/utils/getConfiguration.js.map +1 -0
- package/lib/cjs/utils/setLoggerLevel.js +16 -0
- package/lib/cjs/utils/setLoggerLevel.js.map +1 -0
- package/lib/esm/constants/index.js +4 -0
- package/lib/esm/constants/index.js.map +1 -0
- package/lib/esm/decorators/configuration.js +20 -0
- package/lib/esm/decorators/configuration.js.map +1 -0
- package/lib/esm/decorators/constant.js +51 -0
- package/lib/esm/decorators/constant.js.map +1 -0
- package/lib/esm/decorators/controller.js +49 -0
- package/lib/esm/decorators/controller.js.map +1 -0
- package/lib/esm/decorators/index.js +16 -0
- package/lib/esm/decorators/index.js.map +1 -0
- package/lib/esm/decorators/inject.js +74 -0
- package/lib/esm/decorators/inject.js.map +1 -0
- package/lib/esm/decorators/injectable.js +30 -0
- package/lib/esm/decorators/injectable.js.map +1 -0
- package/lib/esm/decorators/intercept.js +24 -0
- package/lib/esm/decorators/intercept.js.map +1 -0
- package/lib/esm/decorators/interceptor.js +17 -0
- package/lib/esm/decorators/interceptor.js.map +1 -0
- package/lib/esm/decorators/lazyInject.js +66 -0
- package/lib/esm/decorators/lazyInject.js.map +1 -0
- package/lib/esm/decorators/module.js +27 -0
- package/lib/esm/decorators/module.js.map +1 -0
- package/lib/esm/decorators/opts.js +50 -0
- package/lib/esm/decorators/opts.js.map +1 -0
- package/lib/esm/decorators/overrideProvider.js +13 -0
- package/lib/esm/decorators/overrideProvider.js.map +1 -0
- package/lib/esm/decorators/scope.js +12 -0
- package/lib/esm/decorators/scope.js.map +1 -0
- package/lib/esm/decorators/service.js +14 -0
- package/lib/esm/decorators/service.js.map +1 -0
- package/lib/esm/decorators/useOpts.js +61 -0
- package/lib/esm/decorators/useOpts.js.map +1 -0
- package/lib/esm/decorators/value.js +51 -0
- package/lib/esm/decorators/value.js.map +1 -0
- package/lib/esm/domain/Container.js +68 -0
- package/lib/esm/domain/Container.js.map +1 -0
- package/lib/esm/domain/ContextLogger.js +81 -0
- package/lib/esm/domain/ContextLogger.js.map +1 -0
- package/lib/esm/domain/DIContext.js +41 -0
- package/lib/esm/domain/DIContext.js.map +1 -0
- package/lib/esm/domain/InjectablePropertyType.js +9 -0
- package/lib/esm/domain/InjectablePropertyType.js.map +1 -0
- package/lib/esm/domain/LocalsContainer.js +51 -0
- package/lib/esm/domain/LocalsContainer.js.map +1 -0
- package/lib/esm/domain/Provider.js +118 -0
- package/lib/esm/domain/Provider.js.map +1 -0
- package/lib/esm/domain/ProviderScope.js +7 -0
- package/lib/esm/domain/ProviderScope.js.map +1 -0
- package/lib/esm/domain/ProviderType.js +12 -0
- package/lib/esm/domain/ProviderType.js.map +1 -0
- package/lib/esm/domain/index.js +9 -0
- package/lib/esm/domain/index.js.map +1 -0
- package/lib/esm/errors/InjectionError.js +54 -0
- package/lib/esm/errors/InjectionError.js.map +1 -0
- package/lib/esm/errors/UndefinedTokenError.js +7 -0
- package/lib/esm/errors/UndefinedTokenError.js.map +1 -0
- package/lib/esm/index.js +18 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/DIConfigurationOptions.js +2 -0
- package/lib/esm/interfaces/DIConfigurationOptions.js.map +1 -0
- package/lib/esm/interfaces/DILogger.js +2 -0
- package/lib/esm/interfaces/DILogger.js.map +1 -0
- package/lib/esm/interfaces/DIResolver.js +2 -0
- package/lib/esm/interfaces/DIResolver.js.map +1 -0
- package/lib/esm/interfaces/IProvider.js +2 -0
- package/lib/esm/interfaces/IProvider.js.map +1 -0
- package/lib/esm/interfaces/InjectableProperties.js +2 -0
- package/lib/esm/interfaces/InjectableProperties.js.map +1 -0
- package/lib/esm/interfaces/InterceptorContext.js +2 -0
- package/lib/esm/interfaces/InterceptorContext.js.map +1 -0
- package/lib/esm/interfaces/InterceptorMethods.js +2 -0
- package/lib/esm/interfaces/InterceptorMethods.js.map +1 -0
- package/lib/esm/interfaces/InvokeOptions.js +2 -0
- package/lib/esm/interfaces/InvokeOptions.js.map +1 -0
- package/lib/esm/interfaces/OnDestroy.js +2 -0
- package/lib/esm/interfaces/OnDestroy.js.map +1 -0
- package/lib/esm/interfaces/OnInit.js +2 -0
- package/lib/esm/interfaces/OnInit.js.map +1 -0
- package/lib/esm/interfaces/RegistrySettings.js +2 -0
- package/lib/esm/interfaces/RegistrySettings.js.map +1 -0
- package/lib/esm/interfaces/ResolvedInvokeOptions.js +2 -0
- package/lib/esm/interfaces/ResolvedInvokeOptions.js.map +1 -0
- package/lib/esm/interfaces/TokenProvider.js +2 -0
- package/lib/esm/interfaces/TokenProvider.js.map +1 -0
- package/lib/esm/interfaces/index.js +15 -0
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/registries/GlobalProviders.js +120 -0
- package/lib/esm/registries/GlobalProviders.js.map +1 -0
- package/lib/esm/registries/ProviderRegistry.js +80 -0
- package/lib/esm/registries/ProviderRegistry.js.map +1 -0
- package/lib/esm/services/DIConfiguration.js +125 -0
- package/lib/esm/services/DIConfiguration.js.map +1 -0
- package/lib/esm/services/DILogger.js +11 -0
- package/lib/esm/services/DILogger.js.map +1 -0
- package/lib/esm/services/DITest.js +104 -0
- package/lib/esm/services/DITest.js.map +1 -0
- package/lib/esm/services/InjectorService.js +578 -0
- package/lib/esm/services/InjectorService.js.map +1 -0
- package/lib/esm/utils/colors.js +7 -0
- package/lib/esm/utils/colors.js.map +1 -0
- package/lib/esm/utils/createContainer.js +10 -0
- package/lib/esm/utils/createContainer.js.map +1 -0
- package/lib/esm/utils/data/Test1.js +4 -0
- package/lib/esm/utils/data/Test1.js.map +1 -0
- package/lib/esm/utils/data/Test2.js +4 -0
- package/lib/esm/utils/data/Test2.js.map +1 -0
- package/lib/esm/utils/getConfiguration.js +16 -0
- package/lib/esm/utils/getConfiguration.js.map +1 -0
- package/lib/esm/utils/setLoggerLevel.js +12 -0
- package/lib/esm/utils/setLoggerLevel.js.map +1 -0
- package/lib/{constants → types/constants}/index.d.ts +0 -0
- package/lib/{decorators → types/decorators}/configuration.d.ts +0 -0
- package/lib/{decorators → types/decorators}/constant.d.ts +0 -0
- package/lib/{decorators → types/decorators}/controller.d.ts +0 -0
- package/lib/{decorators → types/decorators}/index.d.ts +1 -0
- package/lib/{decorators → types/decorators}/inject.d.ts +2 -0
- package/lib/{decorators → types/decorators}/injectable.d.ts +2 -1
- package/lib/{decorators → types/decorators}/intercept.d.ts +0 -0
- package/lib/{decorators → types/decorators}/interceptor.d.ts +0 -0
- package/lib/types/decorators/lazyInject.d.ts +43 -0
- package/lib/{decorators → types/decorators}/module.d.ts +0 -0
- package/lib/{decorators → types/decorators}/opts.d.ts +0 -0
- package/lib/{decorators → types/decorators}/overrideProvider.d.ts +0 -0
- package/lib/{decorators → types/decorators}/scope.d.ts +0 -0
- package/lib/{decorators → types/decorators}/service.d.ts +0 -0
- package/lib/{decorators → types/decorators}/useOpts.d.ts +0 -0
- package/lib/{decorators → types/decorators}/value.d.ts +0 -0
- package/lib/{domain → types/domain}/Container.d.ts +2 -3
- package/lib/{domain → types/domain}/ContextLogger.d.ts +0 -0
- package/lib/{domain → types/domain}/DIContext.d.ts +0 -0
- package/lib/{domain → types/domain}/InjectablePropertyType.d.ts +0 -0
- package/lib/{domain → types/domain}/LocalsContainer.d.ts +0 -0
- package/lib/{domain → types/domain}/Provider.d.ts +16 -0
- package/lib/{domain → types/domain}/ProviderScope.d.ts +0 -0
- package/lib/{domain → types/domain}/ProviderType.d.ts +0 -0
- package/lib/{domain → types/domain}/index.d.ts +0 -0
- package/lib/{errors → types/errors}/InjectionError.d.ts +0 -0
- package/lib/{errors → types/errors}/UndefinedTokenError.d.ts +0 -0
- package/lib/{index.d.ts → types/index.d.ts} +1 -0
- package/lib/{interfaces → types/interfaces}/DIConfigurationOptions.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/DILogger.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/DIResolver.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/IProvider.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/InjectableProperties.d.ts +8 -4
- package/lib/{interfaces → types/interfaces}/InterceptorContext.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/InterceptorMethods.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/InvokeOptions.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/OnDestroy.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/OnInit.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/RegistrySettings.d.ts +6 -2
- package/lib/types/interfaces/ResolvedInvokeOptions.d.ts +12 -0
- package/lib/{interfaces → types/interfaces}/TokenProvider.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/index.d.ts +1 -0
- package/lib/{registries → types/registries}/GlobalProviders.d.ts +5 -1
- package/lib/{registries → types/registries}/ProviderRegistry.d.ts +0 -0
- package/lib/{services → types/services}/DIConfiguration.d.ts +0 -0
- package/lib/{services → types/services}/DILogger.d.ts +0 -0
- package/lib/{services → types/services}/DITest.d.ts +5 -1
- package/lib/{services → types/services}/InjectorService.d.ts +32 -8
- package/lib/types/utils/colors.d.ts +5 -0
- package/lib/{utils → types/utils}/createContainer.d.ts +0 -0
- package/lib/types/utils/data/Test1.d.ts +3 -0
- package/lib/types/utils/data/Test2.d.ts +3 -0
- package/lib/{utils → types/utils}/getConfiguration.d.ts +0 -0
- package/lib/{utils → types/utils}/setLoggerLevel.d.ts +0 -0
- package/package.json +15 -14
- package/tsconfig.compile.esm.json +24 -0
- package/lib/index.js +0 -2269
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -2232
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../../src/services/DILogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,gBAAgB,EAAC,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAElD,gBAAgB,CAAC;IACf,OAAO,EAAE,MAAM;IACf,IAAI,EAAE,CAAC,eAAe,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"]}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Env, getValue } from "@tsed/core";
|
|
2
|
+
import { $log } from "@tsed/logger";
|
|
3
|
+
import { createContainer } from "../utils/createContainer";
|
|
4
|
+
import { LocalsContainer } from "../domain/LocalsContainer";
|
|
5
|
+
import { setLoggerLevel } from "../utils/setLoggerLevel";
|
|
6
|
+
import { InjectorService } from "./InjectorService";
|
|
7
|
+
/**
|
|
8
|
+
* Tool to run test with lightweight DI sandbox.
|
|
9
|
+
*/
|
|
10
|
+
export class DITest {
|
|
11
|
+
static get injector() {
|
|
12
|
+
if (DITest._injector) {
|
|
13
|
+
return DITest._injector;
|
|
14
|
+
}
|
|
15
|
+
/* istanbul ignore next */
|
|
16
|
+
throw new Error("PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\n" +
|
|
17
|
+
"Example:\n" +
|
|
18
|
+
"before(async () => {\n" +
|
|
19
|
+
" await PlatformTest.create()\n" +
|
|
20
|
+
" await PlatformTest.invoke(MyService, [])\n" +
|
|
21
|
+
"})");
|
|
22
|
+
}
|
|
23
|
+
static set injector(injector) {
|
|
24
|
+
DITest._injector = injector;
|
|
25
|
+
}
|
|
26
|
+
static hasInjector() {
|
|
27
|
+
return !!DITest._injector;
|
|
28
|
+
}
|
|
29
|
+
static async create(settings = {}) {
|
|
30
|
+
DITest.injector = DITest.createInjector(settings);
|
|
31
|
+
await this.createContainer(settings);
|
|
32
|
+
}
|
|
33
|
+
static async createContainer(settings = {}) {
|
|
34
|
+
var _a;
|
|
35
|
+
const container = createContainer();
|
|
36
|
+
(_a = settings.imports) === null || _a === void 0 ? void 0 : _a.forEach(({ token, use }) => {
|
|
37
|
+
container.addProvider(token, {
|
|
38
|
+
useValue: use
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
await DITest.injector.load(container);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a new injector with the right default services
|
|
45
|
+
*/
|
|
46
|
+
static createInjector(settings = {}) {
|
|
47
|
+
const injector = new InjectorService();
|
|
48
|
+
injector.logger = $log;
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
injector.settings.set(DITest.configure(settings));
|
|
51
|
+
setLoggerLevel(injector);
|
|
52
|
+
return injector;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
|
|
56
|
+
*/
|
|
57
|
+
static async reset() {
|
|
58
|
+
if (DITest.hasInjector()) {
|
|
59
|
+
await DITest.injector.destroy();
|
|
60
|
+
DITest._injector = null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Invoke a provider and return a fresh instance
|
|
65
|
+
* @param target
|
|
66
|
+
* @param providers
|
|
67
|
+
*/
|
|
68
|
+
static invoke(target, providers = []) {
|
|
69
|
+
const locals = new LocalsContainer();
|
|
70
|
+
providers.forEach((p) => {
|
|
71
|
+
locals.set(p.token, p.use);
|
|
72
|
+
});
|
|
73
|
+
locals.set(InjectorService, DITest.injector);
|
|
74
|
+
const instance = DITest.injector.invoke(target, locals, { rebuild: true });
|
|
75
|
+
if (instance && instance.$onInit) {
|
|
76
|
+
// await instance.$onInit();
|
|
77
|
+
const result = instance.$onInit();
|
|
78
|
+
if (result instanceof Promise) {
|
|
79
|
+
return result.then(() => instance);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return instance;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Return the instance from injector registry
|
|
86
|
+
* @param target
|
|
87
|
+
* @param options
|
|
88
|
+
*/
|
|
89
|
+
static get(target, options = {}) {
|
|
90
|
+
return DITest.injector.get(target, options);
|
|
91
|
+
}
|
|
92
|
+
static configure(settings = {}) {
|
|
93
|
+
return {
|
|
94
|
+
...settings,
|
|
95
|
+
env: getValue(settings, "env", Env.TEST),
|
|
96
|
+
logger: {
|
|
97
|
+
...getValue(settings, "logger", {}),
|
|
98
|
+
level: getValue(settings, "logger.level", "off")
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
DITest._injector = null;
|
|
104
|
+
//# sourceMappingURL=DITest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../src/services/DITest.ts"],"names":[],"mappings":"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","sourcesContent":["import {Env, getValue} from \"@tsed/core\";\nimport {$log} from \"@tsed/logger\";\nimport {createContainer} from \"../utils/createContainer\";\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\nexport interface DITestOptions extends Partial<TsED.Configuration> {\n imports?: DITestInvokeOptions[];\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: DITestOptions = {}) {\n DITest.injector = DITest.createInjector(settings);\n\n await this.createContainer(settings);\n }\n\n static async createContainer(settings: DITestOptions = {}) {\n const container = createContainer();\n\n settings.imports?.forEach(({token, use}) => {\n container.addProvider(token, {\n useValue: use\n });\n });\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"]}
|
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
var _InjectorService_cache;
|
|
2
|
+
var InjectorService_1;
|
|
3
|
+
import { __classPrivateFieldGet, __decorate, __metadata } from "tslib";
|
|
4
|
+
import { ancestorsOf, catchError, classOf, deepClone, deepMerge, isClass, isFunction, isInheritedFrom, Metadata, nameOf, prototypeOf, Store } from "@tsed/core";
|
|
5
|
+
import { DI_PARAM_OPTIONS, INJECTABLE_PROP } from "../constants";
|
|
6
|
+
import { Configuration } from "../decorators/configuration";
|
|
7
|
+
import { Injectable } from "../decorators/injectable";
|
|
8
|
+
import { InjectablePropertyType, ProviderScope } from "../domain";
|
|
9
|
+
import { Container } from "../domain/Container";
|
|
10
|
+
import { LocalsContainer } from "../domain/LocalsContainer";
|
|
11
|
+
import { Provider } from "../domain/Provider";
|
|
12
|
+
import { InjectionError } from "../errors/InjectionError";
|
|
13
|
+
import { UndefinedTokenError } from "../errors/UndefinedTokenError";
|
|
14
|
+
import { GlobalProviders } from "../registries/GlobalProviders";
|
|
15
|
+
import { createContainer } from "../utils/createContainer";
|
|
16
|
+
import { DIConfiguration } from "./DIConfiguration";
|
|
17
|
+
/**
|
|
18
|
+
* This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
|
|
19
|
+
*
|
|
20
|
+
* ### Example:
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import {InjectorService} from "@tsed/di";
|
|
24
|
+
*
|
|
25
|
+
* // Import the services (all services are decorated with @Service()";
|
|
26
|
+
* import MyService1 from "./services/service1";
|
|
27
|
+
* import MyService2 from "./services/service2";
|
|
28
|
+
* import MyService3 from "./services/service3";
|
|
29
|
+
*
|
|
30
|
+
* // When all services is imported you can load InjectorService.
|
|
31
|
+
* const injector = new InjectorService()
|
|
32
|
+
*
|
|
33
|
+
* await injector.load();
|
|
34
|
+
*
|
|
35
|
+
* const myService1 = injector.get<MyService1>(MyServcice1);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
let InjectorService = InjectorService_1 = class InjectorService extends Container {
|
|
39
|
+
constructor() {
|
|
40
|
+
super();
|
|
41
|
+
this.settings = new DIConfiguration();
|
|
42
|
+
this.logger = console;
|
|
43
|
+
this.resolvedConfiguration = false;
|
|
44
|
+
_InjectorService_cache.set(this, new LocalsContainer());
|
|
45
|
+
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(InjectorService_1, this);
|
|
46
|
+
}
|
|
47
|
+
get resolvers() {
|
|
48
|
+
return this.settings.resolvers;
|
|
49
|
+
}
|
|
50
|
+
get scopes() {
|
|
51
|
+
return this.settings.scopes || {};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Retrieve default scope for a given provider.
|
|
55
|
+
* @param provider
|
|
56
|
+
*/
|
|
57
|
+
scopeOf(provider) {
|
|
58
|
+
return provider.scope || this.scopes[provider.type] || ProviderScope.SINGLETON;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
|
|
62
|
+
* @param token
|
|
63
|
+
* @param settings
|
|
64
|
+
* @deprecated
|
|
65
|
+
*/
|
|
66
|
+
forkProvider(token, settings = {}) {
|
|
67
|
+
if (!this.hasProvider(token)) {
|
|
68
|
+
this.addProvider(token);
|
|
69
|
+
}
|
|
70
|
+
const provider = this.getProvider(token);
|
|
71
|
+
Object.assign(provider, settings);
|
|
72
|
+
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, this.invoke(token));
|
|
73
|
+
return provider;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Return a list of instance build by the injector.
|
|
77
|
+
*/
|
|
78
|
+
toArray() {
|
|
79
|
+
return __classPrivateFieldGet(this, _InjectorService_cache, "f").toArray();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get a service or factory already constructed from his symbol or class.
|
|
83
|
+
*
|
|
84
|
+
* #### Example
|
|
85
|
+
*
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import {InjectorService} from "@tsed/di";
|
|
88
|
+
* import MyService from "./services";
|
|
89
|
+
*
|
|
90
|
+
* class OtherService {
|
|
91
|
+
* constructor(injectorService: InjectorService) {
|
|
92
|
+
* const myService = injectorService.get<MyService>(MyService);
|
|
93
|
+
* }
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @param token The class or symbol registered in InjectorService.
|
|
98
|
+
* @param options
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
get(token, options = {}) {
|
|
102
|
+
const instance = this.getInstance(token);
|
|
103
|
+
if (instance !== undefined) {
|
|
104
|
+
return instance;
|
|
105
|
+
}
|
|
106
|
+
if (!this.hasProvider(token)) {
|
|
107
|
+
for (const resolver of this.resolvers) {
|
|
108
|
+
const result = resolver.get(token, options);
|
|
109
|
+
if (result !== undefined) {
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The has() method returns a boolean indicating whether an element with the specified key exists or not.
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
* @param token
|
|
119
|
+
*/
|
|
120
|
+
has(token) {
|
|
121
|
+
return __classPrivateFieldGet(this, _InjectorService_cache, "f").get(token) !== undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Invoke the class and inject all services that required by the class constructor.
|
|
125
|
+
*
|
|
126
|
+
* #### Example
|
|
127
|
+
*
|
|
128
|
+
* ```typescript
|
|
129
|
+
* import {InjectorService} from "@tsed/di";
|
|
130
|
+
* import MyService from "./services";
|
|
131
|
+
*
|
|
132
|
+
* class OtherService {
|
|
133
|
+
* constructor(injectorService: InjectorService) {
|
|
134
|
+
* const myService = injectorService.invoke<MyService>(MyService);
|
|
135
|
+
* }
|
|
136
|
+
* }
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @param token The injectable class to invoke. Class parameters are injected according constructor signature.
|
|
140
|
+
* @param locals Optional object. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
|
|
141
|
+
* @param options
|
|
142
|
+
* @returns {T} The class constructed.
|
|
143
|
+
*/
|
|
144
|
+
invoke(token, locals = new LocalsContainer(), options = {}) {
|
|
145
|
+
const provider = this.ensureProvider(token);
|
|
146
|
+
let instance;
|
|
147
|
+
!locals.has(Configuration) && locals.set(Configuration, this.settings);
|
|
148
|
+
if (locals.has(token)) {
|
|
149
|
+
return locals.get(token);
|
|
150
|
+
}
|
|
151
|
+
if (token === DI_PARAM_OPTIONS) {
|
|
152
|
+
return {};
|
|
153
|
+
}
|
|
154
|
+
if (!provider || options.rebuild) {
|
|
155
|
+
instance = this.resolve(token, locals, options);
|
|
156
|
+
if (this.hasProvider(token)) {
|
|
157
|
+
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, instance);
|
|
158
|
+
}
|
|
159
|
+
return instance;
|
|
160
|
+
}
|
|
161
|
+
switch (this.scopeOf(provider)) {
|
|
162
|
+
case ProviderScope.SINGLETON:
|
|
163
|
+
if (!this.has(token)) {
|
|
164
|
+
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, this.resolve(token, locals, options));
|
|
165
|
+
if (provider.isAsync()) {
|
|
166
|
+
__classPrivateFieldGet(this, _InjectorService_cache, "f").get(token).then((instance) => {
|
|
167
|
+
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, instance);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
instance = this.get(token);
|
|
172
|
+
break;
|
|
173
|
+
case ProviderScope.REQUEST:
|
|
174
|
+
instance = this.resolve(token, locals, options);
|
|
175
|
+
locals.set(token, instance);
|
|
176
|
+
break;
|
|
177
|
+
case 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()) {
|
|
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
|
+
const instance = __classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
|
|
193
|
+
if (instance !== undefined) {
|
|
194
|
+
locals.set(provider.token, instance);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return locals;
|
|
199
|
+
}
|
|
200
|
+
loadSync(locals = new LocalsContainer()) {
|
|
201
|
+
for (const [, provider] of this) {
|
|
202
|
+
if (!locals.has(provider.token) && this.scopeOf(provider) === ProviderScope.SINGLETON) {
|
|
203
|
+
this.invoke(provider.token, locals);
|
|
204
|
+
}
|
|
205
|
+
const instance = __classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
|
|
206
|
+
if (instance !== undefined) {
|
|
207
|
+
locals.set(provider.token, instance);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return locals;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Boostrap injector from container and resolve configuration.
|
|
214
|
+
*
|
|
215
|
+
* @param container
|
|
216
|
+
*/
|
|
217
|
+
bootstrap(container = createContainer()) {
|
|
218
|
+
// Clone all providers in the container
|
|
219
|
+
this.addProviders(container);
|
|
220
|
+
// Resolve all configuration
|
|
221
|
+
this.resolveConfiguration();
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
|
|
226
|
+
*
|
|
227
|
+
* @param container
|
|
228
|
+
* @param rootModule
|
|
229
|
+
*/
|
|
230
|
+
async load(container = createContainer(), rootModule) {
|
|
231
|
+
this.bootstrap(container);
|
|
232
|
+
// build async and sync provider
|
|
233
|
+
let locals = await this.loadAsync();
|
|
234
|
+
if (rootModule) {
|
|
235
|
+
await this.invoke(rootModule);
|
|
236
|
+
}
|
|
237
|
+
// load sync provider
|
|
238
|
+
locals = this.loadSync(locals);
|
|
239
|
+
await locals.emit("$beforeInit");
|
|
240
|
+
await locals.emit("$onInit");
|
|
241
|
+
return locals;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Load all configurations registered on providers
|
|
245
|
+
*/
|
|
246
|
+
resolveConfiguration() {
|
|
247
|
+
if (this.resolvedConfiguration) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const mergedConfiguration = new Map();
|
|
251
|
+
super.forEach((provider) => {
|
|
252
|
+
if (provider.configuration && provider.type !== "server:module") {
|
|
253
|
+
Object.entries(provider.configuration).forEach(([key, value]) => {
|
|
254
|
+
value = mergedConfiguration.has(key) ? deepMerge(mergedConfiguration.get(key), value) : deepClone(value);
|
|
255
|
+
mergedConfiguration.set(key, value);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
if (provider.resolvers) {
|
|
259
|
+
this.resolvers.push(...provider.resolvers);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
mergedConfiguration.forEach((value, key) => {
|
|
263
|
+
this.settings[key] = deepMerge(value, this.settings[key]);
|
|
264
|
+
});
|
|
265
|
+
this.settings.build();
|
|
266
|
+
this.resolvedConfiguration = true;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
*
|
|
270
|
+
* @param instance
|
|
271
|
+
* @param locals
|
|
272
|
+
* @param options
|
|
273
|
+
*/
|
|
274
|
+
bindInjectableProperties(instance, locals, options) {
|
|
275
|
+
const properties = ancestorsOf(classOf(instance)).reduce((properties, target) => {
|
|
276
|
+
const store = Store.from(target);
|
|
277
|
+
return {
|
|
278
|
+
...properties,
|
|
279
|
+
...(store.get(INJECTABLE_PROP) || {})
|
|
280
|
+
};
|
|
281
|
+
}, {});
|
|
282
|
+
Object.values(properties).forEach((definition) => {
|
|
283
|
+
switch (definition.bindingType) {
|
|
284
|
+
case InjectablePropertyType.METHOD:
|
|
285
|
+
this.bindMethod(instance, definition);
|
|
286
|
+
break;
|
|
287
|
+
case InjectablePropertyType.PROPERTY:
|
|
288
|
+
this.bindProperty(instance, definition, locals, options);
|
|
289
|
+
break;
|
|
290
|
+
case InjectablePropertyType.CONSTANT:
|
|
291
|
+
this.bindConstant(instance, definition);
|
|
292
|
+
break;
|
|
293
|
+
case InjectablePropertyType.VALUE:
|
|
294
|
+
this.bindValue(instance, definition);
|
|
295
|
+
break;
|
|
296
|
+
case InjectablePropertyType.INTERCEPTOR:
|
|
297
|
+
this.bindInterceptor(instance, definition);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
*
|
|
304
|
+
* @param instance
|
|
305
|
+
* @param {string} propertyKey
|
|
306
|
+
*/
|
|
307
|
+
bindMethod(instance, { propertyKey }) {
|
|
308
|
+
const target = classOf(instance);
|
|
309
|
+
const originalMethod = instance[propertyKey];
|
|
310
|
+
const deps = Metadata.getParamTypes(prototypeOf(target), propertyKey);
|
|
311
|
+
instance[propertyKey] = () => {
|
|
312
|
+
const services = deps.map((dependency) => this.get(dependency));
|
|
313
|
+
return originalMethod.call(instance, ...services);
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Create an injectable property.
|
|
318
|
+
*
|
|
319
|
+
* @param instance
|
|
320
|
+
* @param {string} propertyKey
|
|
321
|
+
* @param {any} useType
|
|
322
|
+
* @param resolver
|
|
323
|
+
* @param options
|
|
324
|
+
* @param locals
|
|
325
|
+
* @param invokeOptions
|
|
326
|
+
*/
|
|
327
|
+
bindProperty(instance, { propertyKey, resolver, options = {} }, locals, invokeOptions) {
|
|
328
|
+
let get;
|
|
329
|
+
get = resolver(this, locals, { ...invokeOptions, options });
|
|
330
|
+
catchError(() => Object.defineProperty(instance, propertyKey, {
|
|
331
|
+
get
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
*
|
|
336
|
+
* @param instance
|
|
337
|
+
* @param {string} propertyKey
|
|
338
|
+
* @param {any} useType
|
|
339
|
+
*/
|
|
340
|
+
bindValue(instance, { propertyKey, expression, defaultValue }) {
|
|
341
|
+
const descriptor = {
|
|
342
|
+
get: () => this.settings.get(expression) || defaultValue,
|
|
343
|
+
set: (value) => this.settings.set(expression, value),
|
|
344
|
+
enumerable: true,
|
|
345
|
+
configurable: true
|
|
346
|
+
};
|
|
347
|
+
catchError(() => Object.defineProperty(instance, propertyKey, descriptor));
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
*
|
|
351
|
+
* @param instance
|
|
352
|
+
* @param {string} propertyKey
|
|
353
|
+
* @param {any} useType
|
|
354
|
+
*/
|
|
355
|
+
bindConstant(instance, { propertyKey, expression, defaultValue }) {
|
|
356
|
+
let bean;
|
|
357
|
+
const get = () => {
|
|
358
|
+
if (bean !== undefined) {
|
|
359
|
+
return bean;
|
|
360
|
+
}
|
|
361
|
+
const value = this.settings.get(expression, defaultValue);
|
|
362
|
+
bean = Object.freeze(deepClone(value));
|
|
363
|
+
return bean;
|
|
364
|
+
};
|
|
365
|
+
const descriptor = {
|
|
366
|
+
get,
|
|
367
|
+
enumerable: true,
|
|
368
|
+
configurable: true
|
|
369
|
+
};
|
|
370
|
+
catchError(() => Object.defineProperty(instance, propertyKey, descriptor));
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
*
|
|
374
|
+
* @param instance
|
|
375
|
+
* @param propertyKey
|
|
376
|
+
* @param useType
|
|
377
|
+
* @param options
|
|
378
|
+
*/
|
|
379
|
+
bindInterceptor(instance, { propertyKey, useType, options }) {
|
|
380
|
+
const target = classOf(instance);
|
|
381
|
+
const originalMethod = instance[propertyKey];
|
|
382
|
+
instance[propertyKey] = (...args) => {
|
|
383
|
+
const next = (err) => {
|
|
384
|
+
if (!err) {
|
|
385
|
+
return originalMethod.apply(instance, args);
|
|
386
|
+
}
|
|
387
|
+
throw err;
|
|
388
|
+
};
|
|
389
|
+
const context = {
|
|
390
|
+
target,
|
|
391
|
+
propertyKey,
|
|
392
|
+
args,
|
|
393
|
+
options,
|
|
394
|
+
next
|
|
395
|
+
};
|
|
396
|
+
const interceptor = this.get(useType);
|
|
397
|
+
return interceptor.intercept({
|
|
398
|
+
...context,
|
|
399
|
+
options
|
|
400
|
+
}, next);
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Allow handler hack for AsyncHookContext plugin.
|
|
405
|
+
* @param ctx
|
|
406
|
+
* @param cb
|
|
407
|
+
* @protected
|
|
408
|
+
*/
|
|
409
|
+
runInContext(ctx, cb) {
|
|
410
|
+
return cb();
|
|
411
|
+
}
|
|
412
|
+
async lazyInvoke(token) {
|
|
413
|
+
let instance = this.getInstance(token);
|
|
414
|
+
if (!instance) {
|
|
415
|
+
instance = await this.invoke(token);
|
|
416
|
+
if (isFunction(instance === null || instance === void 0 ? void 0 : instance.$onInit)) {
|
|
417
|
+
await instance.$onInit();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return instance;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
|
|
424
|
+
* @param eventName The event name to emit at all services.
|
|
425
|
+
* @param args List of the parameters to give to each services.
|
|
426
|
+
* @returns {Promise<any[]>} A list of promises.
|
|
427
|
+
*/
|
|
428
|
+
async emit(eventName, ...args) {
|
|
429
|
+
return __classPrivateFieldGet(this, _InjectorService_cache, "f").emit(eventName, ...args);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* @param eventName
|
|
433
|
+
* @param value
|
|
434
|
+
* @param args
|
|
435
|
+
*/
|
|
436
|
+
alter(eventName, value, ...args) {
|
|
437
|
+
return __classPrivateFieldGet(this, _InjectorService_cache, "f").alter(eventName, value, ...args);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* @param eventName
|
|
441
|
+
* @param value
|
|
442
|
+
* @param args
|
|
443
|
+
*/
|
|
444
|
+
async alterAsync(eventName, value, ...args) {
|
|
445
|
+
return __classPrivateFieldGet(this, _InjectorService_cache, "f").alterAsync(eventName, value, ...args);
|
|
446
|
+
}
|
|
447
|
+
async destroy() {
|
|
448
|
+
await __classPrivateFieldGet(this, _InjectorService_cache, "f").destroy();
|
|
449
|
+
}
|
|
450
|
+
ensureProvider(token) {
|
|
451
|
+
if (!this.hasProvider(token) && GlobalProviders.has(token)) {
|
|
452
|
+
this.addProvider(token);
|
|
453
|
+
}
|
|
454
|
+
return this.getProvider(token);
|
|
455
|
+
}
|
|
456
|
+
getInstance(token) {
|
|
457
|
+
return __classPrivateFieldGet(this, _InjectorService_cache, "f").get(token);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Invoke a class method and inject service.
|
|
461
|
+
*
|
|
462
|
+
* #### IInjectableMethod options
|
|
463
|
+
*
|
|
464
|
+
* * **target**: Optional. The class instance.
|
|
465
|
+
* * **methodName**: `string` Optional. The method name.
|
|
466
|
+
* * **designParamTypes**: `any[]` Optional. List of injectable types.
|
|
467
|
+
* * **locals**: `Map<Function, any>` Optional. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
|
|
468
|
+
*
|
|
469
|
+
* #### Example
|
|
470
|
+
*
|
|
471
|
+
* @param target
|
|
472
|
+
* @param locals
|
|
473
|
+
* @param options
|
|
474
|
+
* @private
|
|
475
|
+
*/
|
|
476
|
+
resolve(target, locals, options = {}) {
|
|
477
|
+
const resolvedOpts = this.mapInvokeOptions(target, locals, options);
|
|
478
|
+
const { token, deps, construct, imports, provider } = resolvedOpts;
|
|
479
|
+
if (provider) {
|
|
480
|
+
GlobalProviders.onInvoke(provider, locals, { ...resolvedOpts, injector: this });
|
|
481
|
+
}
|
|
482
|
+
let instance;
|
|
483
|
+
let currentDependency = false;
|
|
484
|
+
try {
|
|
485
|
+
const invokeDependency = (parent) => (token, index) => {
|
|
486
|
+
var _a;
|
|
487
|
+
currentDependency = { token, index, deps };
|
|
488
|
+
if (token !== DI_PARAM_OPTIONS) {
|
|
489
|
+
const options = (_a = provider === null || provider === void 0 ? void 0 : provider.store) === null || _a === void 0 ? void 0 : _a.get(`${DI_PARAM_OPTIONS}:${index}`);
|
|
490
|
+
locals.set(DI_PARAM_OPTIONS, options || {});
|
|
491
|
+
}
|
|
492
|
+
return isInheritedFrom(token, Provider, 1) ? provider : this.invoke(token, locals, { parent });
|
|
493
|
+
};
|
|
494
|
+
// Invoke manually imported providers
|
|
495
|
+
imports.forEach(invokeDependency());
|
|
496
|
+
// Inject dependencies
|
|
497
|
+
const services = deps.map(invokeDependency(token));
|
|
498
|
+
currentDependency = false;
|
|
499
|
+
instance = construct(services);
|
|
500
|
+
}
|
|
501
|
+
catch (error) {
|
|
502
|
+
InjectionError.throwInjectorError(token, currentDependency, error);
|
|
503
|
+
}
|
|
504
|
+
if (instance === undefined) {
|
|
505
|
+
throw new InjectionError(token, `Unable to create new instance from undefined value. Check your provider declaration for ${nameOf(token)}`);
|
|
506
|
+
}
|
|
507
|
+
if (instance && isClass(classOf(instance))) {
|
|
508
|
+
this.bindInjectableProperties(instance, locals, options);
|
|
509
|
+
}
|
|
510
|
+
return instance;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Create options to invoke a provider or class.
|
|
514
|
+
* @param token
|
|
515
|
+
* @param locals
|
|
516
|
+
* @param options
|
|
517
|
+
*/
|
|
518
|
+
mapInvokeOptions(token, locals, options) {
|
|
519
|
+
let imports = options.imports;
|
|
520
|
+
let deps = options.deps;
|
|
521
|
+
let scope = options.scope;
|
|
522
|
+
let construct;
|
|
523
|
+
if (!token) {
|
|
524
|
+
throw new UndefinedTokenError();
|
|
525
|
+
}
|
|
526
|
+
let provider;
|
|
527
|
+
if (!this.hasProvider(token)) {
|
|
528
|
+
provider = new Provider(token);
|
|
529
|
+
this.resolvers.forEach((resolver) => {
|
|
530
|
+
const result = resolver.get(token, locals.get(DI_PARAM_OPTIONS));
|
|
531
|
+
if (result !== undefined) {
|
|
532
|
+
provider.useFactory = () => result;
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
provider = this.getProvider(token);
|
|
538
|
+
}
|
|
539
|
+
scope = scope || this.scopeOf(provider);
|
|
540
|
+
deps = deps || provider.deps;
|
|
541
|
+
imports = imports || provider.imports;
|
|
542
|
+
if (provider.useValue !== undefined) {
|
|
543
|
+
construct = () => (isFunction(provider.useValue) ? provider.useValue() : provider.useValue);
|
|
544
|
+
}
|
|
545
|
+
else if (provider.useFactory) {
|
|
546
|
+
construct = (deps) => provider.useFactory(...deps);
|
|
547
|
+
}
|
|
548
|
+
else if (provider.useAsyncFactory) {
|
|
549
|
+
construct = async (deps) => {
|
|
550
|
+
deps = await Promise.all(deps);
|
|
551
|
+
return provider.useAsyncFactory(...deps);
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
// useClass
|
|
556
|
+
deps = deps || Metadata.getParamTypes(provider.useClass);
|
|
557
|
+
construct = (deps) => new provider.useClass(...deps);
|
|
558
|
+
}
|
|
559
|
+
return {
|
|
560
|
+
token,
|
|
561
|
+
scope: scope || Store.from(token).get("scope") || ProviderScope.SINGLETON,
|
|
562
|
+
deps: deps || [],
|
|
563
|
+
imports: imports || [],
|
|
564
|
+
construct,
|
|
565
|
+
provider
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
_InjectorService_cache = new WeakMap();
|
|
570
|
+
InjectorService = InjectorService_1 = __decorate([
|
|
571
|
+
Injectable({
|
|
572
|
+
scope: ProviderScope.SINGLETON,
|
|
573
|
+
global: true
|
|
574
|
+
}),
|
|
575
|
+
__metadata("design:paramtypes", [])
|
|
576
|
+
], InjectorService);
|
|
577
|
+
export { InjectorService };
|
|
578
|
+
//# sourceMappingURL=InjectorService.js.map
|