@tsed/di 7.0.0-alpha.1 → 7.0.0-beta.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/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 +566 -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 +563 -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/types/decorators/controller.d.ts +43 -0
- package/lib/{decorators → types/decorators}/index.d.ts +2 -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 +5 -1
- package/lib/{domain → types/domain}/DIContext.d.ts +6 -7
- 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 +17 -3
- 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 +31 -14
- 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 -11
- package/readme.md +1 -1
- package/tsconfig.compile.esm.json +24 -0
- package/lib/index.js +0 -2271
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -2235
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectionError = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const colors_1 = require("../utils/colors");
|
|
6
|
+
class InjectionError extends Error {
|
|
7
|
+
constructor(token, origin) {
|
|
8
|
+
super((0, core_1.isString)(origin) ? origin : "");
|
|
9
|
+
this.name = "INJECTION_ERROR";
|
|
10
|
+
this.tokens = [];
|
|
11
|
+
this.tokens = [token];
|
|
12
|
+
if (origin) {
|
|
13
|
+
if ((0, core_1.isString)(origin)) {
|
|
14
|
+
this.origin = {
|
|
15
|
+
message: origin,
|
|
16
|
+
stack: this.stack
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
if (origin.tokens) {
|
|
21
|
+
this.tokens = this.tokens.concat(origin.tokens);
|
|
22
|
+
this.origin = origin.origin;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.origin = origin;
|
|
26
|
+
this.stack = origin.stack;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const originMessage = this.origin ? "\nOrigin: " + this.origin.message : "";
|
|
31
|
+
const tokensMessage = this.tokens.map((token) => (0, core_1.nameOf)(token)).join(" > ");
|
|
32
|
+
this.message = `Injection failed on ${tokensMessage}${originMessage}`;
|
|
33
|
+
}
|
|
34
|
+
static throwInjectorError(token, currentDependency, error) {
|
|
35
|
+
if (currentDependency && (0, core_1.isClass)(token)) {
|
|
36
|
+
error.message = printDependencyInjectionError(token, { ...currentDependency, message: error.message });
|
|
37
|
+
}
|
|
38
|
+
throw new InjectionError(token, error);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.InjectionError = InjectionError;
|
|
42
|
+
function printDependencyInjectionError(token, options) {
|
|
43
|
+
let erroredArg = "";
|
|
44
|
+
const args = (0, core_1.getConstructorArgNames)(token)
|
|
45
|
+
.map((arg, index) => {
|
|
46
|
+
if (options.index === index) {
|
|
47
|
+
erroredArg = arg;
|
|
48
|
+
arg = colors_1.colors.red(arg);
|
|
49
|
+
}
|
|
50
|
+
return `${arg}: ${(0, core_1.nameOf)(options.deps[index])}`;
|
|
51
|
+
})
|
|
52
|
+
.join(", ");
|
|
53
|
+
const signature = (0, core_1.nameOf)(token) + "->constructor(" + args + ")";
|
|
54
|
+
const indexOf = signature.indexOf(erroredArg) - 5;
|
|
55
|
+
const drawline = (indexOf) => " ".repeat(indexOf) + colors_1.colors.red("^" + "‾".repeat(erroredArg.length - 1));
|
|
56
|
+
return "Unable to inject dependency. " + options.message + "\n\n" + signature + "\n" + (indexOf > -1 ? drawline(indexOf) : "");
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=InjectionError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InjectionError.js","sourceRoot":"","sources":["../../../src/errors/InjectionError.ts"],"names":[],"mappings":"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","sourcesContent":["import {getConstructorArgNames, isClass, isString, nameOf} from \"@tsed/core\";\nimport {TokenProvider} from \"../interfaces\";\nimport {colors} from \"../utils/colors\";\n\nexport class InjectionError extends Error {\n name = \"INJECTION_ERROR\";\n\n public tokens: TokenProvider[] = [];\n public origin: any;\n\n constructor(token: TokenProvider, origin?: any) {\n super(isString(origin) ? origin : \"\");\n\n this.tokens = [token];\n\n if (origin) {\n if (isString(origin)) {\n this.origin = {\n message: origin,\n stack: this.stack\n };\n } else {\n if (origin.tokens) {\n this.tokens = this.tokens.concat(origin.tokens);\n this.origin = origin.origin;\n } else {\n this.origin = origin;\n this.stack = origin.stack;\n }\n }\n }\n\n const originMessage = this.origin ? \"\\nOrigin: \" + this.origin.message : \"\";\n const tokensMessage = this.tokens.map((token) => nameOf(token)).join(\" > \");\n\n this.message = `Injection failed on ${tokensMessage}${originMessage}`;\n }\n\n static throwInjectorError(token: any, currentDependency: any, error: any) {\n if (currentDependency && isClass(token)) {\n error.message = printDependencyInjectionError(token, {...currentDependency, message: error.message});\n }\n\n throw new InjectionError(token, error);\n }\n}\n\nfunction printDependencyInjectionError(token: any, options: {token: any; index: number; deps: any[]; message: string}) {\n let erroredArg = \"\";\n\n const args = getConstructorArgNames(token)\n .map((arg, index) => {\n if (options.index === index) {\n erroredArg = arg;\n arg = colors.red(arg);\n }\n\n return `${arg}: ${nameOf(options.deps[index])}`;\n })\n .join(\", \");\n\n const signature = nameOf(token) + \"->constructor(\" + args + \")\";\n const indexOf = signature.indexOf(erroredArg) - 5;\n const drawline = (indexOf: number) => \" \".repeat(indexOf) + colors.red(\"^\" + \"‾\".repeat(erroredArg.length - 1));\n\n return \"Unable to inject dependency. \" + options.message + \"\\n\\n\" + signature + \"\\n\" + (indexOf > -1 ? drawline(indexOf) : \"\");\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UndefinedTokenError = void 0;
|
|
4
|
+
class UndefinedTokenError extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super("Given token is undefined. Have you enabled emitDecoratorMetadata in your tsconfig.json or decorated your class with @Injectable, @Service, ... decorator ?");
|
|
7
|
+
this.name = "UNDEFINED_TOKEN_ERROR";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.UndefinedTokenError = UndefinedTokenError;
|
|
11
|
+
//# sourceMappingURL=UndefinedTokenError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UndefinedTokenError.js","sourceRoot":"","sources":["../../../src/errors/UndefinedTokenError.ts"],"names":[],"mappings":";;;AAAA,MAAa,mBAAoB,SAAQ,KAAK;IAG5C;QACE,KAAK,CACH,4JAA4J,CAC7J,CAAC;QALJ,SAAI,GAAG,uBAAuB,CAAC;IAM/B,CAAC;CACF;AARD,kDAQC","sourcesContent":["export class UndefinedTokenError extends Error {\n name = \"UNDEFINED_TOKEN_ERROR\";\n\n constructor() {\n super(\n \"Given token is undefined. Have you enabled emitDecoratorMetadata in your tsconfig.json or decorated your class with @Injectable, @Service, ... decorator ?\"\n );\n }\n}\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./constants/index"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./domain"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./interfaces"), exports);
|
|
7
|
+
(0, tslib_1.__exportStar)(require("./decorators"), exports);
|
|
8
|
+
(0, tslib_1.__exportStar)(require("./registries/ProviderRegistry"), exports);
|
|
9
|
+
(0, tslib_1.__exportStar)(require("./registries/GlobalProviders"), exports);
|
|
10
|
+
(0, tslib_1.__exportStar)(require("./services/InjectorService"), exports);
|
|
11
|
+
(0, tslib_1.__exportStar)(require("./services/DILogger"), exports);
|
|
12
|
+
(0, tslib_1.__exportStar)(require("./services/DIConfiguration"), exports);
|
|
13
|
+
(0, tslib_1.__exportStar)(require("./services/DITest"), exports);
|
|
14
|
+
(0, tslib_1.__exportStar)(require("./errors/InjectionError"), exports);
|
|
15
|
+
(0, tslib_1.__exportStar)(require("./errors/UndefinedTokenError"), exports);
|
|
16
|
+
(0, tslib_1.__exportStar)(require("./utils/createContainer"), exports);
|
|
17
|
+
(0, tslib_1.__exportStar)(require("./utils/getConfiguration"), exports);
|
|
18
|
+
(0, tslib_1.__exportStar)(require("./utils/getConfiguration"), exports);
|
|
19
|
+
(0, tslib_1.__exportStar)(require("./utils/setLoggerLevel"), exports);
|
|
20
|
+
(0, tslib_1.__exportStar)(require("./utils/colors"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAAkC;AAClC,wDAAyB;AACzB,4DAA6B;AAC7B,4DAA6B;AAC7B,6EAA8C;AAC9C,4EAA6C;AAE7C,0EAA2C;AAC3C,mEAAoC;AACpC,0EAA2C;AAC3C,iEAAkC;AAElC,uEAAwC;AACxC,4EAA6C;AAE7C,uEAAwC;AACxC,wEAAyC;AACzC,wEAAyC;AACzC,sEAAuC;AACvC,8DAA+B","sourcesContent":["export * from \"./constants/index\";\nexport * from \"./domain\";\nexport * from \"./interfaces\";\nexport * from \"./decorators\";\nexport * from \"./registries/ProviderRegistry\";\nexport * from \"./registries/GlobalProviders\";\n\nexport * from \"./services/InjectorService\";\nexport * from \"./services/DILogger\";\nexport * from \"./services/DIConfiguration\";\nexport * from \"./services/DITest\";\n\nexport * from \"./errors/InjectionError\";\nexport * from \"./errors/UndefinedTokenError\";\n\nexport * from \"./utils/createContainer\";\nexport * from \"./utils/getConfiguration\";\nexport * from \"./utils/getConfiguration\";\nexport * from \"./utils/setLoggerLevel\";\nexport * from \"./utils/colors\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DIConfigurationOptions.js","sourceRoot":"","sources":["../../../src/interfaces/DIConfigurationOptions.ts"],"names":[],"mappings":"","sourcesContent":["import type {DIResolver} from \"./DIResolver\";\nimport type {ProviderScope} from \"../domain/ProviderScope\";\nimport type {TokenProvider} from \"./TokenProvider\";\n\ndeclare global {\n namespace TsED {\n interface Context {}\n interface Configuration {\n scopes: {[key: string]: ProviderScope};\n /**\n * Define a list of resolvers (it can be an external DI).\n */\n resolvers: DIResolver[];\n /**\n * Define dependencies to build the provider\n */\n imports: TokenProvider[];\n\n [key: string]: any;\n }\n }\n}\n\nexport interface DIConfigurationOptions extends TsED.Configuration {}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DILogger.js","sourceRoot":"","sources":["../../../src/interfaces/DILogger.ts"],"names":[],"mappings":"","sourcesContent":["export interface DILogger {\n info(...args: any[]): void | any;\n\n warn(...args: any[]): void | any;\n\n debug(...args: any[]): void | any;\n\n error(...args: any[]): void | any;\n\n trace(...args: any[]): void | any;\n\n [key: string]: any;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DIResolver.js","sourceRoot":"","sources":["../../../src/interfaces/DIResolver.ts"],"names":[],"mappings":"","sourcesContent":["import type {TokenProvider} from \"./TokenProvider\";\n\nexport interface DIResolver {\n deps?: TokenProvider[];\n get<T = any>(type: TokenProvider, options: any): T | undefined;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IProvider.js","sourceRoot":"","sources":["../../../src/interfaces/IProvider.ts"],"names":[],"mappings":"","sourcesContent":["import type {Type} from \"@tsed/core\";\nimport type {DIResolver} from \"./DIResolver\";\nimport type {ProviderScope} from \"../domain/ProviderScope\";\nimport type {ProviderType} from \"../domain/ProviderType\";\nimport type {TokenProvider} from \"./TokenProvider\";\n/**\n *\n */\nexport interface IProvider<T = any> {\n /**\n * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).\n */\n provide: TokenProvider;\n /**\n * Provider type\n */\n type?: ProviderType | string;\n /**\n * Instance build by the injector\n */\n instance?: T;\n /**\n * Define dependencies to build the provider\n */\n deps?: TokenProvider[];\n /**\n * Class to instantiate for the `token`.\n */\n useClass?: Type<T>;\n /**\n * Provide a function to build the provider\n */\n useFactory?: Function;\n /**\n * Provide an async function to build the provider\n */\n useAsyncFactory?: Function;\n /**\n * Provide predefined value\n */\n useValue?: any;\n /**\n * Scope used by the injector to build the provider.\n */\n scope?: ProviderScope;\n /**\n * A list of resolvers which will be used to resolve missing Symbol/Class when injector invoke a Class. This property allow external DI usage.\n */\n resolvers?: DIResolver[];\n\n /**\n *\n */\n [key: string]: any;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InjectableProperties.js","sourceRoot":"","sources":["../../../src/interfaces/InjectableProperties.ts"],"names":[],"mappings":"","sourcesContent":["import type {InjectablePropertyType} from \"../domain/InjectablePropertyType\";\nimport type {TokenProvider} from \"./TokenProvider\";\nimport type {InjectorService} from \"../services/InjectorService\";\nimport type {InvokeOptions} from \"./InvokeOptions\";\n\nexport interface InjectableProperty {\n propertyKey: string;\n}\n\nexport interface InjectablePropertyOptions extends InjectableProperty {\n bindingType: InjectablePropertyType.METHOD | InjectablePropertyType.PROPERTY | InjectablePropertyType.INTERCEPTOR;\n propertyType: string;\n useType?: TokenProvider;\n resolver: (injector: InjectorService, locals: Map<TokenProvider, any>, options: Partial<InvokeOptions> & {options: any}) => () => any;\n options?: any;\n}\n\nexport interface InjectablePropertyValue extends InjectableProperty {\n bindingType: InjectablePropertyType.CONSTANT | InjectablePropertyType.VALUE;\n expression: string;\n defaultValue?: any;\n}\n\nexport interface InjectableProperties {\n [key: string]: InjectablePropertyOptions | InjectablePropertyValue;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InterceptorContext.js","sourceRoot":"","sources":["../../../src/interfaces/InterceptorContext.ts"],"names":[],"mappings":"","sourcesContent":["import type {Type} from \"@tsed/core\";\n\nexport interface InterceptorNext {\n <T>(err?: Error): T;\n}\n\nexport interface InterceptorContext<Klass = Type, Opts = any> {\n target: Klass;\n propertyKey: string;\n args: any[];\n next: InterceptorNext;\n options?: Opts;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InterceptorMethods.js","sourceRoot":"","sources":["../../../src/interfaces/InterceptorMethods.ts"],"names":[],"mappings":"","sourcesContent":["import type {InterceptorContext, InterceptorNext} from \"./InterceptorContext\";\n\nexport interface InterceptorMethods {\n intercept?(context: InterceptorContext, next?: InterceptorNext): any;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InvokeOptions.js","sourceRoot":"","sources":["../../../src/interfaces/InvokeOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {ProviderScope} from \"../domain/ProviderScope\";\nimport {TokenProvider} from \"./TokenProvider\";\n\nexport interface InvokeOptions<T = any> {\n deps: any[];\n imports: any[];\n parent?: TokenProvider;\n scope: ProviderScope;\n useScope: boolean;\n rebuild?: boolean;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OnDestroy.js","sourceRoot":"","sources":["../../../src/interfaces/OnDestroy.ts"],"names":[],"mappings":"","sourcesContent":["export interface OnDestroy {\n $onDestroy(): Promise<any> | void;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OnInit.js","sourceRoot":"","sources":["../../../src/interfaces/OnInit.ts"],"names":[],"mappings":"","sourcesContent":["export interface OnInit {\n $onInit(): Promise<any> | void;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RegistrySettings.js","sourceRoot":"","sources":["../../../src/interfaces/RegistrySettings.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\nimport type {Provider} from \"../domain\";\nimport type {InjectorService} from \"../services/InjectorService\";\nimport type {ResolvedInvokeOptions} from \"./ResolvedInvokeOptions\";\n\n/**\n * @ignore\n */\nexport interface RegistrySettings {\n injectable?: boolean;\n model?: Type<Provider>;\n\n /**\n *\n * @param provider\n * @param {Map<string | Function, any>} locals\n * @param options\n */\n onInvoke?(provider: Provider, locals: Map<string | Function, any>, options: ResolvedInvokeOptions & {injector: InjectorService}): void;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResolvedInvokeOptions.js","sourceRoot":"","sources":["../../../src/interfaces/ResolvedInvokeOptions.ts"],"names":[],"mappings":"","sourcesContent":["import type {Provider} from \"../domain/Provider\";\nimport type {ProviderScope} from \"../domain/ProviderScope\";\nimport type {TokenProvider} from \"./TokenProvider\";\n\nexport interface ResolvedInvokeOptions {\n token: TokenProvider;\n parent?: TokenProvider;\n scope: ProviderScope;\n deps: TokenProvider[];\n imports: TokenProvider[];\n provider: Provider;\n\n construct(deps: TokenProvider[]): any;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TokenProvider.js","sourceRoot":"","sources":["../../../src/interfaces/TokenProvider.ts"],"names":[],"mappings":"","sourcesContent":["import type {Type} from \"@tsed/core\";\n\nexport type TokenProvider = string | symbol | Type | Function | any;\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./DILogger"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./DIConfigurationOptions"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./DIResolver"), exports);
|
|
7
|
+
(0, tslib_1.__exportStar)(require("./InjectableProperties"), exports);
|
|
8
|
+
(0, tslib_1.__exportStar)(require("./InterceptorMethods"), exports);
|
|
9
|
+
(0, tslib_1.__exportStar)(require("./InterceptorContext"), exports);
|
|
10
|
+
(0, tslib_1.__exportStar)(require("./InvokeOptions"), exports);
|
|
11
|
+
(0, tslib_1.__exportStar)(require("./IProvider"), exports);
|
|
12
|
+
(0, tslib_1.__exportStar)(require("./OnInit"), exports);
|
|
13
|
+
(0, tslib_1.__exportStar)(require("./OnDestroy"), exports);
|
|
14
|
+
(0, tslib_1.__exportStar)(require("./OnInit"), exports);
|
|
15
|
+
(0, tslib_1.__exportStar)(require("./RegistrySettings"), exports);
|
|
16
|
+
(0, tslib_1.__exportStar)(require("./TokenProvider"), exports);
|
|
17
|
+
(0, tslib_1.__exportStar)(require("./ResolvedInvokeOptions"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":";;;AAAA,0DAA2B;AAC3B,wEAAyC;AACzC,4DAA6B;AAC7B,sEAAuC;AACvC,oEAAqC;AACrC,oEAAqC;AACrC,+DAAgC;AAChC,2DAA4B;AAC5B,wDAAyB;AACzB,2DAA4B;AAC5B,wDAAyB;AACzB,kEAAmC;AACnC,+DAAgC;AAChC,uEAAwC","sourcesContent":["export * from \"./DILogger\";\nexport * from \"./DIConfigurationOptions\";\nexport * from \"./DIResolver\";\nexport * from \"./InjectableProperties\";\nexport * from \"./InterceptorMethods\";\nexport * from \"./InterceptorContext\";\nexport * from \"./InvokeOptions\";\nexport * from \"./IProvider\";\nexport * from \"./OnInit\";\nexport * from \"./OnDestroy\";\nexport * from \"./OnInit\";\nexport * from \"./RegistrySettings\";\nexport * from \"./TokenProvider\";\nexport * from \"./ResolvedInvokeOptions\";\n"]}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _GlobalProviderRegistry_settings;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.GlobalProviders = exports.GlobalProviderRegistry = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const core_1 = require("@tsed/core");
|
|
7
|
+
const Provider_1 = require("../domain/Provider");
|
|
8
|
+
const ProviderType_1 = require("../domain/ProviderType");
|
|
9
|
+
class GlobalProviderRegistry extends Map {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(...arguments);
|
|
12
|
+
_GlobalProviderRegistry_settings.set(this, new Map());
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The get() method returns a specified element from a Map object.
|
|
16
|
+
* @param key Required. The key of the element to return from the Map object.
|
|
17
|
+
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
|
|
18
|
+
*/
|
|
19
|
+
get(key) {
|
|
20
|
+
return super.get((0, core_1.getClassOrSymbol)(key));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The has() method returns a boolean indicating whether an element with the specified key exists or not.
|
|
24
|
+
* @param key
|
|
25
|
+
* @returns {boolean}
|
|
26
|
+
*/
|
|
27
|
+
has(key) {
|
|
28
|
+
return super.has((0, core_1.getClassOrSymbol)(key));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The set() method adds or updates an element with a specified key and value to a Map object.
|
|
32
|
+
* @param key Required. The key of the element to add to the Map object.
|
|
33
|
+
* @param metadata Required. The value of the element to add to the Map object.
|
|
34
|
+
*/
|
|
35
|
+
set(key, metadata) {
|
|
36
|
+
super.set((0, core_1.getClassOrSymbol)(key), metadata);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
*
|
|
41
|
+
* @param target
|
|
42
|
+
* @param options
|
|
43
|
+
*/
|
|
44
|
+
merge(target, options) {
|
|
45
|
+
const meta = this.createIfNotExists(target, options);
|
|
46
|
+
Object.keys(options).forEach((key) => {
|
|
47
|
+
meta[key] = options[key];
|
|
48
|
+
});
|
|
49
|
+
this.set(target, meta);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The delete() method removes the specified element from a Map object.
|
|
53
|
+
* @param key Required. The key of the element to remove from the Map object.
|
|
54
|
+
* @returns {boolean} Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.
|
|
55
|
+
*/
|
|
56
|
+
delete(key) {
|
|
57
|
+
return super.delete((0, core_1.getClassOrSymbol)(key));
|
|
58
|
+
}
|
|
59
|
+
createRegistry(type, model, options = {}) {
|
|
60
|
+
const defaultOptions = this.getRegistrySettings(type);
|
|
61
|
+
options = Object.assign(defaultOptions, {
|
|
62
|
+
...options,
|
|
63
|
+
model
|
|
64
|
+
});
|
|
65
|
+
(0, tslib_1.__classPrivateFieldGet)(this, _GlobalProviderRegistry_settings, "f").set(type, options);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
onInvoke(provider, locals, options) {
|
|
69
|
+
const settings = (0, tslib_1.__classPrivateFieldGet)(this, _GlobalProviderRegistry_settings, "f").get(provider.type);
|
|
70
|
+
if (settings === null || settings === void 0 ? void 0 : settings.onInvoke) {
|
|
71
|
+
settings.onInvoke(provider, locals, options);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getRegistrySettings(target) {
|
|
75
|
+
let type = "provider";
|
|
76
|
+
if (typeof target === "string") {
|
|
77
|
+
type = target;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
const provider = this.get(target);
|
|
81
|
+
if (provider) {
|
|
82
|
+
type = provider.type;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return ((0, tslib_1.__classPrivateFieldGet)(this, _GlobalProviderRegistry_settings, "f").get(type) || {
|
|
86
|
+
model: Provider_1.Provider
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
createRegisterFn(type) {
|
|
90
|
+
return (provider, instance) => {
|
|
91
|
+
// istanbul ignore next
|
|
92
|
+
if (!provider.provide) {
|
|
93
|
+
provider = {
|
|
94
|
+
provide: provider
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
provider = Object.assign({ instance }, provider, { type });
|
|
98
|
+
this.merge(provider.provide, provider);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
*
|
|
103
|
+
* @param key
|
|
104
|
+
* @param options
|
|
105
|
+
*/
|
|
106
|
+
createIfNotExists(key, options) {
|
|
107
|
+
const type = options.type || ProviderType_1.ProviderType.PROVIDER;
|
|
108
|
+
if (!this.has(key)) {
|
|
109
|
+
const { model = Provider_1.Provider } = (0, tslib_1.__classPrivateFieldGet)(this, _GlobalProviderRegistry_settings, "f").get(type) || {};
|
|
110
|
+
const item = new model(key, options);
|
|
111
|
+
this.set(key, item);
|
|
112
|
+
}
|
|
113
|
+
return this.get(key);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.GlobalProviderRegistry = GlobalProviderRegistry;
|
|
117
|
+
_GlobalProviderRegistry_settings = new WeakMap();
|
|
118
|
+
/**
|
|
119
|
+
*
|
|
120
|
+
* @type {GlobalProviders}
|
|
121
|
+
*/
|
|
122
|
+
// tslint:disable-next-line: variable-name
|
|
123
|
+
exports.GlobalProviders = new GlobalProviderRegistry();
|
|
124
|
+
//# sourceMappingURL=GlobalProviders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GlobalProviders.js","sourceRoot":"","sources":["../../../src/registries/GlobalProviders.ts"],"names":[],"mappings":"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","sourcesContent":["import {getClassOrSymbol, Type} from \"@tsed/core\";\nimport {Provider} from \"../domain/Provider\";\nimport {ProviderType} from \"../domain/ProviderType\";\nimport type {IProvider, RegistrySettings, TokenProvider} from \"../interfaces\";\nimport type {InjectorService} from \"../services/InjectorService\";\nimport {ResolvedInvokeOptions} from \"../interfaces/ResolvedInvokeOptions\";\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> = {}) {\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 onInvoke(provider: Provider, locals: Map<TokenProvider, any>, options: ResolvedInvokeOptions & {injector: InjectorService}) {\n const settings = this.#settings.get(provider.type);\n\n if (settings?.onInvoke) {\n settings.onInvoke(provider, locals, options);\n }\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 }\n );\n }\n\n createRegisterFn(type: string) {\n return (provider: any | IProvider, instance?: any): void => {\n // istanbul ignore next\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 *\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, options);\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"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerController = exports.registerValue = 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
|
+
/**
|
|
12
|
+
* Register a provider configuration.
|
|
13
|
+
* @param {IProvider<any>} provider
|
|
14
|
+
*/
|
|
15
|
+
function registerProvider(provider) {
|
|
16
|
+
if (!provider.provide) {
|
|
17
|
+
throw new Error("Provider.provide is required");
|
|
18
|
+
}
|
|
19
|
+
GlobalProviders_1.GlobalProviders.merge(provider.provide, provider);
|
|
20
|
+
}
|
|
21
|
+
exports.registerProvider = registerProvider;
|
|
22
|
+
/**
|
|
23
|
+
* Add a new value in the `ProviderRegistry`.
|
|
24
|
+
*
|
|
25
|
+
* #### Example with symbol definition
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import {registerValue, InjectorService} from "@tsed/di";
|
|
30
|
+
*
|
|
31
|
+
* const MyValue = Symbol.from("MyValue")
|
|
32
|
+
*
|
|
33
|
+
* registerValue({token: MyValue, useValue: "myValue"});
|
|
34
|
+
*
|
|
35
|
+
* @Service()
|
|
36
|
+
* export class OtherService {
|
|
37
|
+
* constructor(@Inject(MyValue) myValue: string){
|
|
38
|
+
* console.log(myValue); /// "myValue"
|
|
39
|
+
* }
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
const registerValue = (provider, value) => {
|
|
44
|
+
if (!provider.provide) {
|
|
45
|
+
provider = {
|
|
46
|
+
provide: provider
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
provider = Object.assign({
|
|
50
|
+
scope: domain_1.ProviderScope.SINGLETON,
|
|
51
|
+
useValue: value
|
|
52
|
+
}, provider, { type: domain_1.ProviderType.VALUE });
|
|
53
|
+
GlobalProviders_1.GlobalProviders.merge(provider.provide, provider);
|
|
54
|
+
};
|
|
55
|
+
exports.registerValue = registerValue;
|
|
56
|
+
/**
|
|
57
|
+
* Add a new controller in the `ProviderRegistry`. This controller will be built when `InjectorService` will be loaded.
|
|
58
|
+
*
|
|
59
|
+
* #### Example
|
|
60
|
+
*
|
|
61
|
+
* ```typescript
|
|
62
|
+
* import {registerController, InjectorService} from "@tsed/di";
|
|
63
|
+
*
|
|
64
|
+
* export default class MyController {
|
|
65
|
+
* constructor(){}
|
|
66
|
+
* transform() {
|
|
67
|
+
* return "test";
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* registerController({provide: MyController});
|
|
72
|
+
* // or
|
|
73
|
+
* registerController(MyController);
|
|
74
|
+
*
|
|
75
|
+
* const injector = new InjectorService();
|
|
76
|
+
* injector.load();
|
|
77
|
+
*
|
|
78
|
+
* const myController = injector.get<MyController>(MyController);
|
|
79
|
+
* myController.getFoo(); // test
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @param provider Provider configuration.
|
|
83
|
+
*/
|
|
84
|
+
exports.registerController = GlobalProviders_1.GlobalProviders.createRegisterFn(domain_1.ProviderType.CONTROLLER);
|
|
85
|
+
//# sourceMappingURL=ProviderRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,CAAC,CAAC;AAElE;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,QAA4B;IAC3D,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;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;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\n/**\n * Register a provider configuration.\n * @param {IProvider<any>} provider\n */\nexport function registerProvider(provider: Partial<IProvider>): 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 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 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"]}
|
|
@@ -0,0 +1,129 @@
|
|
|
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 (0, 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
|
+
(0, 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 = (0, core_1.getValue)(this.map, propertyKey);
|
|
82
|
+
if (value !== undefined) {
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
return (0, 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 = (0, 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((0, 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) => (0, 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) => (0, core_1.getValue)(this.map, propertyKey, defaultValue);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.DIConfiguration = DIConfiguration;
|
|
129
|
+
//# sourceMappingURL=DIConfiguration.js.map
|