@tsed/di 7.0.0-beta.9 → 7.0.0-rc.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/coverage.json +6 -0
- package/jest.config.js +9 -0
- package/lib/cjs/decorators/controller.js.map +1 -1
- package/lib/cjs/decorators/inject.js +2 -2
- package/lib/cjs/decorators/inject.js.map +1 -1
- package/lib/cjs/domain/Container.js +1 -2
- package/lib/cjs/domain/Container.js.map +1 -1
- package/lib/cjs/domain/ContextLogger.js +41 -30
- package/lib/cjs/domain/ContextLogger.js.map +1 -1
- package/lib/cjs/domain/ControllerProvider.js +37 -0
- package/lib/cjs/domain/ControllerProvider.js.map +1 -0
- package/lib/cjs/domain/DIContext.js +86 -30
- package/lib/cjs/domain/DIContext.js.map +1 -1
- package/lib/cjs/domain/LocalsContainer.js +3 -46
- package/lib/cjs/domain/LocalsContainer.js.map +1 -1
- package/lib/cjs/domain/Provider.js +20 -1
- package/lib/cjs/domain/Provider.js.map +1 -1
- package/lib/cjs/errors/InjectionError.js +3 -2
- package/lib/cjs/errors/InjectionError.js.map +1 -1
- package/lib/cjs/errors/UndefinedTokenError.js +1 -1
- package/lib/cjs/errors/UndefinedTokenError.js.map +1 -1
- package/lib/cjs/index.js +8 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/AlterRunInContext.js +3 -0
- package/lib/cjs/interfaces/AlterRunInContext.js.map +1 -0
- package/lib/cjs/interfaces/DIConfigurationOptions.js.map +1 -1
- package/lib/cjs/interfaces/DILoggerOptions.js +3 -0
- package/lib/cjs/interfaces/DILoggerOptions.js.map +1 -0
- package/lib/cjs/interfaces/InjectableProperties.js.map +1 -1
- package/lib/cjs/interfaces/InterceptorMethods.js.map +1 -1
- package/lib/cjs/interfaces/TokenProvider.js.map +1 -1
- package/lib/cjs/interfaces/TokenRoute.js +3 -0
- package/lib/cjs/interfaces/TokenRoute.js.map +1 -0
- package/lib/cjs/registries/GlobalProviders.js +7 -12
- package/lib/cjs/registries/GlobalProviders.js.map +1 -1
- package/lib/cjs/registries/ProviderRegistry.js +1 -1
- package/lib/cjs/registries/ProviderRegistry.js.map +1 -1
- package/lib/cjs/services/DIConfiguration.js +56 -43
- package/lib/cjs/services/DIConfiguration.js.map +1 -1
- package/lib/cjs/services/DITest.js +5 -6
- package/lib/cjs/services/DITest.js.map +1 -1
- package/lib/cjs/services/InjectorService.js +94 -92
- package/lib/cjs/services/InjectorService.js.map +1 -1
- package/lib/cjs/utils/asyncHookContext.js +42 -0
- package/lib/cjs/utils/asyncHookContext.js.map +1 -0
- package/lib/cjs/utils/getConfiguration.js +13 -10
- package/lib/cjs/utils/getConfiguration.js.map +1 -1
- package/lib/cjs/utils/resolveControllers.js +47 -0
- package/lib/cjs/utils/resolveControllers.js.map +1 -0
- package/lib/cjs/utils/setLoggerConfiguration.js +11 -0
- package/lib/cjs/utils/setLoggerConfiguration.js.map +1 -0
- package/lib/cjs/utils/setLoggerFormat.js +33 -0
- package/lib/cjs/utils/setLoggerFormat.js.map +1 -0
- package/lib/cjs/utils/setLoggerLevel.js +1 -2
- package/lib/cjs/utils/setLoggerLevel.js.map +1 -1
- package/lib/esm/decorators/controller.js.map +1 -1
- package/lib/esm/decorators/inject.js +1 -1
- package/lib/esm/decorators/inject.js.map +1 -1
- package/lib/esm/domain/Container.js +1 -2
- package/lib/esm/domain/Container.js.map +1 -1
- package/lib/esm/domain/ContextLogger.js +41 -30
- package/lib/esm/domain/ContextLogger.js.map +1 -1
- package/lib/esm/domain/ControllerProvider.js +33 -0
- package/lib/esm/domain/ControllerProvider.js.map +1 -0
- package/lib/esm/domain/DIContext.js +86 -30
- package/lib/esm/domain/DIContext.js.map +1 -1
- package/lib/esm/domain/LocalsContainer.js +3 -46
- package/lib/esm/domain/LocalsContainer.js.map +1 -1
- package/lib/esm/domain/Provider.js +21 -2
- package/lib/esm/domain/Provider.js.map +1 -1
- package/lib/esm/errors/InjectionError.js +3 -2
- package/lib/esm/errors/InjectionError.js.map +1 -1
- package/lib/esm/errors/UndefinedTokenError.js +1 -1
- package/lib/esm/errors/UndefinedTokenError.js.map +1 -1
- package/lib/esm/index.js +8 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/AlterRunInContext.js +2 -0
- package/lib/esm/interfaces/AlterRunInContext.js.map +1 -0
- package/lib/esm/interfaces/DIConfigurationOptions.js.map +1 -1
- package/lib/esm/interfaces/DILoggerOptions.js +2 -0
- package/lib/esm/interfaces/DILoggerOptions.js.map +1 -0
- package/lib/esm/interfaces/InjectableProperties.js.map +1 -1
- package/lib/esm/interfaces/InterceptorMethods.js.map +1 -1
- package/lib/esm/interfaces/TokenProvider.js.map +1 -1
- package/lib/esm/interfaces/TokenRoute.js +2 -0
- package/lib/esm/interfaces/TokenRoute.js.map +1 -0
- package/lib/esm/registries/GlobalProviders.js +7 -12
- package/lib/esm/registries/GlobalProviders.js.map +1 -1
- package/lib/esm/registries/ProviderRegistry.js +1 -1
- package/lib/esm/registries/ProviderRegistry.js.map +1 -1
- package/lib/esm/services/DIConfiguration.js +57 -44
- package/lib/esm/services/DIConfiguration.js.map +1 -1
- package/lib/esm/services/DITest.js +5 -6
- package/lib/esm/services/DITest.js.map +1 -1
- package/lib/esm/services/InjectorService.js +96 -94
- package/lib/esm/services/InjectorService.js.map +1 -1
- package/lib/esm/utils/asyncHookContext.js +33 -0
- package/lib/esm/utils/asyncHookContext.js.map +1 -0
- package/lib/esm/utils/getConfiguration.js +12 -10
- package/lib/esm/utils/getConfiguration.js.map +1 -1
- package/lib/esm/utils/resolveControllers.js +42 -0
- package/lib/esm/utils/resolveControllers.js.map +1 -0
- package/lib/esm/utils/setLoggerConfiguration.js +7 -0
- package/lib/esm/utils/setLoggerConfiguration.js.map +1 -0
- package/lib/esm/utils/setLoggerFormat.js +29 -0
- package/lib/esm/utils/setLoggerFormat.js.map +1 -0
- package/lib/esm/utils/setLoggerLevel.js +1 -2
- package/lib/esm/utils/setLoggerLevel.js.map +1 -1
- package/lib/types/decorators/controller.d.ts +0 -1
- package/lib/types/domain/ContextLogger.d.ts +13 -18
- package/lib/types/domain/ControllerProvider.d.ts +17 -0
- package/lib/types/domain/DIContext.d.ts +23 -29
- package/lib/types/domain/LocalsContainer.d.ts +3 -22
- package/lib/types/domain/Provider.d.ts +2 -1
- package/lib/types/index.d.ts +8 -1
- package/lib/types/interfaces/AlterRunInContext.d.ts +3 -0
- package/lib/types/interfaces/DIConfigurationOptions.d.ts +5 -2
- package/lib/types/interfaces/DILoggerOptions.d.ts +44 -0
- package/lib/types/interfaces/InjectableProperties.d.ts +2 -1
- package/lib/types/interfaces/InterceptorMethods.d.ts +1 -1
- package/lib/types/interfaces/TokenProvider.d.ts +1 -1
- package/lib/types/interfaces/TokenRoute.d.ts +5 -0
- package/lib/types/registries/GlobalProviders.d.ts +6 -5
- package/lib/types/registries/ProviderRegistry.d.ts +2 -1
- package/lib/types/services/DIConfiguration.d.ts +19 -11
- package/lib/types/services/InjectorService.d.ts +20 -28
- package/lib/types/utils/asyncHookContext.d.ts +14 -0
- package/lib/types/utils/getConfiguration.d.ts +1 -0
- package/lib/types/utils/resolveControllers.d.ts +12 -0
- package/lib/types/utils/setLoggerConfiguration.d.ts +2 -0
- package/lib/types/utils/setLoggerFormat.d.ts +6 -0
- package/lib/types/utils/setLoggerLevel.d.ts +1 -1
- package/package.json +15 -11
- package/readme.md +3 -2
- package/lib/cjs/utils/runInContext.js +0 -20
- package/lib/cjs/utils/runInContext.js.map +0 -1
- package/lib/esm/utils/runInContext.js +0 -14
- package/lib/esm/utils/runInContext.js.map +0 -1
- package/lib/types/utils/runInContext.d.ts +0 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../../src/services/DIConfiguration.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"DIConfiguration.js","sourceRoot":"","sources":["../../../src/services/DIConfiguration.ts"],"names":[],"mappings":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;IACd,CAAC;IAED,MAAM,CAAC,WAAmB,EAAE,KAAU;QACpC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC;QAEvC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAU,WAAmB,EAAE,YAAgB;QAChD,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IAChD,CAAC;IAES,MAAM,CAAC,WAAmB,EAAE,YAAkB;QACtD,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QAE9C,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,OAAO,KAAK,CAAC;SACd;QAED,OAAO,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IAC3D,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,KAAU;QAChB,OAAO,KAAK,CAAC,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACnD,CAAC;CACF","sourcesContent":["import {classOf, Env, getValue, proxyDelegation, setValue} from \"@tsed/core\";\nimport type {ProviderScope} from \"../domain/ProviderScope\";\nimport type {DILoggerOptions} from \"../interfaces/DILoggerOptions\";\nimport type {DIResolver} from \"../interfaces/DIResolver\";\nimport type {TokenProvider} from \"../interfaces/TokenProvider\";\nimport type {TokenRoute} from \"../interfaces/TokenRoute\";\n\nexport class DIConfiguration {\n readonly default: Map<string, any> = new Map();\n protected map: Map<string, any> = new Map();\n\n constructor(initialProps = {}) {\n Object.entries({\n scopes: {},\n resolvers: [],\n imports: [],\n routes: [],\n logger: {},\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 version() {\n return this.get(\"version\");\n }\n\n set version(v: string) {\n this.map.set(\"version\", v);\n }\n\n get rootDir() {\n return this.get(\"rootDir\");\n }\n\n set rootDir(value: string) {\n this.map.set(\"rootDir\", value);\n }\n\n get env(): Env {\n return this.map.get(\"env\");\n }\n\n set env(value: Env) {\n this.map.set(\"env\", value);\n }\n\n get scopes(): Record<string, ProviderScope> {\n return this.map.get(\"scopes\");\n }\n\n set scopes(value: Record<string, ProviderScope>) {\n this.map.set(\"scopes\", value);\n }\n\n get resolvers(): DIResolver[] {\n return this.getRaw(\"resolvers\");\n }\n\n set resolvers(resolvers: DIResolver[]) {\n this.map.set(\"resolvers\", resolvers);\n }\n\n get imports(): TokenProvider[] {\n return this.get(\"imports\");\n }\n\n set imports(imports: TokenProvider[]) {\n this.map.set(\"imports\", imports);\n }\n\n get routes(): TokenRoute[] {\n return this.get(\"routes\");\n }\n\n set routes(routes: TokenRoute[]) {\n this.map.set(\"routes\", routes);\n }\n\n get logger(): Partial<DILoggerOptions> {\n return this.get(\"logger\");\n }\n\n set logger(value: Partial<DILoggerOptions>) {\n const logger = {...this.logger, ...value};\n this.map.set(\"logger\", logger);\n }\n\n get debug(): boolean {\n return this.logger.level === \"debug\";\n }\n\n set debug(debug: boolean) {\n this.logger = {...this.logger, level: debug ? \"debug\" : \"info\"};\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 if (propertyKey in this) {\n // @ts-ignore\n this[propertyKey] = value;\n } else {\n this.setRaw(propertyKey, value);\n }\n } else {\n Object.entries(propertyKey).forEach(([key, value]) => {\n this.set(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.getRaw(propertyKey, defaultValue);\n }\n\n protected 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 /**\n *\n * @param value\n * @returns {any}\n */\n resolve(value: any) {\n return value.replace(\"${rootDir}\", this.rootDir);\n }\n}\n"]}
|
|
@@ -2,13 +2,14 @@ import { Env, getValue } from "@tsed/core";
|
|
|
2
2
|
import { $log } from "@tsed/logger";
|
|
3
3
|
import { createContainer } from "../utils/createContainer.js";
|
|
4
4
|
import { LocalsContainer } from "../domain/LocalsContainer.js";
|
|
5
|
-
import {
|
|
5
|
+
import { setLoggerConfiguration } from "../utils/setLoggerConfiguration.js";
|
|
6
6
|
import { InjectorService } from "./InjectorService.js";
|
|
7
7
|
import { DIContext } from "../domain/DIContext.js";
|
|
8
8
|
/**
|
|
9
9
|
* Tool to run test with lightweight DI sandbox.
|
|
10
10
|
*/
|
|
11
11
|
export class DITest {
|
|
12
|
+
static _injector = null;
|
|
12
13
|
static get injector() {
|
|
13
14
|
if (DITest._injector) {
|
|
14
15
|
return DITest._injector;
|
|
@@ -29,12 +30,11 @@ export class DITest {
|
|
|
29
30
|
}
|
|
30
31
|
static async create(settings = {}) {
|
|
31
32
|
DITest.injector = DITest.createInjector(settings);
|
|
32
|
-
await
|
|
33
|
+
await DITest.createContainer(settings);
|
|
33
34
|
}
|
|
34
35
|
static async createContainer(settings = {}) {
|
|
35
|
-
var _a;
|
|
36
36
|
const container = createContainer();
|
|
37
|
-
|
|
37
|
+
settings.imports?.forEach(({ token, use }) => {
|
|
38
38
|
container.addProvider(token, {
|
|
39
39
|
useValue: use
|
|
40
40
|
});
|
|
@@ -49,7 +49,7 @@ export class DITest {
|
|
|
49
49
|
injector.logger = $log;
|
|
50
50
|
// @ts-ignore
|
|
51
51
|
injector.settings.set(DITest.configure(settings));
|
|
52
|
-
|
|
52
|
+
setLoggerConfiguration(injector);
|
|
53
53
|
return injector;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
@@ -108,5 +108,4 @@ export class DITest {
|
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
DITest._injector = null;
|
|
112
111
|
//# sourceMappingURL=DITest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../src/services/DITest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAC,eAAe,EAAC,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAC,eAAe,EAAC,MAAM,2BAA2B,CAAC;AAG1D,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"DITest.js","sourceRoot":"","sources":["../../../src/services/DITest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACzC,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAC,eAAe,EAAC,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAC,eAAe,EAAC,MAAM,2BAA2B,CAAC;AAG1D,OAAO,EAAC,sBAAsB,EAAC,MAAM,iCAAiC,CAAC;AAEvE,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAC,SAAS,EAAC,MAAM,qBAAqB,CAAC;AAW9C;;GAEG;AACH,MAAM,OAAO,MAAM;IACP,MAAM,CAAC,SAAS,GAA2B,IAAI,CAAC;IAE1D,MAAM,KAAK,QAAQ;QACjB,IAAI,MAAM,CAAC,SAAS,EAAE;YAC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EAAE,IAAI;YACR,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM;SAC/B,CAAC,CAAC;IACL,CAAC;IAES,MAAM,CAAC,SAAS,CAAC,WAAwC,EAAE;QACnE,OAAO;YACL,GAAG,QAAQ;YACX,GAAG,EAAE,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC;YACxC,MAAM,EAAE;gBACN,GAAG,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,CAAC;gBACnC,KAAK,EAAE,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,CAAC;aACjD;SACF,CAAC;IACJ,CAAC","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 {setLoggerConfiguration} from \"../utils/setLoggerConfiguration\";\nimport {setLoggerLevel} from \"../utils/setLoggerLevel\";\nimport {InjectorService} from \"./InjectorService\";\nimport {DIContext} from \"../domain/DIContext\";\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 DITest.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 setLoggerConfiguration(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 static createDIContext() {\n return new DIContext({\n id: \"id\",\n injector: DITest.injector,\n logger: DITest.injector.logger\n });\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,20 +1,20 @@
|
|
|
1
|
-
var _InjectorService_cache;
|
|
2
1
|
var InjectorService_1;
|
|
3
|
-
import {
|
|
4
|
-
import { ancestorsOf, catchError, classOf, deepClone, deepMerge, isClass, isFunction, isInheritedFrom, Metadata, nameOf, prototypeOf, Store } from "@tsed/core";
|
|
2
|
+
import { __decorate, __metadata } from "tslib";
|
|
3
|
+
import { ancestorsOf, catchError, classOf, deepClone, deepMerge, Hooks, isClass, isFunction, isInheritedFrom, Metadata, nameOf, prototypeOf, Store } from "@tsed/core";
|
|
5
4
|
import { DI_PARAM_OPTIONS, INJECTABLE_PROP } from "../constants/constants.js";
|
|
6
5
|
import { Configuration } from "../decorators/configuration.js";
|
|
7
6
|
import { Injectable } from "../decorators/injectable.js";
|
|
8
7
|
import { Container } from "../domain/Container.js";
|
|
8
|
+
import { InjectablePropertyType } from "../domain/InjectablePropertyType.js";
|
|
9
9
|
import { LocalsContainer } from "../domain/LocalsContainer.js";
|
|
10
10
|
import { Provider } from "../domain/Provider.js";
|
|
11
|
+
import { ProviderScope } from "../domain/ProviderScope.js";
|
|
11
12
|
import { InjectionError } from "../errors/InjectionError.js";
|
|
12
13
|
import { UndefinedTokenError } from "../errors/UndefinedTokenError.js";
|
|
13
14
|
import { GlobalProviders } from "../registries/GlobalProviders.js";
|
|
14
15
|
import { createContainer } from "../utils/createContainer.js";
|
|
16
|
+
import { resolveControllers } from "../utils/resolveControllers.js";
|
|
15
17
|
import { DIConfiguration } from "./DIConfiguration.js";
|
|
16
|
-
import { ProviderScope } from "../domain/ProviderScope.js";
|
|
17
|
-
import { InjectablePropertyType } from "../domain/InjectablePropertyType.js";
|
|
18
18
|
/**
|
|
19
19
|
* This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
|
|
20
20
|
*
|
|
@@ -28,7 +28,7 @@ import { InjectablePropertyType } from "../domain/InjectablePropertyType.js";
|
|
|
28
28
|
* import MyService2 from "./services/service2";
|
|
29
29
|
* import MyService3 from "./services/service3";
|
|
30
30
|
*
|
|
31
|
-
* // When all services
|
|
31
|
+
* // When all services are imported, you can load InjectorService.
|
|
32
32
|
* const injector = new InjectorService()
|
|
33
33
|
*
|
|
34
34
|
* await injector.load();
|
|
@@ -37,13 +37,14 @@ import { InjectablePropertyType } from "../domain/InjectablePropertyType.js";
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
let InjectorService = InjectorService_1 = class InjectorService extends Container {
|
|
40
|
+
settings = new DIConfiguration();
|
|
41
|
+
logger = console;
|
|
42
|
+
resolvedConfiguration = false;
|
|
43
|
+
#cache = new LocalsContainer();
|
|
44
|
+
#hooks = new Hooks();
|
|
40
45
|
constructor() {
|
|
41
46
|
super();
|
|
42
|
-
this.
|
|
43
|
-
this.logger = console;
|
|
44
|
-
this.resolvedConfiguration = false;
|
|
45
|
-
_InjectorService_cache.set(this, new LocalsContainer());
|
|
46
|
-
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(InjectorService_1, this);
|
|
47
|
+
this.#cache.set(InjectorService_1, this);
|
|
47
48
|
}
|
|
48
49
|
get resolvers() {
|
|
49
50
|
return this.settings.resolvers;
|
|
@@ -62,7 +63,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
62
63
|
* Return a list of instance build by the injector.
|
|
63
64
|
*/
|
|
64
65
|
toArray() {
|
|
65
|
-
return
|
|
66
|
+
return [...this.#cache.values()];
|
|
66
67
|
}
|
|
67
68
|
/**
|
|
68
69
|
* Get a service or factory already constructed from his symbol or class.
|
|
@@ -113,7 +114,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
113
114
|
* @param token
|
|
114
115
|
*/
|
|
115
116
|
has(token) {
|
|
116
|
-
return
|
|
117
|
+
return this.#cache.get(token) !== undefined;
|
|
117
118
|
}
|
|
118
119
|
/**
|
|
119
120
|
* Invoke the class and inject all services that required by the class constructor.
|
|
@@ -136,73 +137,73 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
136
137
|
* @param options
|
|
137
138
|
* @returns {T} The class constructed.
|
|
138
139
|
*/
|
|
139
|
-
invoke(token, locals
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
140
|
+
invoke(token, locals, options = {}) {
|
|
141
|
+
let instance = locals ? locals.get(token) : undefined;
|
|
142
|
+
if (instance !== undefined) {
|
|
143
|
+
return instance;
|
|
144
|
+
}
|
|
145
|
+
if (token === Configuration) {
|
|
146
|
+
return this.settings;
|
|
147
|
+
}
|
|
148
|
+
instance = !options.rebuild ? this.getInstance(token) : undefined;
|
|
149
|
+
if (instance != undefined) {
|
|
150
|
+
return instance;
|
|
145
151
|
}
|
|
146
152
|
if (token === DI_PARAM_OPTIONS) {
|
|
147
153
|
return {};
|
|
148
154
|
}
|
|
155
|
+
const provider = this.ensureProvider(token);
|
|
149
156
|
if (!provider || options.rebuild) {
|
|
150
157
|
instance = this.resolve(token, locals, options);
|
|
151
158
|
if (this.hasProvider(token)) {
|
|
152
|
-
|
|
159
|
+
this.#cache.set(token, instance);
|
|
153
160
|
}
|
|
154
161
|
return instance;
|
|
155
162
|
}
|
|
163
|
+
instance = this.resolve(token, locals, options);
|
|
156
164
|
switch (this.scopeOf(provider)) {
|
|
157
165
|
case ProviderScope.SINGLETON:
|
|
158
|
-
if (!
|
|
159
|
-
|
|
160
|
-
if (provider.isAsync()) {
|
|
161
|
-
__classPrivateFieldGet(this, _InjectorService_cache, "f").get(token).then((instance) => {
|
|
162
|
-
__classPrivateFieldGet(this, _InjectorService_cache, "f").set(token, instance);
|
|
163
|
-
});
|
|
164
|
-
}
|
|
166
|
+
if (provider.hooks && !options.rebuild) {
|
|
167
|
+
this.registerHooks(provider, instance);
|
|
165
168
|
}
|
|
166
|
-
|
|
167
|
-
|
|
169
|
+
if (!provider.isAsync()) {
|
|
170
|
+
this.#cache.set(token, instance);
|
|
171
|
+
return instance;
|
|
172
|
+
}
|
|
173
|
+
// store promise to lock token in cache
|
|
174
|
+
this.#cache.set(token, instance);
|
|
175
|
+
instance = instance.then((instance) => {
|
|
176
|
+
this.#cache.set(token, instance);
|
|
177
|
+
return instance;
|
|
178
|
+
});
|
|
179
|
+
return instance;
|
|
168
180
|
case ProviderScope.REQUEST:
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
181
|
+
if (locals) {
|
|
182
|
+
locals.set(token, instance);
|
|
183
|
+
if (provider.hooks && provider.hooks.$onDestroy) {
|
|
184
|
+
locals.hooks.on("$onDestroy", (...args) => provider.hooks.$onDestroy(instance, ...args));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return instance;
|
|
175
188
|
}
|
|
176
189
|
return instance;
|
|
177
190
|
}
|
|
178
191
|
/**
|
|
179
192
|
* Build only providers which are asynchronous.
|
|
180
193
|
*/
|
|
181
|
-
async loadAsync(
|
|
194
|
+
async loadAsync() {
|
|
182
195
|
for (const [, provider] of this) {
|
|
183
|
-
if (!
|
|
184
|
-
|
|
185
|
-
await this.invoke(provider.token, locals);
|
|
186
|
-
}
|
|
187
|
-
const instance = __classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
|
|
188
|
-
if (instance !== undefined) {
|
|
189
|
-
locals.set(provider.token, instance);
|
|
190
|
-
}
|
|
196
|
+
if (!this.has(provider.token) && provider.isAsync()) {
|
|
197
|
+
await this.invoke(provider.token);
|
|
191
198
|
}
|
|
192
199
|
}
|
|
193
|
-
return locals;
|
|
194
200
|
}
|
|
195
|
-
loadSync(
|
|
201
|
+
loadSync() {
|
|
196
202
|
for (const [, provider] of this) {
|
|
197
|
-
if (!
|
|
198
|
-
this.invoke(provider.token
|
|
199
|
-
}
|
|
200
|
-
const instance = __classPrivateFieldGet(this, _InjectorService_cache, "f").get(provider.token);
|
|
201
|
-
if (instance !== undefined) {
|
|
202
|
-
locals.set(provider.token, instance);
|
|
203
|
+
if (!this.has(provider.token) && this.scopeOf(provider) === ProviderScope.SINGLETON) {
|
|
204
|
+
this.invoke(provider.token);
|
|
203
205
|
}
|
|
204
206
|
}
|
|
205
|
-
return locals;
|
|
206
207
|
}
|
|
207
208
|
/**
|
|
208
209
|
* Boostrap injector from container and resolve configuration.
|
|
@@ -216,24 +217,33 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
216
217
|
this.resolveConfiguration();
|
|
217
218
|
return this;
|
|
218
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Load injector from a given module
|
|
222
|
+
* @param rootModule
|
|
223
|
+
*/
|
|
224
|
+
loadModule(rootModule) {
|
|
225
|
+
this.settings.routes = this.settings.routes.concat(resolveControllers(this.settings));
|
|
226
|
+
const container = createContainer();
|
|
227
|
+
container.delete(rootModule);
|
|
228
|
+
container.addProvider(rootModule, {
|
|
229
|
+
type: "server:module",
|
|
230
|
+
scope: ProviderScope.SINGLETON
|
|
231
|
+
});
|
|
232
|
+
return this.load(container);
|
|
233
|
+
}
|
|
219
234
|
/**
|
|
220
235
|
* Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
|
|
221
236
|
*
|
|
222
237
|
* @param container
|
|
223
|
-
* @param rootModule
|
|
224
238
|
*/
|
|
225
|
-
async load(container = createContainer()
|
|
239
|
+
async load(container = createContainer()) {
|
|
226
240
|
this.bootstrap(container);
|
|
227
241
|
// build async and sync provider
|
|
228
|
-
|
|
229
|
-
if (rootModule) {
|
|
230
|
-
await this.invoke(rootModule);
|
|
231
|
-
}
|
|
242
|
+
await this.loadAsync();
|
|
232
243
|
// load sync provider
|
|
233
|
-
|
|
234
|
-
await
|
|
235
|
-
await
|
|
236
|
-
return locals;
|
|
244
|
+
this.loadSync();
|
|
245
|
+
await this.emit("$beforeInit");
|
|
246
|
+
await this.emit("$onInit");
|
|
237
247
|
}
|
|
238
248
|
/**
|
|
239
249
|
* Load all configurations registered on providers
|
|
@@ -246,18 +256,19 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
246
256
|
super.forEach((provider) => {
|
|
247
257
|
if (provider.configuration && provider.type !== "server:module") {
|
|
248
258
|
Object.entries(provider.configuration).forEach(([key, value]) => {
|
|
249
|
-
|
|
250
|
-
|
|
259
|
+
if (!["resolvers", "mount", "imports"].includes(key)) {
|
|
260
|
+
value = mergedConfiguration.has(key) ? deepMerge(mergedConfiguration.get(key), value) : deepClone(value);
|
|
261
|
+
mergedConfiguration.set(key, value);
|
|
262
|
+
}
|
|
251
263
|
});
|
|
252
264
|
}
|
|
253
265
|
if (provider.resolvers) {
|
|
254
|
-
this.resolvers.
|
|
266
|
+
this.settings.resolvers = this.settings.resolvers.concat(provider.resolvers);
|
|
255
267
|
}
|
|
256
268
|
});
|
|
257
269
|
mergedConfiguration.forEach((value, key) => {
|
|
258
|
-
this.settings
|
|
270
|
+
this.settings.set(key, deepMerge(value, this.settings.get(key)));
|
|
259
271
|
});
|
|
260
|
-
this.settings.build();
|
|
261
272
|
this.resolvedConfiguration = true;
|
|
262
273
|
}
|
|
263
274
|
/**
|
|
@@ -295,7 +306,6 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
295
306
|
});
|
|
296
307
|
}
|
|
297
308
|
/**
|
|
298
|
-
*
|
|
299
309
|
* @param instance
|
|
300
310
|
* @param {string} propertyKey
|
|
301
311
|
*/
|
|
@@ -395,20 +405,11 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
395
405
|
}, next);
|
|
396
406
|
};
|
|
397
407
|
}
|
|
398
|
-
/**
|
|
399
|
-
* Allow handler hack for AsyncHookContext plugin.
|
|
400
|
-
* @param ctx
|
|
401
|
-
* @param cb
|
|
402
|
-
* @protected
|
|
403
|
-
*/
|
|
404
|
-
runInContext(ctx, cb) {
|
|
405
|
-
return cb();
|
|
406
|
-
}
|
|
407
408
|
async lazyInvoke(token) {
|
|
408
409
|
let instance = this.getInstance(token);
|
|
409
410
|
if (!instance) {
|
|
410
411
|
instance = await this.invoke(token);
|
|
411
|
-
if (isFunction(instance
|
|
412
|
+
if (isFunction(instance?.$onInit)) {
|
|
412
413
|
await instance.$onInit();
|
|
413
414
|
}
|
|
414
415
|
}
|
|
@@ -417,11 +418,11 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
417
418
|
/**
|
|
418
419
|
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
|
|
419
420
|
* @param eventName The event name to emit at all services.
|
|
420
|
-
* @param args List of the parameters to give to each
|
|
421
|
+
* @param args List of the parameters to give to each service.
|
|
421
422
|
* @returns {Promise<any[]>} A list of promises.
|
|
422
423
|
*/
|
|
423
424
|
async emit(eventName, ...args) {
|
|
424
|
-
return
|
|
425
|
+
return this.#hooks.asyncEmit(eventName, args);
|
|
425
426
|
}
|
|
426
427
|
/**
|
|
427
428
|
* @param eventName
|
|
@@ -429,7 +430,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
429
430
|
* @param args
|
|
430
431
|
*/
|
|
431
432
|
alter(eventName, value, ...args) {
|
|
432
|
-
return
|
|
433
|
+
return this.#hooks.alter(eventName, value, args);
|
|
433
434
|
}
|
|
434
435
|
/**
|
|
435
436
|
* @param eventName
|
|
@@ -437,10 +438,10 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
437
438
|
* @param args
|
|
438
439
|
*/
|
|
439
440
|
async alterAsync(eventName, value, ...args) {
|
|
440
|
-
return
|
|
441
|
+
return this.#hooks.asyncAlter(eventName, value, args);
|
|
441
442
|
}
|
|
442
443
|
async destroy() {
|
|
443
|
-
await
|
|
444
|
+
await this.emit("$onDestroy");
|
|
444
445
|
}
|
|
445
446
|
ensureProvider(token) {
|
|
446
447
|
if (!this.hasProvider(token) && GlobalProviders.has(token)) {
|
|
@@ -449,7 +450,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
449
450
|
return this.getProvider(token);
|
|
450
451
|
}
|
|
451
452
|
getInstance(token) {
|
|
452
|
-
return
|
|
453
|
+
return this.#cache.get(token);
|
|
453
454
|
}
|
|
454
455
|
/**
|
|
455
456
|
* Invoke a class method and inject service.
|
|
@@ -468,7 +469,7 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
468
469
|
* @param options
|
|
469
470
|
* @private
|
|
470
471
|
*/
|
|
471
|
-
resolve(target, locals, options = {}) {
|
|
472
|
+
resolve(target, locals = new LocalsContainer(), options = {}) {
|
|
472
473
|
const resolvedOpts = this.mapInvokeOptions(target, locals, options);
|
|
473
474
|
const { token, deps, construct, imports, provider } = resolvedOpts;
|
|
474
475
|
if (provider) {
|
|
@@ -478,10 +479,9 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
478
479
|
let currentDependency = false;
|
|
479
480
|
try {
|
|
480
481
|
const invokeDependency = (parent) => (token, index) => {
|
|
481
|
-
var _a;
|
|
482
482
|
currentDependency = { token, index, deps };
|
|
483
483
|
if (token !== DI_PARAM_OPTIONS) {
|
|
484
|
-
const options =
|
|
484
|
+
const options = provider?.store?.get(`${DI_PARAM_OPTIONS}:${index}`);
|
|
485
485
|
locals.set(DI_PARAM_OPTIONS, options || {});
|
|
486
486
|
}
|
|
487
487
|
return isInheritedFrom(token, Provider, 1) ? provider : this.invoke(token, locals, { parent });
|
|
@@ -502,11 +502,6 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
502
502
|
if (instance && isClass(classOf(instance))) {
|
|
503
503
|
this.bindInjectableProperties(instance, locals, options);
|
|
504
504
|
}
|
|
505
|
-
if (instance && provider.hooks) {
|
|
506
|
-
Object.entries(provider.hooks).forEach(([key, cb]) => {
|
|
507
|
-
instance[key] = (...args) => cb(instance, ...args);
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
505
|
return instance;
|
|
511
506
|
}
|
|
512
507
|
/**
|
|
@@ -565,8 +560,15 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
|
|
|
565
560
|
provider
|
|
566
561
|
};
|
|
567
562
|
}
|
|
563
|
+
registerHooks(provider, instance) {
|
|
564
|
+
if (provider.hooks) {
|
|
565
|
+
Object.entries(provider.hooks).forEach(([event, cb]) => {
|
|
566
|
+
const callback = (...args) => cb(this.get(provider.token) || instance, ...args);
|
|
567
|
+
this.#hooks.on(event, callback);
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
568
571
|
};
|
|
569
|
-
_InjectorService_cache = new WeakMap();
|
|
570
572
|
InjectorService = InjectorService_1 = __decorate([
|
|
571
573
|
Injectable({
|
|
572
574
|
scope: ProviderScope.SINGLETON,
|