@tsed/di 7.0.0-beta.2 → 7.0.0-beta.20
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/constants/{index.js → constants.js} +1 -1
- package/lib/cjs/constants/constants.js.map +1 -0
- package/lib/cjs/decorators/constant.js +1 -1
- package/lib/cjs/decorators/constant.js.map +1 -1
- package/lib/cjs/decorators/controller.js.map +1 -1
- package/lib/cjs/decorators/inject.js +27 -2
- package/lib/cjs/decorators/inject.js.map +1 -1
- package/lib/cjs/decorators/injectable.js +0 -2
- package/lib/cjs/decorators/injectable.js.map +1 -1
- package/lib/cjs/decorators/intercept.js +20 -12
- package/lib/cjs/decorators/intercept.js.map +1 -1
- package/lib/cjs/decorators/module.js +4 -3
- package/lib/cjs/decorators/module.js.map +1 -1
- package/lib/cjs/decorators/opts.js +1 -1
- package/lib/cjs/decorators/opts.js.map +1 -1
- package/lib/cjs/decorators/useOpts.js +1 -1
- package/lib/cjs/decorators/useOpts.js.map +1 -1
- package/lib/cjs/decorators/value.js +1 -1
- package/lib/cjs/decorators/value.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 +60 -17
- 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/ProviderOpts.js +3 -0
- package/lib/cjs/interfaces/ProviderOpts.js.map +1 -0
- package/lib/cjs/interfaces/RegistrySettings.js.map +1 -1
- package/lib/cjs/interfaces/TokenProvider.js.map +1 -1
- package/lib/cjs/interfaces/{IProvider.js → TokenRoute.js} +1 -1
- package/lib/cjs/interfaces/TokenRoute.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/registries/GlobalProviders.js +7 -12
- package/lib/cjs/registries/GlobalProviders.js.map +1 -1
- package/lib/cjs/registries/ProviderRegistry.js +8 -7
- 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 +13 -6
- package/lib/cjs/services/DITest.js.map +1 -1
- package/lib/cjs/services/InjectorService.js +115 -113
- 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/constants/{index.js → constants.js} +1 -1
- package/lib/esm/constants/constants.js.map +1 -0
- package/lib/esm/decorators/configuration.js +1 -1
- package/lib/esm/decorators/constant.js +2 -2
- package/lib/esm/decorators/constant.js.map +1 -1
- package/lib/esm/decorators/controller.js +1 -1
- package/lib/esm/decorators/controller.js.map +1 -1
- package/lib/esm/decorators/inject.js +25 -1
- package/lib/esm/decorators/inject.js.map +1 -1
- package/lib/esm/decorators/injectable.js +1 -3
- package/lib/esm/decorators/injectable.js.map +1 -1
- package/lib/esm/decorators/intercept.js +21 -13
- package/lib/esm/decorators/intercept.js.map +1 -1
- package/lib/esm/decorators/interceptor.js +2 -2
- package/lib/esm/decorators/lazyInject.js +1 -1
- package/lib/esm/decorators/module.js +4 -3
- package/lib/esm/decorators/module.js.map +1 -1
- package/lib/esm/decorators/opts.js +4 -4
- package/lib/esm/decorators/opts.js.map +1 -1
- package/lib/esm/decorators/overrideProvider.js +1 -1
- package/lib/esm/decorators/scope.js +1 -1
- package/lib/esm/decorators/service.js +1 -1
- package/lib/esm/decorators/useOpts.js +1 -1
- package/lib/esm/decorators/useOpts.js.map +1 -1
- package/lib/esm/decorators/value.js +2 -2
- package/lib/esm/decorators/value.js.map +1 -1
- package/lib/esm/domain/Container.js +3 -4
- 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 +88 -32
- 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 +23 -4
- package/lib/esm/domain/Provider.js.map +1 -1
- package/lib/esm/errors/InjectionError.js +4 -3
- 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 +60 -17
- 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/ProviderOpts.js +2 -0
- package/lib/esm/interfaces/ProviderOpts.js.map +1 -0
- package/lib/esm/interfaces/RegistrySettings.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/package.json +3 -0
- package/lib/esm/registries/GlobalProviders.js +9 -14
- package/lib/esm/registries/GlobalProviders.js.map +1 -1
- package/lib/esm/registries/ProviderRegistry.js +6 -5
- 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/DILogger.js +2 -2
- package/lib/esm/services/DITest.js +16 -9
- package/lib/esm/services/DITest.js.map +1 -1
- package/lib/esm/services/InjectorService.js +116 -114
- 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/createContainer.js +2 -2
- 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/constants/{index.d.ts → constants.d.ts} +0 -0
- package/lib/types/decorators/controller.d.ts +2 -3
- package/lib/types/decorators/inject.d.ts +15 -0
- package/lib/types/decorators/injectable.d.ts +2 -4
- package/lib/types/decorators/intercept.d.ts +2 -2
- package/lib/types/decorators/module.d.ts +3 -2
- package/lib/types/domain/Container.d.ts +3 -3
- 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 -35
- package/lib/types/domain/LocalsContainer.d.ts +3 -22
- package/lib/types/domain/Provider.d.ts +5 -6
- package/lib/types/errors/InjectionError.d.ts +1 -1
- package/lib/types/index.d.ts +54 -11
- 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/{IProvider.d.ts → ProviderOpts.d.ts} +5 -4
- package/lib/types/interfaces/RegistrySettings.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 +9 -6
- package/lib/types/registries/ProviderRegistry.d.ts +5 -4
- package/lib/types/services/DIConfiguration.d.ts +21 -12
- package/lib/types/services/DITest.d.ts +2 -0
- package/lib/types/services/InjectorService.d.ts +28 -34
- 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 +29 -10
- package/readme.md +40 -43
- package/tsconfig.compile.esm.json +1 -0
- package/lib/cjs/constants/index.js.map +0 -1
- package/lib/cjs/decorators/index.js +0 -19
- package/lib/cjs/decorators/index.js.map +0 -1
- package/lib/cjs/domain/index.js +0 -12
- package/lib/cjs/domain/index.js.map +0 -1
- package/lib/cjs/interfaces/IProvider.js.map +0 -1
- package/lib/cjs/interfaces/index.js +0 -18
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/utils/data/Test1.js +0 -8
- package/lib/cjs/utils/data/Test1.js.map +0 -1
- package/lib/cjs/utils/data/Test2.js +0 -8
- package/lib/cjs/utils/data/Test2.js.map +0 -1
- package/lib/esm/constants/index.js.map +0 -1
- package/lib/esm/decorators/index.js +0 -16
- package/lib/esm/decorators/index.js.map +0 -1
- package/lib/esm/domain/index.js +0 -9
- package/lib/esm/domain/index.js.map +0 -1
- package/lib/esm/interfaces/IProvider.js +0 -2
- package/lib/esm/interfaces/IProvider.js.map +0 -1
- package/lib/esm/interfaces/index.js +0 -15
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/utils/data/Test1.js +0 -4
- package/lib/esm/utils/data/Test1.js.map +0 -1
- package/lib/esm/utils/data/Test2.js +0 -4
- package/lib/esm/utils/data/Test2.js.map +0 -1
- package/lib/types/decorators/index.d.ts +0 -15
- package/lib/types/domain/index.d.ts +0 -8
- package/lib/types/interfaces/index.d.ts +0 -14
- package/lib/types/utils/data/Test1.d.ts +0 -3
- package/lib/types/utils/data/Test2.d.ts +0 -3
|
@@ -1,58 +1,66 @@
|
|
|
1
|
+
import { Hooks } from "@tsed/core";
|
|
1
2
|
import { levels } from "@tsed/logger";
|
|
3
|
+
const LEVELS = levels();
|
|
2
4
|
export class ContextLogger {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
dateStart;
|
|
6
|
+
id;
|
|
7
|
+
#additionalProps;
|
|
8
|
+
maxStackSize;
|
|
9
|
+
level;
|
|
10
|
+
#hooks;
|
|
11
|
+
#stack;
|
|
12
|
+
#logger;
|
|
13
|
+
constructor({ id, logger, dateStart = new Date(), level = "all", maxStackSize = 30, additionalProps }) {
|
|
9
14
|
this.dateStart = dateStart;
|
|
10
|
-
this.
|
|
11
|
-
this
|
|
12
|
-
this
|
|
13
|
-
|
|
14
|
-
this.level = levels()[level.toUpperCase()] || levels().ALL;
|
|
15
|
+
this.id = id;
|
|
16
|
+
this.#logger = logger;
|
|
17
|
+
this.#additionalProps = additionalProps;
|
|
18
|
+
this.level = (LEVELS[level.toUpperCase()] || LEVELS.ALL);
|
|
15
19
|
this.maxStackSize = maxStackSize;
|
|
16
20
|
}
|
|
21
|
+
get hooks() {
|
|
22
|
+
return (this.#hooks = this.#hooks || new Hooks());
|
|
23
|
+
}
|
|
24
|
+
get stack() {
|
|
25
|
+
return (this.#stack = this.#stack || []);
|
|
26
|
+
}
|
|
27
|
+
alterLog(cb) {
|
|
28
|
+
return this.hooks.on("log", cb);
|
|
29
|
+
}
|
|
30
|
+
alterIgnoreLog(cb) {
|
|
31
|
+
return this.hooks.on("ignore", cb);
|
|
32
|
+
}
|
|
17
33
|
info(obj) {
|
|
18
|
-
this.run(levels().INFO, obj
|
|
34
|
+
this.run(levels().INFO, obj);
|
|
19
35
|
return this;
|
|
20
36
|
}
|
|
21
37
|
debug(obj, withRequest = true) {
|
|
22
|
-
this.run(levels().DEBUG, obj,
|
|
23
|
-
obj = this.getData(obj);
|
|
24
|
-
return withRequest ? this.completeRequestPicker(obj) : obj;
|
|
25
|
-
});
|
|
38
|
+
this.run(levels().DEBUG, obj, withRequest);
|
|
26
39
|
return this;
|
|
27
40
|
}
|
|
28
41
|
warn(obj) {
|
|
29
|
-
this.run(levels().WARN, obj
|
|
42
|
+
this.run(levels().WARN, obj);
|
|
30
43
|
return this;
|
|
31
44
|
}
|
|
32
45
|
error(obj) {
|
|
33
|
-
this.run(levels().ERROR, obj
|
|
46
|
+
this.run(levels().ERROR, obj);
|
|
34
47
|
return this;
|
|
35
48
|
}
|
|
36
49
|
trace(obj) {
|
|
37
|
-
this.run(levels().TRACE, obj
|
|
50
|
+
this.run(levels().TRACE, obj);
|
|
38
51
|
return this;
|
|
39
52
|
}
|
|
40
53
|
flush() {
|
|
41
54
|
if (this.stack.length) {
|
|
42
55
|
this.stack.forEach(({ level, data }) => {
|
|
43
|
-
this
|
|
56
|
+
this.#logger[level](data);
|
|
44
57
|
});
|
|
45
|
-
this
|
|
58
|
+
this.#stack = [];
|
|
46
59
|
}
|
|
47
60
|
}
|
|
48
61
|
isLevelEnabled(otherLevel) {
|
|
49
62
|
return this.level.isLessThanOrEqualTo(otherLevel);
|
|
50
63
|
}
|
|
51
|
-
destroy() {
|
|
52
|
-
this.flush();
|
|
53
|
-
this.logger = undefined;
|
|
54
|
-
this.stack = undefined;
|
|
55
|
-
}
|
|
56
64
|
/**
|
|
57
65
|
* Return the duration between the time when LogIncomingRequest has handle the request and now.
|
|
58
66
|
* @returns {number}
|
|
@@ -64,14 +72,17 @@ export class ContextLogger {
|
|
|
64
72
|
if (typeof obj === "string") {
|
|
65
73
|
obj = { message: obj };
|
|
66
74
|
}
|
|
67
|
-
return { ...this
|
|
75
|
+
return { ...this.#additionalProps, reqId: this.id, time: new Date(), duration: this.getDuration(), ...obj };
|
|
68
76
|
}
|
|
69
|
-
run(level, obj,
|
|
77
|
+
run(level, obj, withRequest) {
|
|
70
78
|
if (!this.isLevelEnabled(level)) {
|
|
71
79
|
return;
|
|
72
80
|
}
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
const ignore = this.#hooks?.alter("ignore", false, [obj]);
|
|
82
|
+
if (!ignore) {
|
|
83
|
+
const levelStr = level.levelStr.toLowerCase();
|
|
84
|
+
obj = this.hooks.alter("log", this.getData(obj), [levelStr, withRequest]);
|
|
85
|
+
this.stack.push({ level: levelStr, data: obj });
|
|
75
86
|
}
|
|
76
87
|
if (this.maxStackSize < this.stack.length) {
|
|
77
88
|
this.flush();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContextLogger.js","sourceRoot":"","sources":["../../../src/domain/ContextLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAW,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"ContextLogger.js","sourceRoot":"","sources":["../../../src/domain/ContextLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AACjC,OAAO,EAAC,MAAM,EAAW,MAAM,cAAc,CAAC;AAY9C,MAAM,MAAM,GAA6B,MAAM,EAAE,CAAC;AAElD,MAAM,OAAO,aAAa;IACf,SAAS,CAAO;IAChB,EAAE,CAAS;IACX,gBAAgB,CAA2B;IAEpD,YAAY,CAAS;IACrB,KAAK,CAAW;IAEhB,MAAM,CAAS;IACf,MAAM,CAAS;IACf,OAAO,CAAM;IAEb,YAAY,EAAC,EAAE,EAAE,MAAM,EAAE,SAAS,GAAG,IAAI,IAAI,EAAE,EAAE,KAAK,GAAG,KAAK,EAAE,YAAY,GAAG,EAAE,EAAE,eAAe,EAAuB;QACvH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAa,CAAC;QACrE,IA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G,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;YAE1E,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAC,CAAC,CAAC;SAC/C;QAED,IAAI,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;YACzC,IAAI,CAAC,KAAK,EAAE,CAAC;SACd;IACH,CAAC;CACF","sourcesContent":["import {Hooks} from \"@tsed/core\";\nimport {levels, LogLevel} from \"@tsed/logger\";\nimport {DILogger} from \"../interfaces/DILogger\";\nimport type {DIContext} from \"./DIContext\";\n\nexport interface ContextLoggerOptions extends Record<string, any> {\n id: string;\n logger?: DILogger;\n level?: \"debug\" | \"info\" | \"warn\" | \"error\" | \"off\" | \"all\";\n maxStackSize?: number;\n additionalProps?: Record<any, any>;\n}\n\nconst LEVELS: Record<string, LogLevel> = levels();\n\nexport class ContextLogger {\n readonly dateStart: Date;\n readonly id: string;\n readonly #additionalProps?: Record<string, unknown>;\n\n maxStackSize: number;\n level: LogLevel;\n\n #hooks?: Hooks;\n #stack?: any[];\n #logger: any;\n\n constructor({id, logger, dateStart = new Date(), level = \"all\", maxStackSize = 30, additionalProps}: ContextLoggerOptions) {\n this.dateStart = dateStart;\n this.id = id;\n this.#logger = logger;\n this.#additionalProps = additionalProps;\n this.level = (LEVELS[level.toUpperCase()] || LEVELS.ALL) as LogLevel;\n this.maxStackSize = maxStackSize;\n }\n\n get hooks() {\n return (this.#hooks = this.#hooks || new Hooks());\n }\n\n private get stack() {\n return (this.#stack = this.#stack || []);\n }\n\n alterLog(cb: (data: any, level: \"debug\" | \"info\" | \"warn\" | \"error\" | \"off\" | \"all\", withRequest: boolean) => any) {\n return this.hooks.on(\"log\", cb);\n }\n\n alterIgnoreLog(cb: (ignore: boolean, data: any) => boolean) {\n return this.hooks.on(\"ignore\", cb);\n }\n\n info(obj: any) {\n this.run(levels().INFO, obj);\n return this;\n }\n\n debug(obj: any, withRequest: boolean = true) {\n this.run(levels().DEBUG, obj, withRequest);\n return this;\n }\n\n warn(obj: any) {\n this.run(levels().WARN, obj);\n return this;\n }\n\n error(obj: any) {\n this.run(levels().ERROR, obj);\n return this;\n }\n\n trace(obj: any) {\n this.run(levels().TRACE, obj);\n return this;\n }\n\n public flush() {\n if (this.stack.length) {\n this.stack.forEach(({level, data}: any) => {\n this.#logger[level](data);\n });\n\n this.#stack = [];\n }\n }\n\n public isLevelEnabled(otherLevel: string | LogLevel) {\n return this.level.isLessThanOrEqualTo(otherLevel);\n }\n\n /**\n * Return the duration between the time when LogIncomingRequest has handle the request and now.\n * @returns {number}\n */\n protected getDuration(): number {\n return new Date().getTime() - this.dateStart.getTime();\n }\n\n protected getData(obj: any) {\n if (typeof obj === \"string\") {\n obj = {message: obj};\n }\n\n return {...this.#additionalProps, reqId: this.id, time: new Date(), duration: this.getDuration(), ...obj};\n }\n\n protected run(level: LogLevel, obj: any, withRequest?: boolean) {\n if (!this.isLevelEnabled(level)) {\n return;\n }\n\n const ignore = this.#hooks?.alter(\"ignore\", false, [obj]);\n\n if (!ignore) {\n const levelStr = level.levelStr.toLowerCase();\n\n obj = this.hooks.alter(\"log\", this.getData(obj), [levelStr, withRequest]);\n\n this.stack.push({level: levelStr, data: obj});\n }\n\n if (this.maxStackSize < this.stack.length) {\n this.flush();\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Provider } from "./Provider.js";
|
|
2
|
+
import { ProviderType } from "./ProviderType.js";
|
|
3
|
+
export class ControllerProvider extends Provider {
|
|
4
|
+
tokenRouter;
|
|
5
|
+
constructor(provide, options = {}) {
|
|
6
|
+
super(provide, options);
|
|
7
|
+
this.type = ProviderType.CONTROLLER;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @returns {any[]}
|
|
12
|
+
*/
|
|
13
|
+
get middlewares() {
|
|
14
|
+
return Object.assign({
|
|
15
|
+
use: [],
|
|
16
|
+
useAfter: [],
|
|
17
|
+
useBefore: []
|
|
18
|
+
}, this.store.get("middlewares", {}));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param middlewares
|
|
23
|
+
*/
|
|
24
|
+
set middlewares(middlewares) {
|
|
25
|
+
const mdlwrs = this.middlewares;
|
|
26
|
+
const concat = (key, a, b) => (a[key] = a[key].concat(b[key]));
|
|
27
|
+
Object.keys(middlewares).forEach((key) => {
|
|
28
|
+
concat(key, mdlwrs, middlewares);
|
|
29
|
+
});
|
|
30
|
+
this.store.set("middlewares", mdlwrs);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=ControllerProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ControllerProvider.js","sourceRoot":"","sources":["../../../src/domain/ControllerProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AACpC,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,MAAM,OAAO,kBAA4B,SAAQ,QAAW;IACnD,WAAW,CAAS;IAE3B,YAAY,OAAsB,EAAE,UAA6B,EAAE;QACjE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC,UAAU,CAAC;IACtC,CAAC;IAED;;;OAGG;IACH,IAAI,WAAW;QACb,OAAO,MAAM,CAAC,MAAM,CAClB;YACE,GAAG,EAAE,EAAE;YACP,QAAQ,EAAE,EAAE;YACZ,SAAS,EAAE,EAAE;SACd,EACD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,EAAE,EAAE,CAAC,CAClC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,IAAI,WAAW,CAAC,WAAkC;QAChD,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC;QAChC,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,CAAM,EAAE,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAEjF,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;YAC/C,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;IACxC,CAAC;CACF","sourcesContent":["import {ControllerMiddlewares} from \"../decorators/controller\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {Provider} from \"./Provider\";\nimport {ProviderType} from \"./ProviderType\";\n\nexport class ControllerProvider<T = any> extends Provider<T> {\n public tokenRouter: string;\n\n constructor(provide: TokenProvider, options: Partial<Provider> = {}) {\n super(provide, options);\n this.type = ProviderType.CONTROLLER;\n }\n\n /**\n *\n * @returns {any[]}\n */\n get middlewares(): ControllerMiddlewares {\n return Object.assign(\n {\n use: [],\n useAfter: [],\n useBefore: []\n },\n this.store.get(\"middlewares\", {})\n );\n }\n\n /**\n *\n * @param middlewares\n */\n set middlewares(middlewares: ControllerMiddlewares) {\n const mdlwrs = this.middlewares;\n const concat = (key: string, a: any, b: any) => (a[key] = a[key].concat(b[key]));\n\n Object.keys(middlewares).forEach((key: string) => {\n concat(key, mdlwrs, middlewares);\n });\n\n this.store.set(\"middlewares\", mdlwrs);\n }\n}\n"]}
|
|
@@ -1,41 +1,97 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { runInContext } from "../utils/asyncHookContext.js";
|
|
2
|
+
import { ContextLogger } from "./ContextLogger.js";
|
|
3
|
+
import { LocalsContainer } from "./LocalsContainer.js";
|
|
4
|
+
export class DIContext {
|
|
5
|
+
opts;
|
|
6
|
+
#container;
|
|
7
|
+
#cache;
|
|
8
|
+
#logger;
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
this.opts = opts;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Logger attached to the context request.
|
|
14
|
+
*/
|
|
15
|
+
get logger() {
|
|
16
|
+
this.#logger = this.#logger || new ContextLogger(this.opts);
|
|
17
|
+
return this.#logger;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Request id generated by @@contextMiddleware@@.
|
|
21
|
+
*
|
|
22
|
+
* ::: tip
|
|
23
|
+
* By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, ""))`.
|
|
24
|
+
* Dash are removed to simplify tracking logs in Kibana
|
|
25
|
+
* :::
|
|
26
|
+
*
|
|
27
|
+
* ::: tip
|
|
28
|
+
* Request id can by customized by changing the server configuration.
|
|
29
|
+
*
|
|
30
|
+
* ```typescript
|
|
31
|
+
* @Configuration({
|
|
32
|
+
* logger: {
|
|
33
|
+
* reqIdBuilder: createUniqId // give your own id generator function
|
|
34
|
+
* }
|
|
35
|
+
* })
|
|
36
|
+
* class Server {
|
|
37
|
+
*
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
* :::
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
get id() {
|
|
44
|
+
return this.opts.id;
|
|
45
|
+
}
|
|
46
|
+
get dateStart() {
|
|
47
|
+
return this.logger.dateStart;
|
|
48
|
+
}
|
|
49
|
+
get injector() {
|
|
50
|
+
return this.opts.injector;
|
|
22
51
|
}
|
|
23
52
|
get env() {
|
|
24
|
-
return this.injector.settings.env;
|
|
53
|
+
return this.injector.settings.get("env");
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The request container used by the Ts.ED DI. It contains all services annotated with `@Scope(ProviderScope.REQUEST)`
|
|
57
|
+
*/
|
|
58
|
+
get container() {
|
|
59
|
+
return (this.#container = this.#container || new LocalsContainer());
|
|
25
60
|
}
|
|
26
61
|
async destroy() {
|
|
27
|
-
|
|
28
|
-
this.logger.destroy();
|
|
29
|
-
// @ts-ignore
|
|
30
|
-
delete this.container;
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
delete this.logger;
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
delete this.injector;
|
|
62
|
+
return Promise.all([this.#container?.destroy(), this.#logger?.flush()]);
|
|
35
63
|
}
|
|
36
64
|
async emit(eventName, ...args) {
|
|
37
|
-
|
|
38
|
-
|
|
65
|
+
return this.injector?.emit(eventName, ...args);
|
|
66
|
+
}
|
|
67
|
+
async runInContext(next) {
|
|
68
|
+
return runInContext(this, next);
|
|
69
|
+
}
|
|
70
|
+
cache(key, cb) {
|
|
71
|
+
if (!this.has(key)) {
|
|
72
|
+
this.set(key, cb());
|
|
73
|
+
}
|
|
74
|
+
return this.get(key);
|
|
75
|
+
}
|
|
76
|
+
async cacheAsync(key, cb) {
|
|
77
|
+
if (!this.has(key)) {
|
|
78
|
+
this.set(key, await cb());
|
|
79
|
+
}
|
|
80
|
+
return this.get(key);
|
|
81
|
+
}
|
|
82
|
+
delete(key) {
|
|
83
|
+
return !!this.#cache?.delete(key);
|
|
84
|
+
}
|
|
85
|
+
get(key) {
|
|
86
|
+
return this.#cache?.get(key);
|
|
87
|
+
}
|
|
88
|
+
has(key) {
|
|
89
|
+
return !!this.#cache?.has(key);
|
|
90
|
+
}
|
|
91
|
+
set(key, value) {
|
|
92
|
+
this.#cache = this.#cache || new Map();
|
|
93
|
+
this.#cache.set(key, value);
|
|
94
|
+
return this;
|
|
39
95
|
}
|
|
40
96
|
}
|
|
41
97
|
//# sourceMappingURL=DIContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,YAAY,EAAC,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAC,aAAa,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAQlD,MAAM,OAAO,SAAS;IAOD;IAJnB,UAAU,CAAmB;IAC7B,MAAM,CAAiB;IACvB,OAAO,CAAiB;IAExB,YAAmB,IAAsB;QAAtB,SAAI,GAAJ,IAAI,CAAkB;IAAG,CAAC;IAE7C;;OAEG;IACH,IAAI,MAAM;QACR,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5D,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IACtB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;IAC/B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAS,CAAC;IAC7B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,IAAI,SAAS;QAC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AC;IAED,GAAG,CAAC,GAAQ;QACV,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;IAED,GAAG,CAAC,GAAQ,EAAE,KAAU;QACtB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,GAAG,EAAY,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import {InjectorService} from \"../services/InjectorService\";\nimport {runInContext} from \"../utils/asyncHookContext\";\nimport {ContextLogger, ContextLoggerOptions} from \"./ContextLogger\";\nimport {LocalsContainer} from \"./LocalsContainer\";\n\nexport interface DIContextOptions extends Omit<ContextLoggerOptions, \"dateStart\"> {\n id: string;\n injector: InjectorService;\n logger: any;\n}\n\nexport class DIContext {\n [x: string]: any;\n\n #container?: LocalsContainer;\n #cache?: Map<any, any>;\n #logger?: ContextLogger;\n\n constructor(public opts: DIContextOptions) {}\n\n /**\n * Logger attached to the context request.\n */\n get logger() {\n this.#logger = this.#logger || new ContextLogger(this.opts);\n return this.#logger;\n }\n\n /**\n * Request id generated by @@contextMiddleware@@.\n *\n * ::: tip\n * By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, \"\"))`.\n * Dash are removed to simplify tracking logs in Kibana\n * :::\n *\n * ::: tip\n * Request id can by customized by changing the server configuration.\n *\n * ```typescript\n * @Configuration({\n * logger: {\n * reqIdBuilder: createUniqId // give your own id generator function\n * }\n * })\n * class Server {\n *\n * }\n * ```\n * :::\n *\n */\n get id() {\n return this.opts.id;\n }\n\n get dateStart() {\n return this.logger.dateStart;\n }\n\n get injector(): InjectorService {\n return this.opts.injector!;\n }\n\n get env() {\n return this.injector.settings.get(\"env\");\n }\n\n /**\n * The request container used by the Ts.ED DI. It contains all services annotated with `@Scope(ProviderScope.REQUEST)`\n */\n get container() {\n return (this.#container = this.#container || new LocalsContainer());\n }\n\n async destroy(): Promise<any> {\n return Promise.all([this.#container?.destroy(), this.#logger?.flush()]);\n }\n\n async emit(eventName: string, ...args: any[]) {\n return this.injector?.emit(eventName, ...args);\n }\n\n async runInContext(next: Function) {\n return runInContext(this, next);\n }\n\n cache<Value = any>(key: string, cb: () => Value): Value {\n if (!this.has(key)) {\n this.set(key, cb());\n }\n\n return this.get(key);\n }\n\n async cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value> {\n if (!this.has(key)) {\n this.set(key, await cb());\n }\n\n return this.get(key);\n }\n\n delete(key: any): boolean {\n return !!this.#cache?.delete(key);\n }\n\n get<T = any>(key: any): T {\n return this.#cache?.get(key);\n }\n\n has(key: any): boolean {\n return !!this.#cache?.has(key);\n }\n\n set(key: any, value: any): this {\n this.#cache = this.#cache || new Map<any, any>();\n this.#cache.set(key, value);\n return this;\n }\n}\n\nexport type BaseContext = DIContext & TsED.Context;\n"]}
|
|
@@ -1,51 +1,8 @@
|
|
|
1
|
+
import { Hooks } from "@tsed/core";
|
|
1
2
|
export class LocalsContainer extends Map {
|
|
2
|
-
|
|
3
|
-
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
|
|
4
|
-
* @param eventName The event name to emit at all services.
|
|
5
|
-
* @param args List of the parameters to give to each services.
|
|
6
|
-
* @returns {Promise<any[]>} A list of promises.
|
|
7
|
-
*/
|
|
8
|
-
async emit(eventName, ...args) {
|
|
9
|
-
for (const handler of this.getListeners(eventName)) {
|
|
10
|
-
await handler(...args);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* @param eventName
|
|
15
|
-
* @param value
|
|
16
|
-
* @param args
|
|
17
|
-
*/
|
|
18
|
-
alter(eventName, value, ...args) {
|
|
19
|
-
for (const handler of this.getListeners(eventName)) {
|
|
20
|
-
value = handler(value, ...args);
|
|
21
|
-
}
|
|
22
|
-
return value;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* @param eventName
|
|
26
|
-
* @param value
|
|
27
|
-
* @param args
|
|
28
|
-
*/
|
|
29
|
-
async alterAsync(eventName, value, ...args) {
|
|
30
|
-
for (const handler of this.getListeners(eventName)) {
|
|
31
|
-
value = handler(value, ...args);
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
34
|
-
}
|
|
35
|
-
toArray() {
|
|
36
|
-
return [...this.values()];
|
|
37
|
-
}
|
|
3
|
+
hooks = new Hooks();
|
|
38
4
|
async destroy() {
|
|
39
|
-
await this.
|
|
40
|
-
this.clear();
|
|
41
|
-
}
|
|
42
|
-
getListeners(event) {
|
|
43
|
-
return this.toArray().reduce((listeners, instance) => {
|
|
44
|
-
if (typeof instance === "object" && instance && event in instance) {
|
|
45
|
-
return listeners.concat(instance[event].bind(instance));
|
|
46
|
-
}
|
|
47
|
-
return listeners;
|
|
48
|
-
}, []);
|
|
5
|
+
await this.hooks.asyncEmit("$onDestroy");
|
|
49
6
|
}
|
|
50
7
|
}
|
|
51
8
|
//# sourceMappingURL=LocalsContainer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalsContainer.js","sourceRoot":"","sources":["../../../src/domain/LocalsContainer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"LocalsContainer.js","sourceRoot":"","sources":["../../../src/domain/LocalsContainer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AAGjC,MAAM,OAAO,eAAgB,SAAQ,GAAuB;IACjD,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC;IAE7B,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;IAC3C,CAAC;CACF","sourcesContent":["import {Hooks} from \"@tsed/core\";\nimport type {TokenProvider} from \"../interfaces/TokenProvider\";\n\nexport class LocalsContainer extends Map<TokenProvider, any> {\n readonly hooks = new Hooks();\n\n async destroy() {\n await this.hooks.asyncEmit(\"$onDestroy\");\n }\n}\n"]}
|
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import { classOf, getClassOrSymbol, isClass, nameOf, Store } from "@tsed/core";
|
|
2
|
-
import { ProviderScope } from "./ProviderScope";
|
|
3
|
-
import { ProviderType } from "./ProviderType";
|
|
1
|
+
import { classOf, getClassOrSymbol, isClass, methodsOf, nameOf, Store } from "@tsed/core";
|
|
2
|
+
import { ProviderScope } from "./ProviderScope.js";
|
|
3
|
+
import { ProviderType } from "./ProviderType.js";
|
|
4
4
|
export class Provider {
|
|
5
|
+
type = ProviderType.PROVIDER;
|
|
6
|
+
deps;
|
|
7
|
+
imports;
|
|
8
|
+
useFactory;
|
|
9
|
+
useAsyncFactory;
|
|
10
|
+
useValue;
|
|
11
|
+
hooks;
|
|
12
|
+
_useClass;
|
|
13
|
+
_provide;
|
|
14
|
+
_store;
|
|
15
|
+
_tokenStore;
|
|
5
16
|
constructor(token, options = {}) {
|
|
6
|
-
this.type = ProviderType.PROVIDER;
|
|
7
17
|
this.provide = token;
|
|
8
18
|
this.useClass = token;
|
|
9
19
|
Object.assign(this, options);
|
|
@@ -31,6 +41,15 @@ export class Provider {
|
|
|
31
41
|
if (isClass(value)) {
|
|
32
42
|
this._useClass = classOf(value);
|
|
33
43
|
this._store = Store.from(value);
|
|
44
|
+
this.hooks = methodsOf(this._useClass).reduce((hooks, { propertyKey }) => {
|
|
45
|
+
if (String(propertyKey).startsWith("$")) {
|
|
46
|
+
return {
|
|
47
|
+
...hooks,
|
|
48
|
+
[propertyKey]: (instance, ...args) => instance[propertyKey](...args)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return hooks;
|
|
52
|
+
}, {});
|
|
34
53
|
}
|
|
35
54
|
}
|
|
36
55
|
get className() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.js","sourceRoot":"","sources":["../../../src/domain/Provider.ts"],"names":[],"mappings":"AA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ACtC,IAAI,CAAC,UAAU,IAAI,SAAS;YAC5B,IAAI,CAAC,QAAQ,IAAI,OAAO;YACxB,IAAI,CAAC,eAAe,IAAI,cAAc;SACvC;aACE,MAAM,CAAC,OAAO,CAAC;aACf,IAAI,CAAC,GAAG,CAAC,CAAC;IACf,CAAC;CACF","sourcesContent":["import {classOf, getClassOrSymbol, isClass, methodsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport {ProviderOpts} from \"../interfaces/ProviderOpts\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {ProviderScope} from \"./ProviderScope\";\nimport {ProviderType} from \"./ProviderType\";\n\nexport type ProviderHookCallback<T = any> = (instance: T, ...args: any[]) => Promise<void> | void;\n\nexport class Provider<T = any> implements ProviderOpts<T> {\n public type: ProviderType | any = ProviderType.PROVIDER;\n public deps: TokenProvider[];\n public imports: any[];\n public useFactory: Function;\n public useAsyncFactory: Function;\n public useValue: any;\n public hooks?: Record<string, ProviderHookCallback<T>>;\n private _useClass: Type<T>;\n private _provide: TokenProvider;\n private _store: Store;\n private _tokenStore: Store;\n\n [key: string]: any;\n\n constructor(token: TokenProvider, options: Partial<Provider> = {}) {\n this.provide = token;\n this.useClass = token;\n\n Object.assign(this, options);\n }\n\n get token() {\n return this._provide;\n }\n\n get provide(): TokenProvider {\n return this._provide;\n }\n\n set provide(value: TokenProvider) {\n if (value) {\n this._provide = getClassOrSymbol(value);\n this._tokenStore = this._store = Store.from(value);\n }\n }\n\n get useClass(): Type<T> {\n return this._useClass;\n }\n\n /**\n * Create a new store if the given value is a class. Otherwise the value is ignored.\n * @param value\n */\n set useClass(value: Type<T>) {\n if (isClass(value)) {\n this._useClass = classOf(value);\n this._store = Store.from(value);\n\n this.hooks = methodsOf(this._useClass).reduce((hooks, {propertyKey}) => {\n if (String(propertyKey).startsWith(\"$\")) {\n return {\n ...hooks,\n [propertyKey]: (instance: any, ...args: any[]) => instance[propertyKey](...args)\n };\n }\n return hooks;\n }, {} as any);\n }\n }\n\n get className() {\n return this.name;\n }\n\n get name() {\n return nameOf(this.provide);\n }\n\n get store(): Store {\n return this._store;\n }\n\n get path() {\n return this.store.get(\"path\", \"/\");\n }\n\n set path(path: string) {\n this.store.set(\"path\", path);\n }\n\n /**\n * Get the scope of the provider.\n *\n * ::: tip Note\n * Async provider is always a SINGLETON\n * :::\n *\n * @returns {boolean}\n */\n get scope(): ProviderScope {\n if (this.isAsync()) {\n return ProviderScope.SINGLETON;\n }\n\n return this.get(\"scope\");\n }\n\n /**\n * Change the scope value of the provider.\n * @param scope\n */\n set scope(scope: ProviderScope) {\n this.store.set(\"scope\", scope);\n }\n\n get configuration(): Partial<TsED.Configuration> {\n return this.get(\"configuration\");\n }\n\n set configuration(configuration: Partial<TsED.Configuration>) {\n this.store.set(\"configuration\", configuration);\n }\n\n get children(): TokenProvider[] {\n return this.store.get(\"childrenControllers\", []);\n }\n\n get(key: string) {\n return this.store.get(key) || this._tokenStore.get(key);\n }\n\n isAsync(): boolean {\n return !!this.useAsyncFactory;\n }\n\n clone(): Provider {\n return new (classOf(this))(this._provide, this);\n }\n\n /**\n *\n * @returns {boolean}\n */\n public hasChildren(): boolean {\n return !!this.children.length;\n }\n\n /**\n *\n * @returns {boolean}\n */\n public hasParent(): boolean {\n return !!this.store.get(\"parentController\");\n }\n\n toString() {\n return [\n \"Token\",\n this.name,\n this.useClass && nameOf(this.useClass),\n this.useFactory && \"Factory\",\n this.useValue && \"Value\",\n this.useAsyncFactory && \"AsyncFactory\"\n ]\n .filter(Boolean)\n .join(\":\");\n }\n}\n"]}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { getConstructorArgNames, isClass, isString, nameOf } from "@tsed/core";
|
|
2
|
-
import { colors } from "../utils/colors";
|
|
2
|
+
import { colors } from "../utils/colors.js";
|
|
3
3
|
export class InjectionError extends Error {
|
|
4
|
+
name = "INJECTION_ERROR";
|
|
5
|
+
tokens = [];
|
|
6
|
+
origin;
|
|
4
7
|
constructor(token, origin) {
|
|
5
8
|
super(isString(origin) ? origin : "");
|
|
6
|
-
this.name = "INJECTION_ERROR";
|
|
7
|
-
this.tokens = [];
|
|
8
9
|
this.tokens = [token];
|
|
9
10
|
if (origin) {
|
|
10
11
|
if (isString(origin)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InjectionError.js","sourceRoot":"","sources":["../../../src/errors/InjectionError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,sBAAsB,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAC,MAAM,YAAY,CAAC;AAE7E,OAAO,EAAC,MAAM,EAAC,MAAM,iBAAiB,CAAC;AAEvC,MAAM,OAAO,cAAe,SAAQ,KAAK;
|
|
1
|
+
{"version":3,"file":"InjectionError.js","sourceRoot":"","sources":["../../../src/errors/InjectionError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,sBAAsB,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAC,MAAM,YAAY,CAAC;AAE7E,OAAO,EAAC,MAAM,EAAC,MAAM,iBAAiB,CAAC;AAEvC,MAAM,OAAO,cAAe,SAAQ,KAAK;IACvC,IAAI,GAAG,iBAAiB,CAAC;IAElB,MAAM,GAAoB,EAAE,CAAC;IAC7B,MAAM,CAAM;IAEnB,YAAY,KAAoB,EAAE,MAAY;QAC5C,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAEtC,IAAI,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,CAAC;QAEtB,IAAI,MAAM,EAAE;YACV,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;gBACpB,IAAI,CAAC,MAAM,GAAG;oBACZ,OAAO,EAAE,MAAM;oBACf,KAAK,EAAE,IAAI,CAAC,KAAK;iBAClB,CAAC;aACH;iBAAM;gBACL,IAAI,MAAM,CAAC,MAAM,EAAE;oBACjB,IAAI,CAAC,MAAM,GAAG,IA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GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;SACvB;QAED,OAAO,GAAG,GAAG,KAAK,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;IAClD,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAEd,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,gBAAgB,GAAG,IAAI,GAAG,GAAG,CAAC;IAChE,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAClD,MAAM,QAAQ,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;IAEhH,OAAO,+BAA+B,GAAG,OAAO,CAAC,OAAO,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AACjI,CAAC","sourcesContent":["import {getConstructorArgNames, isClass, isString, nameOf} from \"@tsed/core\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\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"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export class UndefinedTokenError extends Error {
|
|
2
|
+
name = "UNDEFINED_TOKEN_ERROR";
|
|
2
3
|
constructor() {
|
|
3
4
|
super("Given token is undefined. Have you enabled emitDecoratorMetadata in your tsconfig.json or decorated your class with @Injectable, @Service, ... decorator ?");
|
|
4
|
-
this.name = "UNDEFINED_TOKEN_ERROR";
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
7
|
//# sourceMappingURL=UndefinedTokenError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UndefinedTokenError.js","sourceRoot":"","sources":["../../../src/errors/UndefinedTokenError.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,mBAAoB,SAAQ,KAAK;
|
|
1
|
+
{"version":3,"file":"UndefinedTokenError.js","sourceRoot":"","sources":["../../../src/errors/UndefinedTokenError.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,IAAI,GAAG,uBAAuB,CAAC;IAE/B;QACE,KAAK,CACH,4JAA4J,CAC7J,CAAC;IACJ,CAAC;CACF","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"]}
|