@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,14 @@
|
|
|
1
|
+
import { Injectable } from "./injectable";
|
|
2
|
+
/**
|
|
3
|
+
* The decorators `@Service()` declare a new service can be injected in other service or controller on there `constructor`.
|
|
4
|
+
* All services annotated with `@Service()` are constructed one time.
|
|
5
|
+
*
|
|
6
|
+
* > `@Service()` use the `reflect-metadata` to collect and inject service on controllers or other services.
|
|
7
|
+
*
|
|
8
|
+
* @returns {Function}
|
|
9
|
+
* @decorator
|
|
10
|
+
*/
|
|
11
|
+
export function Service() {
|
|
12
|
+
return Injectable();
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/decorators/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AAExC;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO;IACrB,OAAO,UAAU,EAAE,CAAC;AACtB,CAAC","sourcesContent":["import {Injectable} from \"./injectable\";\n\n/**\n * The decorators `@Service()` declare a new service can be injected in other service or controller on there `constructor`.\n * All services annotated with `@Service()` are constructed one time.\n *\n * > `@Service()` use the `reflect-metadata` to collect and inject service on controllers or other services.\n *\n * @returns {Function}\n * @decorator\n */\nexport function Service(): Function {\n return Injectable();\n}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { decoratorTypeOf, DecoratorTypes, Store, UnsupportedDecoratorType } from "@tsed/core";
|
|
2
|
+
import { DI_PARAM_OPTIONS, INJECTABLE_PROP } from "../constants";
|
|
3
|
+
/**
|
|
4
|
+
* Add options to invoke the Service.
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import {Injectable, Opts, UseOpts} from "@tsed/di";
|
|
8
|
+
*
|
|
9
|
+
* @Injectable()
|
|
10
|
+
* class MyConfigurableService {
|
|
11
|
+
* source: string;
|
|
12
|
+
* constructor(@Opts options: any = {}) {
|
|
13
|
+
* console.log("Hello ", options.source); // log: Hello Service1 then Hello Service2
|
|
14
|
+
*
|
|
15
|
+
* this.source = options.source;
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* @Injectable()
|
|
20
|
+
* class MyService1 {
|
|
21
|
+
* constructor(@UseOpts({source: 'Service1'}) service: MyConfigurableService) {
|
|
22
|
+
* console.log(service.source) // log: Service1
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @Injectable()
|
|
27
|
+
* class MyService2 {
|
|
28
|
+
* constructor(@UseOpts({source: 'Service2'}) service: MyConfigurableService) {
|
|
29
|
+
* console.log(service.source) // log: Service2
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* ::: warning
|
|
35
|
+
* Using @@Opts@@ decorator on a constructor parameter change the Scope of the provider to `ProviderScope.INSTANCE`.
|
|
36
|
+
* :::
|
|
37
|
+
*
|
|
38
|
+
* @returns {Function}
|
|
39
|
+
* @decorator
|
|
40
|
+
* @param options
|
|
41
|
+
*/
|
|
42
|
+
export function UseOpts(options) {
|
|
43
|
+
return (target, propertyKey, index) => {
|
|
44
|
+
const bindingType = decoratorTypeOf([target, propertyKey, index]);
|
|
45
|
+
switch (bindingType) {
|
|
46
|
+
case DecoratorTypes.PARAM_CTOR:
|
|
47
|
+
Store.from(target).merge(`${DI_PARAM_OPTIONS}:${index}`, options);
|
|
48
|
+
break;
|
|
49
|
+
case DecoratorTypes.PROP:
|
|
50
|
+
Store.from(target).merge(INJECTABLE_PROP, {
|
|
51
|
+
[propertyKey]: {
|
|
52
|
+
options
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
throw new UnsupportedDecoratorType(UseOpts, [target, propertyKey, index]);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=useOpts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useOpts.js","sourceRoot":"","sources":["../../../src/decorators/useOpts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,cAAc,EAAE,KAAK,EAAE,wBAAwB,EAAC,MAAM,YAAY,CAAC;AAC5F,OAAO,EAAC,gBAAgB,EAAE,eAAe,EAAC,MAAM,cAAc,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,UAAU,OAAO,CAAC,OAA6B;IACnD,OAAO,CAAC,MAAc,EAAE,WAA4B,EAAE,KAAc,EAAO,EAAE;QAC3E,MAAM,WAAW,GAAG,eAAe,CAAC,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;QAElE,QAAQ,WAAW,EAAE;YACnB,KAAK,cAAc,CAAC,UAAU;gBAC5B,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,gBAAgB,IAAI,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;gBAClE,MAAM;YAER,KAAK,cAAc,CAAC,IAAI;gBACtB,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,eAAe,EAAE;oBACxC,CAAC,WAAqB,CAAC,EAAE;wBACvB,OAAO;qBACR;iBACF,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,wBAAwB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;SAC7E;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {decoratorTypeOf, DecoratorTypes, Store, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {DI_PARAM_OPTIONS, INJECTABLE_PROP} from \"../constants\";\n\n/**\n * Add options to invoke the Service.\n *\n * ```typescript\n * import {Injectable, Opts, UseOpts} from \"@tsed/di\";\n *\n * @Injectable()\n * class MyConfigurableService {\n * source: string;\n * constructor(@Opts options: any = {}) {\n * console.log(\"Hello \", options.source); // log: Hello Service1 then Hello Service2\n *\n * this.source = options.source;\n * }\n * }\n *\n * @Injectable()\n * class MyService1 {\n * constructor(@UseOpts({source: 'Service1'}) service: MyConfigurableService) {\n * console.log(service.source) // log: Service1\n * }\n * }\n *\n * @Injectable()\n * class MyService2 {\n * constructor(@UseOpts({source: 'Service2'}) service: MyConfigurableService) {\n * console.log(service.source) // log: Service2\n * }\n * }\n * ```\n *\n * ::: warning\n * Using @@Opts@@ decorator on a constructor parameter change the Scope of the provider to `ProviderScope.INSTANCE`.\n * :::\n *\n * @returns {Function}\n * @decorator\n * @param options\n */\nexport function UseOpts(options: {[key: string]: any}): Function {\n return (target: Object, propertyKey: string | symbol, index?: number): any => {\n const bindingType = decoratorTypeOf([target, propertyKey, index]);\n\n switch (bindingType) {\n case DecoratorTypes.PARAM_CTOR:\n Store.from(target).merge(`${DI_PARAM_OPTIONS}:${index}`, options);\n break;\n\n case DecoratorTypes.PROP:\n Store.from(target).merge(INJECTABLE_PROP, {\n [propertyKey as string]: {\n options\n }\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseOpts, [target, propertyKey, index]);\n }\n };\n}\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Store } from "@tsed/core";
|
|
2
|
+
import { INJECTABLE_PROP } from "../constants";
|
|
3
|
+
import { InjectablePropertyType } from "../domain/InjectablePropertyType";
|
|
4
|
+
/**
|
|
5
|
+
* Return value from Configuration.
|
|
6
|
+
*
|
|
7
|
+
* ## Example
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import {Env} from "@tsed/core";
|
|
11
|
+
* import {Constant, Value} from "@tsed/di";
|
|
12
|
+
*
|
|
13
|
+
* export class MyClass {
|
|
14
|
+
*
|
|
15
|
+
* @Constant("env")
|
|
16
|
+
* env: Env;
|
|
17
|
+
*
|
|
18
|
+
* @Value("swagger.path")
|
|
19
|
+
* swaggerPath: string;
|
|
20
|
+
*
|
|
21
|
+
* @Value("swagger.path", "defaultValue")
|
|
22
|
+
* swaggerPath: string;
|
|
23
|
+
*
|
|
24
|
+
* constructor() {
|
|
25
|
+
* console.log(this.swaggerPath) // undefined. Not available on constructor
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* $onInit() {
|
|
29
|
+
* console.log(this.swaggerPath) // something
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param expression
|
|
35
|
+
* @param defaultValue
|
|
36
|
+
* @returns {(targetClass: any, attributeName: string) => any}
|
|
37
|
+
* @decorator
|
|
38
|
+
*/
|
|
39
|
+
export function Value(expression, defaultValue) {
|
|
40
|
+
return (target, propertyKey) => {
|
|
41
|
+
Store.from(target).merge(INJECTABLE_PROP, {
|
|
42
|
+
[propertyKey]: {
|
|
43
|
+
bindingType: InjectablePropertyType.VALUE,
|
|
44
|
+
propertyKey,
|
|
45
|
+
expression,
|
|
46
|
+
defaultValue
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=value.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value.js","sourceRoot":"","sources":["../../../src/decorators/value.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AACjC,OAAO,EAAC,eAAe,EAAC,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAC,sBAAsB,EAAC,MAAM,kCAAkC,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,KAAK,CAAC,UAAe,EAAE,YAAkB;IACvD,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,eAAe,EAAE;YACxC,CAAC,WAAW,CAAC,EAAE;gBACb,WAAW,EAAE,sBAAsB,CAAC,KAAK;gBACzC,WAAW;gBACX,UAAU;gBACV,YAAY;aACb;SACsB,CAAC,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {Store} from \"@tsed/core\";\nimport {INJECTABLE_PROP} from \"../constants\";\nimport type {InjectableProperties} from \"../interfaces/InjectableProperties\";\nimport {InjectablePropertyType} from \"../domain/InjectablePropertyType\";\n\n/**\n * Return value from Configuration.\n *\n * ## Example\n *\n * ```typescript\n * import {Env} from \"@tsed/core\";\n * import {Constant, Value} from \"@tsed/di\";\n *\n * export class MyClass {\n *\n * @Constant(\"env\")\n * env: Env;\n *\n * @Value(\"swagger.path\")\n * swaggerPath: string;\n *\n * @Value(\"swagger.path\", \"defaultValue\")\n * swaggerPath: string;\n *\n * constructor() {\n * console.log(this.swaggerPath) // undefined. Not available on constructor\n * }\n *\n * $onInit() {\n * console.log(this.swaggerPath) // something\n * }\n * }\n * ```\n *\n * @param expression\n * @param defaultValue\n * @returns {(targetClass: any, attributeName: string) => any}\n * @decorator\n */\nexport function Value(expression: any, defaultValue?: any) {\n return (target: any, propertyKey: string) => {\n Store.from(target).merge(INJECTABLE_PROP, {\n [propertyKey]: {\n bindingType: InjectablePropertyType.VALUE,\n propertyKey,\n expression,\n defaultValue\n }\n } as InjectableProperties);\n };\n}\n"]}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { GlobalProviders } from "../registries/GlobalProviders";
|
|
2
|
+
import { Provider } from "./Provider";
|
|
3
|
+
export class Container extends Map {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param token
|
|
7
|
+
* @param settings
|
|
8
|
+
*/
|
|
9
|
+
add(token, settings = {}) {
|
|
10
|
+
var _a;
|
|
11
|
+
const provider = ((_a = GlobalProviders.get(token)) === null || _a === void 0 ? void 0 : _a.clone()) || new Provider(token);
|
|
12
|
+
Object.assign(provider, settings);
|
|
13
|
+
return super.set(token, provider);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Add a provider to the
|
|
17
|
+
* @param token
|
|
18
|
+
* @param settings
|
|
19
|
+
*/
|
|
20
|
+
addProvider(token, settings = {}) {
|
|
21
|
+
return this.add(token, settings);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param token
|
|
26
|
+
*/
|
|
27
|
+
hasProvider(token) {
|
|
28
|
+
return super.has(token);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Add a provider to the
|
|
32
|
+
* @param token
|
|
33
|
+
* @param provider
|
|
34
|
+
*/
|
|
35
|
+
setProvider(token, provider) {
|
|
36
|
+
return super.set(token, provider);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The getProvider() method returns a specified element from a Map object.
|
|
40
|
+
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
|
|
41
|
+
* @param token
|
|
42
|
+
*/
|
|
43
|
+
getProvider(token) {
|
|
44
|
+
return super.get(token);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get all providers registered in the injector container.
|
|
48
|
+
*
|
|
49
|
+
* @param {ProviderType} type Filter the list by the given ProviderType.
|
|
50
|
+
* @returns {[TokenProvider , Provider<any>][]}
|
|
51
|
+
*/
|
|
52
|
+
getProviders(type) {
|
|
53
|
+
return [...this].reduce((providers, [_, provider]) => {
|
|
54
|
+
if (provider.type === type || !type) {
|
|
55
|
+
return [...providers, provider];
|
|
56
|
+
}
|
|
57
|
+
return providers;
|
|
58
|
+
}, []);
|
|
59
|
+
}
|
|
60
|
+
addProviders(container) {
|
|
61
|
+
container.forEach((provider) => {
|
|
62
|
+
if (!this.hasProvider(provider.provide)) {
|
|
63
|
+
this.setProvider(provider.provide, provider.clone());
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=Container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Container.js","sourceRoot":"","sources":["../../../src/domain/Container.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,eAAe,EAAC,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAEpC,MAAM,OAAO,SAAU,SAAQ,GAA4B;IACzD;;;;OAIG;IACI,GAAG,CAAC,KAAoB,EAAE,WAA+B,EAAE;;QAChE,MAAM,QAAQ,GAAG,CAAA,MAAA,eAAe,CAAC,GAAG,CAAC,KAAK,CAA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AAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;aACtD;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {IProvider} from \"../interfaces/IProvider\";\nimport type {ProviderType} from \"./ProviderType\";\nimport type {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {GlobalProviders} from \"../registries/GlobalProviders\";\nimport {Provider} from \"./Provider\";\n\nexport class Container extends Map<TokenProvider, Provider> {\n /**\n *\n * @param token\n * @param settings\n */\n public add(token: TokenProvider, settings: Partial<IProvider> = {}): this {\n const provider = GlobalProviders.get(token)?.clone() || new Provider(token);\n\n Object.assign(provider, settings);\n\n return super.set(token, provider);\n }\n\n /**\n * Add a provider to the\n * @param token\n * @param settings\n */\n public addProvider(token: TokenProvider, settings: Partial<IProvider> = {}): this {\n return this.add(token, settings);\n }\n\n /**\n *\n * @param token\n */\n public hasProvider(token: TokenProvider) {\n return super.has(token);\n }\n\n /**\n * Add a provider to the\n * @param token\n * @param provider\n */\n public setProvider(token: TokenProvider, provider: Provider) {\n return super.set(token, provider);\n }\n\n /**\n * The getProvider() method returns a specified element from a 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 * @param token\n */\n public getProvider<T extends Provider = Provider>(token: TokenProvider): T | undefined {\n return super.get(token) as T;\n }\n\n /**\n * Get all providers registered in the injector container.\n *\n * @param {ProviderType} type Filter the list by the given ProviderType.\n * @returns {[TokenProvider , Provider<any>][]}\n */\n public getProviders(type?: ProviderType | string): Provider[] {\n return [...this].reduce((providers, [_, provider]) => {\n if (provider.type === type || !type) {\n return [...providers, provider];\n }\n return providers;\n }, []);\n }\n\n public addProviders(container: Map<TokenProvider, Provider>) {\n container.forEach((provider) => {\n if (!this.hasProvider(provider.provide)) {\n this.setProvider(provider.provide, provider.clone());\n }\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { levels } from "@tsed/logger";
|
|
2
|
+
export class ContextLogger {
|
|
3
|
+
constructor(logger, { id, url = "", dateStart = new Date(), ignoreUrlPatterns = [], minimalRequestPicker, completeRequestPicker, level = "all", maxStackSize = 30, ignoreLog, additionalProps }) {
|
|
4
|
+
this.logger = logger;
|
|
5
|
+
this.stack = [];
|
|
6
|
+
this.id = id;
|
|
7
|
+
this.url = url;
|
|
8
|
+
this.additionalProps = additionalProps || {};
|
|
9
|
+
this.dateStart = dateStart;
|
|
10
|
+
this.ignoreLog = ignoreLog || (() => false);
|
|
11
|
+
this.minimalRequestPicker = minimalRequestPicker || ((l) => l);
|
|
12
|
+
this.completeRequestPicker = completeRequestPicker || ((l) => l);
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
this.level = levels()[level.toUpperCase()] || levels().ALL;
|
|
15
|
+
this.maxStackSize = maxStackSize;
|
|
16
|
+
}
|
|
17
|
+
info(obj) {
|
|
18
|
+
this.run(levels().INFO, obj, (obj) => this.minimalRequestPicker(this.getData(obj)));
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
debug(obj, withRequest = true) {
|
|
22
|
+
this.run(levels().DEBUG, obj, (obj) => {
|
|
23
|
+
obj = this.getData(obj);
|
|
24
|
+
return withRequest ? this.completeRequestPicker(obj) : obj;
|
|
25
|
+
});
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
warn(obj) {
|
|
29
|
+
this.run(levels().WARN, obj, (obj) => this.completeRequestPicker(this.getData(obj)));
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
error(obj) {
|
|
33
|
+
this.run(levels().ERROR, obj, (obj) => this.completeRequestPicker(this.getData(obj)));
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
trace(obj) {
|
|
37
|
+
this.run(levels().TRACE, obj, (obj) => this.completeRequestPicker(this.getData(obj)));
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
flush() {
|
|
41
|
+
if (this.stack.length) {
|
|
42
|
+
this.stack.forEach(({ level, data }) => {
|
|
43
|
+
this.logger[level](data);
|
|
44
|
+
});
|
|
45
|
+
this.stack = [];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
isLevelEnabled(otherLevel) {
|
|
49
|
+
return this.level.isLessThanOrEqualTo(otherLevel);
|
|
50
|
+
}
|
|
51
|
+
destroy() {
|
|
52
|
+
this.flush();
|
|
53
|
+
this.logger = undefined;
|
|
54
|
+
this.stack = undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Return the duration between the time when LogIncomingRequest has handle the request and now.
|
|
58
|
+
* @returns {number}
|
|
59
|
+
*/
|
|
60
|
+
getDuration() {
|
|
61
|
+
return new Date().getTime() - this.dateStart.getTime();
|
|
62
|
+
}
|
|
63
|
+
getData(obj) {
|
|
64
|
+
if (typeof obj === "string") {
|
|
65
|
+
obj = { message: obj };
|
|
66
|
+
}
|
|
67
|
+
return { ...this.additionalProps, reqId: this.id, time: new Date(), duration: this.getDuration(), ...obj };
|
|
68
|
+
}
|
|
69
|
+
run(level, obj, mapper) {
|
|
70
|
+
if (!this.isLevelEnabled(level)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (!this.ignoreLog(obj)) {
|
|
74
|
+
this.stack.push({ level: level.levelStr.toLowerCase(), data: mapper(obj) });
|
|
75
|
+
}
|
|
76
|
+
if (this.maxStackSize < this.stack.length) {
|
|
77
|
+
this.flush();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ContextLogger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContextLogger.js","sourceRoot":"","sources":["../../../src/domain/ContextLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAW,MAAM,cAAc,CAAC;AAc9C,MAAM,OAAO,aAAa;IAYxB,YACU,MAAW,EACnB,EACE,EAAE,EACF,GAAG,GAAG,EAAE,EACR,SAAS,GAAG,IAAI,IAAI,EAAE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";\n\nexport interface ContextLoggerOptions extends Record<string, any> {\n id: string;\n url?: string;\n dateStart?: Date;\n level?: \"debug\" | \"info\" | \"warn\" | \"error\" | \"off\" | \"all\";\n maxStackSize?: number;\n minimalRequestPicker?: (o: any) => any;\n completeRequestPicker?: (o: any) => any;\n ignoreLog?: (data: any) => boolean;\n additionalProps?: Record<any, any>;\n}\n\nexport class ContextLogger {\n id: string;\n url: string;\n minimalRequestPicker: Function;\n completeRequestPicker: Function;\n maxStackSize: number;\n private readonly additionalProps: Record<string, unknown>;\n private readonly dateStart: Date;\n private readonly ignoreLog: (data: any) => boolean;\n private stack: any = [];\n private level: LogLevel;\n\n constructor(\n private logger: any,\n {\n id,\n url = \"\",\n dateStart = new Date(),\n ignoreUrlPatterns = [],\n minimalRequestPicker,\n completeRequestPicker,\n level = \"all\",\n maxStackSize = 30,\n ignoreLog,\n additionalProps\n }: ContextLoggerOptions\n ) {\n this.id = id;\n this.url = url;\n this.additionalProps = additionalProps || {};\n this.dateStart = dateStart;\n this.ignoreLog = ignoreLog || (() => false);\n this.minimalRequestPicker = minimalRequestPicker || ((l: any) => l);\n this.completeRequestPicker = completeRequestPicker || ((l: any) => l);\n // @ts-ignore\n this.level = levels()[level.toUpperCase()] || levels().ALL;\n this.maxStackSize = maxStackSize;\n }\n\n info(obj: any) {\n this.run(levels().INFO, obj, (obj) => this.minimalRequestPicker(this.getData(obj)));\n return this;\n }\n\n debug(obj: any, withRequest: boolean = true) {\n this.run(levels().DEBUG, obj, (obj) => {\n obj = this.getData(obj);\n return withRequest ? this.completeRequestPicker(obj) : obj;\n });\n return this;\n }\n\n warn(obj: any) {\n this.run(levels().WARN, obj, (obj) => this.completeRequestPicker(this.getData(obj)));\n return this;\n }\n\n error(obj: any) {\n this.run(levels().ERROR, obj, (obj) => this.completeRequestPicker(this.getData(obj)));\n return this;\n }\n\n trace(obj: any) {\n this.run(levels().TRACE, obj, (obj) => this.completeRequestPicker(this.getData(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 destroy() {\n this.flush();\n\n this.logger = undefined;\n this.stack = undefined;\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, mapper: (data: any) => any) {\n if (!this.isLevelEnabled(level)) {\n return;\n }\n\n if (!this.ignoreLog(obj)) {\n this.stack.push({level: level.levelStr.toLowerCase(), data: mapper(obj)});\n }\n\n if (this.maxStackSize < this.stack.length) {\n this.flush();\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ContextLogger } from "./ContextLogger";
|
|
2
|
+
import { LocalsContainer } from "./LocalsContainer";
|
|
3
|
+
export class DIContext extends Map {
|
|
4
|
+
constructor({ id, injector, logger, ignoreLog, ...options }) {
|
|
5
|
+
super();
|
|
6
|
+
/**
|
|
7
|
+
* Date when request have been handled by the server. @@RequestLogger@@ use this date to log request duration.
|
|
8
|
+
*/
|
|
9
|
+
this.dateStart = new Date();
|
|
10
|
+
/**
|
|
11
|
+
* The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
|
|
12
|
+
*/
|
|
13
|
+
this.container = new LocalsContainer();
|
|
14
|
+
this.id = id;
|
|
15
|
+
injector && (this.injector = injector);
|
|
16
|
+
this.logger = new ContextLogger(logger, {
|
|
17
|
+
...options,
|
|
18
|
+
dateStart: this.dateStart,
|
|
19
|
+
id,
|
|
20
|
+
ignoreLog
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
get env() {
|
|
24
|
+
return this.injector.settings.env;
|
|
25
|
+
}
|
|
26
|
+
async destroy() {
|
|
27
|
+
await this.container.destroy();
|
|
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;
|
|
35
|
+
}
|
|
36
|
+
async emit(eventName, ...args) {
|
|
37
|
+
var _a;
|
|
38
|
+
return (_a = this.injector) === null || _a === void 0 ? void 0 : _a.emit(eventName, ...args);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=DIContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,aAAa,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAwBlD,MAAM,OAAO,SAAU,SAAQ,GAAa;IA4C1C,YAAY,EAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,EAAmB;QACzE,KAAK,EAAE,CAAC;QAlBV;;WAEG;QACM,cAAS,GAAS,IAAI,IAAI,EAAE,CAAC;QACtC;;WAEG;QACI,cAAS,GAAG,IAAI,eAAe,EAAO,CAAC;QAY5C,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QAEb,QAAQ,IAAI,CAAC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,CAAC;QAEvC,IAAI,CAAC,MAAM,GAAG,IAAI,aAAa,CAAC,MAAM,EAAE;YACtC,GAAG,OAAO;YACV,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,EAAE;YACF,SAAS;SACV,CAAC,CAAC;IACL,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACtB,aAAa;QACb,OAAO,IAAI,CAAC,SAAS,CAAC;QACtB,aAAa;QACb,OAAO,IAAI,CAAC,MAAM,CAAC;QACnB,aAAa;QACb,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,SAAiB,EAAE,GAAG,IAAW;;QAC1C,OAAO,MAAA,IAAI,CAAC,QAAQ,0CAAE,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC;CACF","sourcesContent":["import type {Env} from \"@tsed/core\";\nimport {InjectorService} from \"../services/InjectorService\";\nimport {ContextLogger, ContextLoggerOptions} from \"./ContextLogger\";\nimport {LocalsContainer} from \"./LocalsContainer\";\n\nexport interface ContextMethods extends Map<any, any> {\n readonly id: string;\n readonly logger: ContextLogger;\n readonly injector: InjectorService;\n readonly container: LocalsContainer;\n readonly env: Env;\n\n destroy(): any;\n}\n\ndeclare global {\n namespace TsED {\n interface Context {}\n }\n}\n\nexport interface DIContextOptions extends Omit<ContextLoggerOptions, \"dateStart\"> {\n id: string;\n logger: any;\n injector?: InjectorService;\n}\n\nexport class DIContext extends Map<any, any> implements ContextMethods {\n [x: string]: any;\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 readonly id: string;\n /**\n * Date when request have been handled by the server. @@RequestLogger@@ use this date to log request duration.\n */\n readonly dateStart: Date = new Date();\n /**\n * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`\n */\n public container = new LocalsContainer<any>();\n /**\n * Logger attached to the context request.\n */\n public logger: ContextLogger;\n /**\n *\n */\n public injector: InjectorService;\n\n constructor({id, injector, logger, ignoreLog, ...options}: DIContextOptions) {\n super();\n this.id = id;\n\n injector && (this.injector = injector);\n\n this.logger = new ContextLogger(logger, {\n ...options,\n dateStart: this.dateStart,\n id,\n ignoreLog\n });\n }\n\n get env() {\n return this.injector.settings.env;\n }\n\n async destroy() {\n await this.container.destroy();\n this.logger.destroy();\n // @ts-ignore\n delete this.container;\n // @ts-ignore\n delete this.logger;\n // @ts-ignore\n delete this.injector;\n }\n\n async emit(eventName: string, ...args: any[]) {\n return this.injector?.emit(eventName, ...args);\n }\n}\n\nexport type BaseContext = DIContext & TsED.Context;\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export var InjectablePropertyType;
|
|
2
|
+
(function (InjectablePropertyType) {
|
|
3
|
+
InjectablePropertyType["METHOD"] = "method";
|
|
4
|
+
InjectablePropertyType["PROPERTY"] = "property";
|
|
5
|
+
InjectablePropertyType["CONSTANT"] = "constant";
|
|
6
|
+
InjectablePropertyType["VALUE"] = "value";
|
|
7
|
+
InjectablePropertyType["INTERCEPTOR"] = "interceptor";
|
|
8
|
+
})(InjectablePropertyType || (InjectablePropertyType = {}));
|
|
9
|
+
//# sourceMappingURL=InjectablePropertyType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InjectablePropertyType.js","sourceRoot":"","sources":["../../../src/domain/InjectablePropertyType.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,sBAMX;AAND,WAAY,sBAAsB;IAChC,2CAAiB,CAAA;IACjB,+CAAqB,CAAA;IACrB,+CAAqB,CAAA;IACrB,yCAAe,CAAA;IACf,qDAA2B,CAAA;AAC7B,CAAC,EANW,sBAAsB,KAAtB,sBAAsB,QAMjC","sourcesContent":["export enum InjectablePropertyType {\n METHOD = \"method\",\n PROPERTY = \"property\",\n CONSTANT = \"constant\",\n VALUE = \"value\",\n INTERCEPTOR = \"interceptor\"\n}\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
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
|
+
}
|
|
38
|
+
async destroy() {
|
|
39
|
+
await this.emit("$onDestroy");
|
|
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
|
+
}, []);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=LocalsContainer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalsContainer.js","sourceRoot":"","sources":["../../../src/domain/LocalsContainer.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,eAAyB,SAAQ,GAAqB;IACjE;;;;;OAKG;IACI,KAAK,CAAC,IAAI,CAAC,SAAiB,EAAE,GAAG,IAAW;QACjD,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,EAAE;YAClD,MAAM,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;SACxB;IACH,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAU,SAAiB,EAAE,KAAU,EAAE,GAAG,IAAW;QACjE,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,EAAE;YAClD,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;SACjC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,UAAU,CAAU,SAAiB,EAAE,KAAU,EAAE,GAAG,IAAW;QAC5E,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,EAAE;YAClD,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;SACjC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO;QACL,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAC9B,IAAI,CAAC,KAAK,EAAE,CAAC;IACf,CAAC;IAES,YAAY,CAAC,KAAa;QAClC,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE;YACnD,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,IAAI,KAAK,IAAI,QAAQ,EAAE;gBACjE,OAAO,SAAS,CAAC,MAAM,CAAE,QAAgB,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;aAClE;YAED,OAAO,SAAS,CAAC;QACnB,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;CACF","sourcesContent":["import type {TokenProvider} from \"../interfaces/TokenProvider\";\n\nexport class LocalsContainer<V = any> extends Map<TokenProvider, V> {\n /**\n * Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).\n * @param eventName The event name to emit at all services.\n * @param args List of the parameters to give to each services.\n * @returns {Promise<any[]>} A list of promises.\n */\n public async emit(eventName: string, ...args: any[]) {\n for (const handler of this.getListeners(eventName)) {\n await handler(...args);\n }\n }\n\n /**\n * @param eventName\n * @param value\n * @param args\n */\n public alter<T = any>(eventName: string, value: any, ...args: any[]): T {\n for (const handler of this.getListeners(eventName)) {\n value = handler(value, ...args);\n }\n\n return value;\n }\n\n /**\n * @param eventName\n * @param value\n * @param args\n */\n public async alterAsync<T = any>(eventName: string, value: any, ...args: any[]): Promise<T> {\n for (const handler of this.getListeners(eventName)) {\n value = handler(value, ...args);\n }\n\n return value;\n }\n\n toArray() {\n return [...this.values()];\n }\n\n async destroy() {\n await this.emit(\"$onDestroy\");\n this.clear();\n }\n\n protected getListeners(event: string): any[] {\n return this.toArray().reduce((listeners, instance) => {\n if (typeof instance === \"object\" && instance && event in instance) {\n return listeners.concat((instance as any)[event].bind(instance));\n }\n\n return listeners;\n }, []);\n }\n}\n"]}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { classOf, getClassOrSymbol, isClass, nameOf, Store } from "@tsed/core";
|
|
2
|
+
import { ProviderScope } from "./ProviderScope";
|
|
3
|
+
import { ProviderType } from "./ProviderType";
|
|
4
|
+
export class Provider {
|
|
5
|
+
constructor(token, options = {}) {
|
|
6
|
+
this.type = ProviderType.PROVIDER;
|
|
7
|
+
this.provide = token;
|
|
8
|
+
this.useClass = token;
|
|
9
|
+
Object.assign(this, options);
|
|
10
|
+
}
|
|
11
|
+
get token() {
|
|
12
|
+
return this._provide;
|
|
13
|
+
}
|
|
14
|
+
get provide() {
|
|
15
|
+
return this._provide;
|
|
16
|
+
}
|
|
17
|
+
set provide(value) {
|
|
18
|
+
if (value) {
|
|
19
|
+
this._provide = getClassOrSymbol(value);
|
|
20
|
+
this._tokenStore = this._store = Store.from(value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
get useClass() {
|
|
24
|
+
return this._useClass;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a new store if the given value is a class. Otherwise the value is ignored.
|
|
28
|
+
* @param value
|
|
29
|
+
*/
|
|
30
|
+
set useClass(value) {
|
|
31
|
+
if (isClass(value)) {
|
|
32
|
+
this._useClass = classOf(value);
|
|
33
|
+
this._store = Store.from(value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
get className() {
|
|
37
|
+
return this.name;
|
|
38
|
+
}
|
|
39
|
+
get name() {
|
|
40
|
+
return nameOf(this.provide);
|
|
41
|
+
}
|
|
42
|
+
get store() {
|
|
43
|
+
return this._store;
|
|
44
|
+
}
|
|
45
|
+
get path() {
|
|
46
|
+
return this.store.get("path", "/");
|
|
47
|
+
}
|
|
48
|
+
set path(path) {
|
|
49
|
+
this.store.set("path", path);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get the scope of the provider.
|
|
53
|
+
*
|
|
54
|
+
* ::: tip Note
|
|
55
|
+
* Async provider is always a SINGLETON
|
|
56
|
+
* :::
|
|
57
|
+
*
|
|
58
|
+
* @returns {boolean}
|
|
59
|
+
*/
|
|
60
|
+
get scope() {
|
|
61
|
+
if (this.isAsync()) {
|
|
62
|
+
return ProviderScope.SINGLETON;
|
|
63
|
+
}
|
|
64
|
+
return this.get("scope");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Change the scope value of the provider.
|
|
68
|
+
* @param scope
|
|
69
|
+
*/
|
|
70
|
+
set scope(scope) {
|
|
71
|
+
this.store.set("scope", scope);
|
|
72
|
+
}
|
|
73
|
+
get configuration() {
|
|
74
|
+
return this.get("configuration");
|
|
75
|
+
}
|
|
76
|
+
set configuration(configuration) {
|
|
77
|
+
this.store.set("configuration", configuration);
|
|
78
|
+
}
|
|
79
|
+
get children() {
|
|
80
|
+
return this.store.get("childrenControllers", []);
|
|
81
|
+
}
|
|
82
|
+
get(key) {
|
|
83
|
+
return this.store.get(key) || this._tokenStore.get(key);
|
|
84
|
+
}
|
|
85
|
+
isAsync() {
|
|
86
|
+
return !!this.useAsyncFactory;
|
|
87
|
+
}
|
|
88
|
+
clone() {
|
|
89
|
+
return new (classOf(this))(this._provide, this);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
*
|
|
93
|
+
* @returns {boolean}
|
|
94
|
+
*/
|
|
95
|
+
hasChildren() {
|
|
96
|
+
return !!this.children.length;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
*
|
|
100
|
+
* @returns {boolean}
|
|
101
|
+
*/
|
|
102
|
+
hasParent() {
|
|
103
|
+
return !!this.store.get("parentController");
|
|
104
|
+
}
|
|
105
|
+
toString() {
|
|
106
|
+
return [
|
|
107
|
+
"Token",
|
|
108
|
+
this.name,
|
|
109
|
+
this.useClass && nameOf(this.useClass),
|
|
110
|
+
this.useFactory && "Factory",
|
|
111
|
+
this.useValue && "Value",
|
|
112
|
+
this.useAsyncFactory && "AsyncFactory"
|
|
113
|
+
]
|
|
114
|
+
.filter(Boolean)
|
|
115
|
+
.join(":");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=Provider.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OAAO,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC;IAChC,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;IAED;;;OAGG;IACI,WAAW;QAChB,OAAO,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAChC,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;IAC9C,CAAC;IAED,QAAQ;QACN,OAAO;YACL,OAAO;YACP,IAAI,CAAC,IAAI;YACT,IAAI,CAAC,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;YACtC,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, nameOf, Store, Type} from \"@tsed/core\";\nimport {IProvider, TokenProvider} from \"../interfaces\";\nimport {ProviderScope} from \"./ProviderScope\";\nimport {ProviderType} from \"./ProviderType\";\n\nexport class Provider<T = any> implements IProvider<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 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 }\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"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export var ProviderScope;
|
|
2
|
+
(function (ProviderScope) {
|
|
3
|
+
ProviderScope["SINGLETON"] = "singleton";
|
|
4
|
+
ProviderScope["REQUEST"] = "request";
|
|
5
|
+
ProviderScope["INSTANCE"] = "instance";
|
|
6
|
+
})(ProviderScope || (ProviderScope = {}));
|
|
7
|
+
//# sourceMappingURL=ProviderScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProviderScope.js","sourceRoot":"","sources":["../../../src/domain/ProviderScope.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,aAIX;AAJD,WAAY,aAAa;IACvB,wCAAuB,CAAA;IACvB,oCAAmB,CAAA;IACnB,sCAAqB,CAAA;AACvB,CAAC,EAJW,aAAa,KAAb,aAAa,QAIxB","sourcesContent":["export enum ProviderScope {\n SINGLETON = \"singleton\",\n REQUEST = \"request\",\n INSTANCE = \"instance\"\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export var ProviderType;
|
|
2
|
+
(function (ProviderType) {
|
|
3
|
+
ProviderType["VALUE"] = "value";
|
|
4
|
+
ProviderType["FACTORY"] = "factory";
|
|
5
|
+
ProviderType["SERVICE"] = "service";
|
|
6
|
+
ProviderType["PROVIDER"] = "provider";
|
|
7
|
+
ProviderType["MODULE"] = "module";
|
|
8
|
+
ProviderType["CONTROLLER"] = "controller";
|
|
9
|
+
ProviderType["INTERCEPTOR"] = "interceptor";
|
|
10
|
+
ProviderType["MIDDLEWARE"] = "middleware";
|
|
11
|
+
})(ProviderType || (ProviderType = {}));
|
|
12
|
+
//# sourceMappingURL=ProviderType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProviderType.js","sourceRoot":"","sources":["../../../src/domain/ProviderType.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,YASX;AATD,WAAY,YAAY;IACtB,+BAAe,CAAA;IACf,mCAAmB,CAAA;IACnB,mCAAmB,CAAA;IACnB,qCAAqB,CAAA;IACrB,iCAAiB,CAAA;IACjB,yCAAyB,CAAA;IACzB,2CAA2B,CAAA;IAC3B,yCAAyB,CAAA;AAC3B,CAAC,EATW,YAAY,KAAZ,YAAY,QASvB","sourcesContent":["export enum ProviderType {\n VALUE = \"value\",\n FACTORY = \"factory\",\n SERVICE = \"service\",\n PROVIDER = \"provider\",\n MODULE = \"module\",\n CONTROLLER = \"controller\",\n INTERCEPTOR = \"interceptor\",\n MIDDLEWARE = \"middleware\"\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./Container";
|
|
2
|
+
export * from "./InjectablePropertyType";
|
|
3
|
+
export * from "./LocalsContainer";
|
|
4
|
+
export * from "./Provider";
|
|
5
|
+
export * from "./ProviderScope";
|
|
6
|
+
export * from "./ProviderType";
|
|
7
|
+
export * from "./ContextLogger";
|
|
8
|
+
export * from "./DIContext";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/domain/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC","sourcesContent":["export * from \"./Container\";\nexport * from \"./InjectablePropertyType\";\nexport * from \"./LocalsContainer\";\nexport * from \"./Provider\";\nexport * from \"./ProviderScope\";\nexport * from \"./ProviderType\";\nexport * from \"./ContextLogger\";\nexport * from \"./DIContext\";\n"]}
|