@tsed/di 6.114.5-rc.1 → 6.114.5

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.
Files changed (78) hide show
  1. package/lib/cjs/constants/constants.js +3 -1
  2. package/lib/cjs/constants/constants.js.map +1 -1
  3. package/lib/cjs/decorators/constant.js +9 -8
  4. package/lib/cjs/decorators/constant.js.map +1 -1
  5. package/lib/cjs/decorators/inject.js +28 -26
  6. package/lib/cjs/decorators/inject.js.map +1 -1
  7. package/lib/cjs/decorators/intercept.js +10 -30
  8. package/lib/cjs/decorators/intercept.js.map +1 -1
  9. package/lib/cjs/decorators/lazyInject.js +14 -13
  10. package/lib/cjs/decorators/lazyInject.js.map +1 -1
  11. package/lib/cjs/decorators/useOpts.js +5 -1
  12. package/lib/cjs/decorators/useOpts.js.map +1 -1
  13. package/lib/cjs/decorators/value.js +9 -9
  14. package/lib/cjs/decorators/value.js.map +1 -1
  15. package/lib/cjs/domain/DIContext.js +1 -1
  16. package/lib/cjs/domain/DIContext.js.map +1 -1
  17. package/lib/cjs/index.js +1 -3
  18. package/lib/cjs/index.js.map +1 -1
  19. package/lib/cjs/interfaces/{DILoggerOptions.js → InjectableProperties.js} +1 -1
  20. package/lib/cjs/interfaces/InjectableProperties.js.map +1 -0
  21. package/lib/cjs/services/DIConfiguration.js +43 -49
  22. package/lib/cjs/services/DIConfiguration.js.map +1 -1
  23. package/lib/cjs/services/DITest.js +2 -2
  24. package/lib/cjs/services/DITest.js.map +1 -1
  25. package/lib/cjs/services/InjectorService.js +133 -19
  26. package/lib/cjs/services/InjectorService.js.map +1 -1
  27. package/lib/cjs/utils/setLoggerLevel.js +2 -1
  28. package/lib/cjs/utils/setLoggerLevel.js.map +1 -1
  29. package/lib/esm/constants/constants.js +2 -0
  30. package/lib/esm/constants/constants.js.map +1 -1
  31. package/lib/esm/decorators/constant.js +10 -9
  32. package/lib/esm/decorators/constant.js.map +1 -1
  33. package/lib/esm/decorators/inject.js +30 -28
  34. package/lib/esm/decorators/inject.js.map +1 -1
  35. package/lib/esm/decorators/intercept.js +11 -31
  36. package/lib/esm/decorators/intercept.js.map +1 -1
  37. package/lib/esm/decorators/lazyInject.js +14 -13
  38. package/lib/esm/decorators/lazyInject.js.map +1 -1
  39. package/lib/esm/decorators/useOpts.js +6 -2
  40. package/lib/esm/decorators/useOpts.js.map +1 -1
  41. package/lib/esm/decorators/value.js +10 -10
  42. package/lib/esm/decorators/value.js.map +1 -1
  43. package/lib/esm/domain/DIContext.js +1 -1
  44. package/lib/esm/domain/DIContext.js.map +1 -1
  45. package/lib/esm/index.js +1 -3
  46. package/lib/esm/index.js.map +1 -1
  47. package/lib/esm/interfaces/InjectableProperties.js +2 -0
  48. package/lib/esm/interfaces/InjectableProperties.js.map +1 -0
  49. package/lib/esm/services/DIConfiguration.js +44 -50
  50. package/lib/esm/services/DIConfiguration.js.map +1 -1
  51. package/lib/esm/services/DITest.js +2 -2
  52. package/lib/esm/services/DITest.js.map +1 -1
  53. package/lib/esm/services/InjectorService.js +135 -21
  54. package/lib/esm/services/InjectorService.js.map +1 -1
  55. package/lib/esm/utils/setLoggerLevel.js +2 -1
  56. package/lib/esm/utils/setLoggerLevel.js.map +1 -1
  57. package/lib/types/constants/constants.d.ts +2 -0
  58. package/lib/types/decorators/inject.d.ts +2 -5
  59. package/lib/types/index.d.ts +1 -3
  60. package/lib/types/interfaces/InjectableProperties.d.ts +24 -0
  61. package/lib/types/services/DIConfiguration.d.ts +12 -17
  62. package/lib/types/services/InjectorService.d.ts +50 -5
  63. package/lib/types/utils/setLoggerLevel.d.ts +1 -1
  64. package/package.json +6 -9
  65. package/lib/cjs/interfaces/DILoggerOptions.js.map +0 -1
  66. package/lib/cjs/utils/setLoggerConfiguration.js +0 -11
  67. package/lib/cjs/utils/setLoggerConfiguration.js.map +0 -1
  68. package/lib/cjs/utils/setLoggerFormat.js +0 -33
  69. package/lib/cjs/utils/setLoggerFormat.js.map +0 -1
  70. package/lib/esm/interfaces/DILoggerOptions.js +0 -2
  71. package/lib/esm/interfaces/DILoggerOptions.js.map +0 -1
  72. package/lib/esm/utils/setLoggerConfiguration.js +0 -7
  73. package/lib/esm/utils/setLoggerConfiguration.js.map +0 -1
  74. package/lib/esm/utils/setLoggerFormat.js +0 -29
  75. package/lib/esm/utils/setLoggerFormat.js.map +0 -1
  76. package/lib/types/interfaces/DILoggerOptions.d.ts +0 -44
  77. package/lib/types/utils/setLoggerConfiguration.d.ts +0 -2
  78. package/lib/types/utils/setLoggerFormat.d.ts +0 -6
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DI_PARAM_OPTIONS = void 0;
3
+ exports.DI_PARAM_OPTIONS = exports.DI_PARAMS = exports.INJECTABLE_PROP = void 0;
4
+ exports.INJECTABLE_PROP = "DI:INJECTABLE_PROP";
5
+ exports.DI_PARAMS = "DI:PARAMS";
4
6
  exports.DI_PARAM_OPTIONS = "DI:PARAM:OPTIONS";
5
7
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAG,kBAAkB,CAAC","sourcesContent":["export const DI_PARAM_OPTIONS = \"DI:PARAM:OPTIONS\";\n"]}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG,oBAAoB,CAAC;AACvC,QAAA,SAAS,GAAG,WAAW,CAAC;AACxB,QAAA,gBAAgB,GAAG,kBAAkB,CAAC","sourcesContent":["export const INJECTABLE_PROP = \"DI:INJECTABLE_PROP\";\nexport const DI_PARAMS = \"DI:PARAMS\";\nexport const DI_PARAM_OPTIONS = \"DI:PARAM:OPTIONS\";\n"]}
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Constant = void 0;
4
4
  const core_1 = require("@tsed/core");
5
+ const constants_1 = require("../constants/constants");
6
+ const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
5
7
  /**
6
8
  * Return value from Configuration.
7
9
  *
@@ -39,14 +41,13 @@ const core_1 = require("@tsed/core");
39
41
  */
40
42
  function Constant(expression, defaultValue) {
41
43
  return (target, propertyKey) => {
42
- Object.defineProperty((0, core_1.prototypeOf)(target), propertyKey, {
43
- get() {
44
- const injector = this.$$injector;
45
- const value = injector.settings.get(expression, defaultValue);
46
- return Object.freeze((0, core_1.deepClone)(value));
47
- },
48
- enumerable: true,
49
- configurable: true
44
+ core_1.Store.from(target).merge(constants_1.INJECTABLE_PROP, {
45
+ [propertyKey]: {
46
+ bindingType: InjectablePropertyType_1.InjectablePropertyType.CONSTANT,
47
+ propertyKey,
48
+ expression,
49
+ defaultValue
50
+ }
50
51
  });
51
52
  };
52
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"constant.js","sourceRoot":"","sources":["../../../src/decorators/constant.ts"],"names":[],"mappings":";;;AAAA,qCAAkD;AAGlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,QAAQ,CAAC,UAAkB,EAAE,YAAkB;IAC7D,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,MAAM,CAAC,cAAc,CAAC,IAAA,kBAAW,EAAC,MAAM,CAAC,EAAE,WAAW,EAAE;YACtD,GAAG;gBACD,MAAM,QAAQ,GAAG,IAAI,CAAC,UAA6B,CAAC;gBACpD,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;gBAE9D,OAAO,MAAM,CAAC,MAAM,CAAC,IAAA,gBAAS,EAAC,KAAK,CAAC,CAAC,CAAC;YACzC,CAAC;YACD,UAAU,EAAE,IAAI;YAChB,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAbD,4BAaC","sourcesContent":["import {deepClone, prototypeOf} from \"@tsed/core\";\nimport type {InjectorService} from \"../services/InjectorService\";\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 {string} expression\n * @param defaultValue\n * @returns {(targetClass: any, attributeName: string) => any}\n * @decorator\n */\nexport function Constant(expression: string, defaultValue?: any): any {\n return (target: any, propertyKey: string) => {\n Object.defineProperty(prototypeOf(target), propertyKey, {\n get() {\n const injector = this.$$injector as InjectorService;\n const value = injector.settings.get(expression, defaultValue);\n\n return Object.freeze(deepClone(value));\n },\n enumerable: true,\n configurable: true\n });\n };\n}\n"]}
1
+ {"version":3,"file":"constant.js","sourceRoot":"","sources":["../../../src/decorators/constant.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AACjC,sDAAuD;AAEvD,6EAAwE;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,QAAQ,CAAC,UAAkB,EAAE,YAAkB;IAC7D,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,2BAAe,EAAE;YACxC,CAAC,WAAW,CAAC,EAAE;gBACb,WAAW,EAAE,+CAAsB,CAAC,QAAQ;gBAC5C,WAAW;gBACX,UAAU;gBACV,YAAY;aACb;SACsB,CAAC,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC;AAXD,4BAWC","sourcesContent":["import {Store} from \"@tsed/core\";\nimport {INJECTABLE_PROP} from \"../constants/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 {string} expression\n * @param defaultValue\n * @returns {(targetClass: any, attributeName: string) => any}\n * @decorator\n */\nexport function Constant(expression: string, defaultValue?: any): any {\n return (target: any, propertyKey: string) => {\n Store.from(target).merge(INJECTABLE_PROP, {\n [propertyKey]: {\n bindingType: InjectablePropertyType.CONSTANT,\n propertyKey,\n expression,\n defaultValue\n }\n } as InjectableProperties);\n };\n}\n"]}
@@ -4,23 +4,12 @@ exports.InjectContext = exports.Inject = exports.injectProperty = void 0;
4
4
  const core_1 = require("@tsed/core");
5
5
  const constants_1 = require("../constants/constants");
6
6
  const runInContext_1 = require("../utils/runInContext");
7
- function injectProperty(target, propertyKey, resolver) {
8
- Object.defineProperty((0, core_1.prototypeOf)(target), propertyKey, {
9
- enumerable: false,
10
- configurable: true,
11
- get() {
12
- const key = `$$getter_${propertyKey}`;
13
- if (!this[key]) {
14
- const fn = resolver(this.$$injector, this.$$locals, this.$$invokeOptions);
15
- Object.defineProperty(this, key, {
16
- configurable: true,
17
- enumerable: false,
18
- get() {
19
- return fn;
20
- }
21
- });
22
- }
23
- return this[key]();
7
+ function injectProperty(target, propertyKey, options) {
8
+ core_1.Store.from(target).merge(constants_1.INJECTABLE_PROP, {
9
+ [propertyKey]: {
10
+ bindingType: core_1.DecoratorTypes.PROP,
11
+ propertyKey,
12
+ ...options
24
13
  }
25
14
  });
26
15
  }
@@ -56,23 +45,32 @@ function Inject(symbol, onGet = (bean) => bean) {
56
45
  }
57
46
  break;
58
47
  case core_1.DecoratorTypes.PROP:
59
- injectProperty(target, propertyKey, (injector, locals, invokeOptions) => {
60
- const useType = symbol || core_1.Metadata.getType(target, propertyKey);
61
- let bean;
62
- if (!bean) {
63
- const options = core_1.Store.from(target, propertyKey).get(constants_1.DI_PARAM_OPTIONS);
48
+ injectProperty(target, propertyKey, {
49
+ resolver(injector, locals, { options, ...invokeOptions }) {
64
50
  locals.set(constants_1.DI_PARAM_OPTIONS, { ...options });
65
- bean = injector.invoke(useType, locals, invokeOptions);
66
- locals.delete(constants_1.DI_PARAM_OPTIONS);
51
+ let bean;
52
+ if (!bean) {
53
+ const useType = symbol || core_1.Metadata.getType(target, propertyKey);
54
+ bean = injector.invoke(useType, locals, invokeOptions);
55
+ locals.delete(constants_1.DI_PARAM_OPTIONS);
56
+ }
67
57
  if ((0, core_1.isPromise)(bean)) {
68
58
  bean.then((result) => {
69
59
  bean = result;
70
60
  });
71
61
  }
62
+ return () => onGet(bean);
72
63
  }
73
- return () => onGet(bean);
74
64
  });
75
65
  break;
66
+ case core_1.DecoratorTypes.METHOD:
67
+ core_1.Store.from(target).merge(constants_1.INJECTABLE_PROP, {
68
+ [propertyKey]: {
69
+ bindingType,
70
+ propertyKey
71
+ }
72
+ });
73
+ return descriptor;
76
74
  default:
77
75
  throw new core_1.UnsupportedDecoratorType(Inject, [target, propertyKey, descriptor]);
78
76
  }
@@ -95,7 +93,11 @@ exports.Inject = Inject;
95
93
  */
96
94
  function InjectContext() {
97
95
  return (target, propertyKey) => {
98
- injectProperty(target, propertyKey, () => () => (0, runInContext_1.getContext)());
96
+ injectProperty(target, propertyKey, {
97
+ resolver() {
98
+ return () => (0, runInContext_1.getContext)();
99
+ }
100
+ });
99
101
  };
100
102
  }
101
103
  exports.InjectContext = InjectContext;
@@ -1 +1 @@
1
- {"version":3,"file":"inject.js","sourceRoot":"","sources":["../../../src/decorators/inject.ts"],"names":[],"mappings":"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","sourcesContent":["import {decoratorTypeOf, DecoratorTypes, isPromise, Metadata, prototypeOf, Store, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {DI_PARAM_OPTIONS} from \"../constants/constants\";\nimport {InvokeOptions} from \"../interfaces/InvokeOptions\";\nimport {TokenProvider} from \"../interfaces/TokenProvider\";\nimport {InjectorService} from \"../services/InjectorService\";\nimport {getContext} from \"../utils/runInContext\";\n\nexport type InjectPropertyResolver = (\n injector: InjectorService,\n locals: Map<TokenProvider, any>,\n options: Partial<InvokeOptions>\n) => () => any;\n\nexport function injectProperty(target: any, propertyKey: string, resolver: InjectPropertyResolver) {\n Object.defineProperty(prototypeOf(target), propertyKey, {\n enumerable: false,\n configurable: true,\n get() {\n const key = `$$getter_${propertyKey}`;\n\n if (!this[key]) {\n const fn = resolver(this.$$injector, this.$$locals, this.$$invokeOptions);\n Object.defineProperty(this, key, {\n configurable: true,\n enumerable: false,\n get() {\n return fn;\n }\n });\n }\n\n return this[key]();\n }\n });\n}\n\n/**\n * Inject a provider to another provider.\n *\n * Use this decorator to inject a custom provider on constructor parameter or property.\n *\n * ```typescript\n * @Injectable()\n * export class MyService {\n * @Inject(CONNECTION)\n * connection: CONNECTION;\n * }\n * ```\n *\n * @param symbol\n * @param onGet Use the given name method to inject\n * @returns {Function}\n * @decorator\n */\nexport function Inject(symbol?: any, onGet = (bean: any) => bean): Function {\n return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<Function> | number): any | void => {\n const bindingType = decoratorTypeOf([target, propertyKey, descriptor]);\n\n switch (bindingType) {\n case DecoratorTypes.PARAM:\n case DecoratorTypes.PARAM_CTOR:\n if (symbol) {\n const paramTypes = Metadata.getParamTypes(target, propertyKey);\n\n paramTypes[descriptor as number] = symbol;\n Metadata.setParamTypes(target, propertyKey, paramTypes);\n }\n break;\n\n case DecoratorTypes.PROP:\n injectProperty(target, propertyKey, (injector, locals, invokeOptions) => {\n const useType = symbol || Metadata.getType(target, propertyKey);\n\n let bean: any;\n\n if (!bean) {\n const options = Store.from(target, propertyKey).get(DI_PARAM_OPTIONS);\n locals.set(DI_PARAM_OPTIONS, {...options});\n\n bean = injector.invoke(useType, locals, invokeOptions);\n\n locals.delete(DI_PARAM_OPTIONS);\n\n if (isPromise(bean)) {\n bean.then((result: any) => {\n bean = result;\n });\n }\n }\n\n return () => onGet(bean);\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(Inject, [target, propertyKey, descriptor]);\n }\n };\n}\n\n/**\n * Inject a context like PlatformContext or any BaseContext.\n *\n * ```typescript\n * @Injectable()\n * export class MyService {\n * @InjectContext()\n * ctx?: Context;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n */\nexport function InjectContext(): PropertyDecorator {\n return (target: any, propertyKey: string): any | void => {\n injectProperty(target, propertyKey, () => () => getContext());\n };\n}\n"]}
1
+ {"version":3,"file":"inject.js","sourceRoot":"","sources":["../../../src/decorators/inject.ts"],"names":[],"mappings":"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","sourcesContent":["import {decoratorTypeOf, DecoratorTypes, isPromise, Metadata, Store, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {DI_PARAM_OPTIONS, INJECTABLE_PROP} from \"../constants/constants\";\nimport type {InjectablePropertyOptions} from \"../interfaces/InjectableProperties\";\nimport {getContext} from \"../utils/runInContext\";\n\nexport function injectProperty(target: any, propertyKey: string, options: Partial<InjectablePropertyOptions>) {\n Store.from(target).merge(INJECTABLE_PROP, {\n [propertyKey]: {\n bindingType: DecoratorTypes.PROP,\n propertyKey,\n ...options\n }\n });\n}\n\n/**\n * Inject a provider to another provider.\n *\n * Use this decorator to inject a custom provider on constructor parameter or property.\n *\n * ```typescript\n * @Injectable()\n * export class MyService {\n * @Inject(CONNECTION)\n * connection: CONNECTION;\n * }\n * ```\n *\n * @param symbol\n * @param onGet Use the given name method to inject\n * @returns {Function}\n * @decorator\n */\nexport function Inject(symbol?: any, onGet = (bean: any) => bean): Function {\n return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<Function> | number): any | void => {\n const bindingType = decoratorTypeOf([target, propertyKey, descriptor]);\n\n switch (bindingType) {\n case DecoratorTypes.PARAM:\n case DecoratorTypes.PARAM_CTOR:\n if (symbol) {\n const paramTypes = Metadata.getParamTypes(target, propertyKey);\n\n paramTypes[descriptor as number] = symbol;\n Metadata.setParamTypes(target, propertyKey, paramTypes);\n }\n break;\n\n case DecoratorTypes.PROP:\n injectProperty(target, propertyKey, {\n resolver(injector, locals, {options, ...invokeOptions}) {\n locals.set(DI_PARAM_OPTIONS, {...options});\n\n let bean: any;\n\n if (!bean) {\n const useType = symbol || Metadata.getType(target, propertyKey);\n bean = injector.invoke(useType, locals, invokeOptions);\n locals.delete(DI_PARAM_OPTIONS);\n }\n\n if (isPromise(bean)) {\n bean.then((result: any) => {\n bean = result;\n });\n }\n\n return () => onGet(bean);\n }\n });\n break;\n\n case DecoratorTypes.METHOD:\n Store.from(target).merge(INJECTABLE_PROP, {\n [propertyKey]: {\n bindingType,\n propertyKey\n }\n });\n\n return descriptor;\n\n default:\n throw new UnsupportedDecoratorType(Inject, [target, propertyKey, descriptor]);\n }\n };\n}\n\n/**\n * Inject a context like PlatformContext or any BaseContext.\n *\n * ```typescript\n * @Injectable()\n * export class MyService {\n * @InjectContext()\n * ctx?: Context;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n */\nexport function InjectContext(): PropertyDecorator {\n return (target: any, propertyKey: string): any | void => {\n injectProperty(target, propertyKey, {\n resolver() {\n return () => getContext();\n }\n });\n };\n}\n"]}
@@ -2,33 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Intercept = void 0;
4
4
  const core_1 = require("@tsed/core");
5
- function bindMethod(target, propertyKey, descriptor, { options, useType }) {
6
- const originalMethod = descriptor.value;
7
- return {
8
- ...descriptor,
9
- value(...args) {
10
- const injector = this.$$injector;
11
- const next = (err) => {
12
- if (!err) {
13
- return originalMethod.apply(this, args);
14
- }
15
- throw err;
16
- };
17
- const context = {
18
- target,
19
- propertyKey,
20
- args,
21
- options,
22
- next
23
- };
24
- const interceptor = injector.get(useType);
25
- return interceptor.intercept({
26
- ...context,
27
- options
28
- }, next);
29
- }
30
- };
31
- }
5
+ const constants_1 = require("../constants/constants");
6
+ const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
32
7
  /**
33
8
  * Attaches interceptor to method call and executes the before and after methods
34
9
  *
@@ -45,10 +20,15 @@ function Intercept(interceptor, options) {
45
20
  (0, core_1.decorateMethodsOf)(target, Intercept(interceptor, options));
46
21
  break;
47
22
  case core_1.DecoratorTypes.METHOD:
48
- return bindMethod(target, propertyKey, descriptor, {
49
- options,
50
- useType: interceptor
23
+ core_1.Store.from(target).merge(constants_1.INJECTABLE_PROP, {
24
+ [propertyKey]: {
25
+ options,
26
+ propertyKey,
27
+ useType: interceptor,
28
+ bindingType: InjectablePropertyType_1.InjectablePropertyType.INTERCEPTOR
29
+ }
51
30
  });
31
+ return descriptor;
52
32
  }
53
33
  };
54
34
  }
@@ -1 +1 @@
1
- {"version":3,"file":"intercept.js","sourceRoot":"","sources":["../../../src/decorators/intercept.ts"],"names":[],"mappings":";;;AAAA,qCAAyG;AAKzG,SAAS,UAAU,CAAC,MAAW,EAAE,WAAmB,EAAE,UAA8B,EAAE,EAAC,OAAO,EAAE,OAAO,EAAM;IAC3G,MAAM,cAAc,GAAI,UAAiC,CAAC,KAAK,CAAC;IAEhE,OAAO;QACL,GAAG,UAAU;QACb,KAAK,CAAC,GAAG,IAAW;YAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,UAA6B,CAAC;YACpD,MAAM,IAAI,GAAG,CAAC,GAAW,EAAE,EAAE;gBAC3B,IAAI,CAAC,GAAG,EAAE;oBACR,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;iBACzC;gBAED,MAAM,GAAG,CAAC;YACZ,CAAC,CAAC;YAEF,MAAM,OAAO,GAA4B;gBACvC,MAAM;gBACN,WAAW;gBACX,IAAI;gBACJ,OAAO;gBACP,IAAI;aACL,CAAC;YAEF,MAAM,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAqB,OAAO,CAAE,CAAC;YAE/D,OAAO,WAAW,CAAC,SAAU,CAC3B;gBACE,GAAG,OAAO;gBACV,OAAO;aACR,EACD,IAAI,CACL,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS,CAA+B,WAAoB,EAAE,OAAa;IACzF,OAAO,CAAC,GAAG,IAAyB,EAAE,EAAE;QACtC,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC;QAC/C,MAAM,IAAI,GAAG,IAAA,sBAAe,EAAC,IAAI,CAAC,CAAC;QACnC,QAAQ,IAAI,EAAE;YACZ,KAAK,qBAAc,CAAC,KAAK;gBACvB,IAAA,wBAAiB,EAAC,MAAM,EAAE,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC;gBAC3D,MAAM;YACR,KAAK,qBAAc,CAAC,MAAM;gBACxB,OAAO,UAAU,CAAC,MAAM,EAAE,WAAqB,EAAE,UAAgC,EAAE;oBACjF,OAAO;oBACP,OAAO,EAAE,WAAW;iBACrB,CAAC,CAAC;SACN;IACH,CAAC,CAAC;AACJ,CAAC;AAfD,8BAeC","sourcesContent":["import {decorateMethodsOf, DecoratorParameters, decoratorTypeOf, DecoratorTypes, Type} from \"@tsed/core\";\nimport {InterceptorContext} from \"../interfaces/InterceptorContext\";\nimport type {InterceptorMethods} from \"../interfaces/InterceptorMethods\";\nimport type {InjectorService} from \"../services/InjectorService\";\n\nfunction bindMethod(target: any, propertyKey: string, descriptor: PropertyDescriptor, {options, useType}: any) {\n const originalMethod = (descriptor as PropertyDescriptor).value;\n\n return {\n ...descriptor,\n value(...args: any[]) {\n const injector = this.$$injector as InjectorService;\n const next = (err?: Error) => {\n if (!err) {\n return originalMethod.apply(this, args);\n }\n\n throw err;\n };\n\n const context: InterceptorContext<any> = {\n target,\n propertyKey,\n args,\n options,\n next\n };\n\n const interceptor = injector.get<InterceptorMethods>(useType)!;\n\n return interceptor.intercept!(\n {\n ...context,\n options\n },\n next\n );\n }\n };\n}\n\n/**\n * Attaches interceptor to method call and executes the before and after methods\n *\n * @param interceptor\n * @param options\n * @decorator\n */\nexport function Intercept<T extends InterceptorMethods>(interceptor: Type<T>, options?: any): any {\n return (...args: DecoratorParameters) => {\n const [target, propertyKey, descriptor] = args;\n const type = decoratorTypeOf(args);\n switch (type) {\n case DecoratorTypes.CLASS:\n decorateMethodsOf(target, Intercept(interceptor, options));\n break;\n case DecoratorTypes.METHOD:\n return bindMethod(target, propertyKey as string, descriptor as PropertyDescriptor, {\n options,\n useType: interceptor\n });\n }\n };\n}\n"]}
1
+ {"version":3,"file":"intercept.js","sourceRoot":"","sources":["../../../src/decorators/intercept.ts"],"names":[],"mappings":";;;AAAA,qCAAgH;AAChH,sDAAuD;AACvD,6EAAwE;AAIxE;;;;;;GAMG;AACH,SAAgB,SAAS,CAA+B,WAAoB,EAAE,OAAa;IACzF,OAAO,CAAC,GAAG,IAAyB,EAAE,EAAE;QACtC,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC;QAC/C,MAAM,IAAI,GAAG,IAAA,sBAAe,EAAC,IAAI,CAAC,CAAC;QACnC,QAAQ,IAAI,EAAE;YACZ,KAAK,qBAAc,CAAC,KAAK;gBACvB,IAAA,wBAAiB,EAAC,MAAM,EAAE,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC;gBAC3D,MAAM;YACR,KAAK,qBAAc,CAAC,MAAM;gBACxB,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,2BAAe,EAAE;oBACxC,CAAC,WAAW,CAAC,EAAE;wBACb,OAAO;wBACP,WAAW;wBACX,OAAO,EAAE,WAAW;wBACpB,WAAW,EAAE,+CAAsB,CAAC,WAAW;qBAChD;iBACsB,CAAC,CAAC;gBAE3B,OAAO,UAAU,CAAC;SACrB;IACH,CAAC,CAAC;AACJ,CAAC;AArBD,8BAqBC","sourcesContent":["import {decorateMethodsOf, DecoratorParameters, decoratorTypeOf, DecoratorTypes, Store, Type} from \"@tsed/core\";\nimport {INJECTABLE_PROP} from \"../constants/constants\";\nimport {InjectablePropertyType} from \"../domain/InjectablePropertyType\";\nimport type {InterceptorMethods} from \"../interfaces/InterceptorMethods\";\nimport type {InjectableProperties} from \"../interfaces/InjectableProperties\";\n\n/**\n * Attaches interceptor to method call and executes the before and after methods\n *\n * @param interceptor\n * @param options\n * @decorator\n */\nexport function Intercept<T extends InterceptorMethods>(interceptor: Type<T>, options?: any): any {\n return (...args: DecoratorParameters) => {\n const [target, propertyKey, descriptor] = args;\n const type = decoratorTypeOf(args);\n switch (type) {\n case DecoratorTypes.CLASS:\n decorateMethodsOf(target, Intercept(interceptor, options));\n break;\n case DecoratorTypes.METHOD:\n Store.from(target).merge(INJECTABLE_PROP, {\n [propertyKey]: {\n options,\n propertyKey,\n useType: interceptor,\n bindingType: InjectablePropertyType.INTERCEPTOR\n }\n } as InjectableProperties);\n\n return descriptor;\n }\n };\n}\n"]}
@@ -25,22 +25,23 @@ const inject_1 = require("./inject");
25
25
  */
26
26
  function LazyInject(key, resolver, { optional = false, packageName = resolver.toString() } = {}) {
27
27
  return (target, propertyKey) => {
28
- let token;
29
- (0, inject_1.injectProperty)(target, propertyKey, (injector) => {
30
- let bean;
31
- return async () => {
32
- if (!token) {
33
- const exports = await (0, core_1.importPackage)(packageName, resolver, optional);
34
- token = exports[key];
28
+ let bean, token;
29
+ (0, inject_1.injectProperty)(target, propertyKey, {
30
+ resolver(injector) {
31
+ return async () => {
35
32
  if (!token) {
36
- if (!optional) {
37
- throw new Error(`Unable to lazy load the "${key}". The token isn\'t a valid token provider.`);
33
+ const exports = await (0, core_1.importPackage)(packageName, resolver, optional);
34
+ token = exports[key];
35
+ if (!token) {
36
+ if (!optional) {
37
+ throw new Error(`Unable to lazy load the "${key}". The token isn\'t a valid token provider.`);
38
+ }
38
39
  }
40
+ bean = token ? await injector.lazyInvoke(token) : {};
39
41
  }
40
- bean = token ? await injector.lazyInvoke(token) : {};
41
- }
42
- return bean;
43
- };
42
+ return bean;
43
+ };
44
+ }
44
45
  });
45
46
  };
46
47
  }
@@ -1 +1 @@
1
- {"version":3,"file":"lazyInject.js","sourceRoot":"","sources":["../../../src/decorators/lazyInject.ts"],"names":[],"mappings":";;;AAAA,qCAAyC;AACzC,qCAAwC;AAExC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,UAAU,CACxB,GAAW,EACX,QAAmB,EACnB,EAAC,QAAQ,GAAG,KAAK,EAAE,WAAW,GAAG,QAAQ,CAAC,QAAQ,EAAE,KAAgD,EAAE;IAEtG,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAc,EAAE;QACtD,IAAI,KAAU,CAAC;QAEf,IAAA,uBAAc,EAAC,MAAM,EAAE,WAAW,EAAE,CAAC,QAAQ,EAAE,EAAE;YAC/C,IAAI,IAAS,CAAC;YACd,OAAO,KAAK,IAAI,EAAE;gBAChB,IAAI,CAAC,KAAK,EAAE;oBACV,MAAM,OAAO,GAAG,MAAM,IAAA,oBAAa,EAAC,WAAW,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;oBACrE,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;oBAErB,IAAI,CAAC,KAAK,EAAE;wBACV,IAAI,CAAC,QAAQ,EAAE;4BACb,MAAM,IAAI,KAAK,CAAC,4BAA4B,GAAG,6CAA6C,CAAC,CAAC;yBAC/F;qBACF;oBAED,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;iBACtD;gBAED,OAAO,IAAI,CAAC;YACd,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AA5BD,gCA4BC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,kBAAkB,CAAC,GAAW,EAAE,QAAmB;IACjE,OAAQ,UAAkB,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAC,QAAQ,EAAE,IAAI,EAAC,CAAC,CAAC;AAC9D,CAAC;AAFD,gDAEC","sourcesContent":["import {importPackage} from \"@tsed/core\";\nimport {injectProperty} from \"./inject\";\n\n/**\n * Lazy load a provider from his package and invoke only when the provider is used\n *\n * ```typescript\n * import type {PlatformException} from \"@tsed/platform-exceptions\";\n *\n * @Injectable()\n * export class MyService {\n * @LazyInject(\"PlatformException\", () => import(\"@tsed/platform-exceptions\"))\n * exceptions: Promise<PlatformException>;\n * }\n * ```\n *\n * @param key\n * @param packageName\n * @param resolver\n * @param optional\n * @returns {Function}\n * @decorator\n */\nexport function LazyInject(\n key: string,\n resolver: () => any,\n {optional = false, packageName = resolver.toString()}: {optional?: boolean; packageName?: string} = {}\n): PropertyDecorator {\n return (target: any, propertyKey: string): any | void => {\n let token: any;\n\n injectProperty(target, propertyKey, (injector) => {\n let bean: any;\n return async () => {\n if (!token) {\n const exports = await importPackage(packageName, resolver, optional);\n token = exports[key];\n\n if (!token) {\n if (!optional) {\n throw new Error(`Unable to lazy load the \"${key}\". The token isn\\'t a valid token provider.`);\n }\n }\n\n bean = token ? await injector.lazyInvoke(token) : {};\n }\n\n return bean;\n };\n });\n };\n}\n\n/**\n * Optionally Lazy load a provider from his package and invoke only when the provider is used\n *\n * ```typescript\n * import type {PlatformException} from \"@tsed/platform-exceptions\";\n *\n * @Injectable()\n * export class MyService {\n * @OptionalLazyInject(\"@tsed/platform-exceptions\")\n * exceptions: Promise<PlatformException>;\n * }\n * ```\n *\n * @param key\n * @param resolver\n * @returns {Function}\n * @decorator\n */\nexport function OptionalLazyInject(key: string, resolver: () => any): PropertyDecorator {\n return (LazyInject as any)(key, resolver, {optional: true});\n}\n"]}
1
+ {"version":3,"file":"lazyInject.js","sourceRoot":"","sources":["../../../src/decorators/lazyInject.ts"],"names":[],"mappings":";;;AAAA,qCAAyC;AACzC,qCAAwC;AAExC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,UAAU,CACxB,GAAW,EACX,QAAmB,EACnB,EAAC,QAAQ,GAAG,KAAK,EAAE,WAAW,GAAG,QAAQ,CAAC,QAAQ,EAAE,KAAgD,EAAE;IAEtG,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAc,EAAE;QACtD,IAAI,IAAS,EAAE,KAAU,CAAC;QAC1B,IAAA,uBAAc,EAAC,MAAM,EAAE,WAAW,EAAE;YAClC,QAAQ,CAAC,QAAQ;gBACf,OAAO,KAAK,IAAI,EAAE;oBAChB,IAAI,CAAC,KAAK,EAAE;wBACV,MAAM,OAAO,GAAG,MAAM,IAAA,oBAAa,EAAC,WAAW,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;wBACrE,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;wBAErB,IAAI,CAAC,KAAK,EAAE;4BACV,IAAI,CAAC,QAAQ,EAAE;gCACb,MAAM,IAAI,KAAK,CAAC,4BAA4B,GAAG,6CAA6C,CAAC,CAAC;6BAC/F;yBACF;wBAED,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;qBACtD;oBAED,OAAO,IAAI,CAAC;gBACd,CAAC,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AA5BD,gCA4BC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,kBAAkB,CAAC,GAAW,EAAE,QAAmB;IACjE,OAAQ,UAAkB,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAC,QAAQ,EAAE,IAAI,EAAC,CAAC,CAAC;AAC9D,CAAC;AAFD,gDAEC","sourcesContent":["import {importPackage} from \"@tsed/core\";\nimport {injectProperty} from \"./inject\";\n\n/**\n * Lazy load a provider from his package and invoke only when the provider is used\n *\n * ```typescript\n * import type {PlatformException} from \"@tsed/platform-exceptions\";\n *\n * @Injectable()\n * export class MyService {\n * @LazyInject(\"PlatformException\", () => import(\"@tsed/platform-exceptions\"))\n * exceptions: Promise<PlatformException>;\n * }\n * ```\n *\n * @param key\n * @param packageName\n * @param resolver\n * @param optional\n * @returns {Function}\n * @decorator\n */\nexport function LazyInject(\n key: string,\n resolver: () => any,\n {optional = false, packageName = resolver.toString()}: {optional?: boolean; packageName?: string} = {}\n): PropertyDecorator {\n return (target: any, propertyKey: string): any | void => {\n let bean: any, token: any;\n injectProperty(target, propertyKey, {\n resolver(injector) {\n return async () => {\n if (!token) {\n const exports = await importPackage(packageName, resolver, optional);\n token = exports[key];\n\n if (!token) {\n if (!optional) {\n throw new Error(`Unable to lazy load the \"${key}\". The token isn\\'t a valid token provider.`);\n }\n }\n\n bean = token ? await injector.lazyInvoke(token) : {};\n }\n\n return bean;\n };\n }\n });\n };\n}\n\n/**\n * Optionally Lazy load a provider from his package and invoke only when the provider is used\n *\n * ```typescript\n * import type {PlatformException} from \"@tsed/platform-exceptions\";\n *\n * @Injectable()\n * export class MyService {\n * @OptionalLazyInject(\"@tsed/platform-exceptions\")\n * exceptions: Promise<PlatformException>;\n * }\n * ```\n *\n * @param key\n * @param resolver\n * @returns {Function}\n * @decorator\n */\nexport function OptionalLazyInject(key: string, resolver: () => any): PropertyDecorator {\n return (LazyInject as any)(key, resolver, {optional: true});\n}\n"]}
@@ -50,7 +50,11 @@ function UseOpts(options) {
50
50
  core_1.Store.from(target).merge(`${constants_1.DI_PARAM_OPTIONS}:${index}`, options);
51
51
  break;
52
52
  case core_1.DecoratorTypes.PROP:
53
- core_1.Store.from(target, propertyKey).merge(constants_1.DI_PARAM_OPTIONS, options);
53
+ core_1.Store.from(target).merge(constants_1.INJECTABLE_PROP, {
54
+ [propertyKey]: {
55
+ options
56
+ }
57
+ });
54
58
  break;
55
59
  default:
56
60
  throw new core_1.UnsupportedDecoratorType(UseOpts, [target, propertyKey, index]);
@@ -1 +1 @@
1
- {"version":3,"file":"useOpts.js","sourceRoot":"","sources":["../../../src/decorators/useOpts.ts"],"names":[],"mappings":";;;AAAA,qCAA4F;AAC5F,sDAAwD;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,OAAO,CAAC,OAA6B;IACnD,OAAO,CAAC,MAAc,EAAE,WAA4B,EAAE,KAAc,EAAO,EAAE;QAC3E,MAAM,WAAW,GAAG,IAAA,sBAAe,EAAC,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;QAElE,QAAQ,WAAW,EAAE;YACnB,KAAK,qBAAc,CAAC,UAAU;gBAC5B,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,4BAAgB,IAAI,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;gBAClE,MAAM;YAER,KAAK,qBAAc,CAAC,IAAI;gBACtB,YAAK,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,KAAK,CAAC,4BAAgB,EAAE,OAAO,CAAC,CAAC;gBACjE,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;SAC7E;IACH,CAAC,CAAC;AACJ,CAAC;AAjBD,0BAiBC","sourcesContent":["import {decoratorTypeOf, DecoratorTypes, Store, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {DI_PARAM_OPTIONS} from \"../constants/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, propertyKey).merge(DI_PARAM_OPTIONS, options);\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseOpts, [target, propertyKey, index]);\n }\n };\n}\n"]}
1
+ {"version":3,"file":"useOpts.js","sourceRoot":"","sources":["../../../src/decorators/useOpts.ts"],"names":[],"mappings":";;;AAAA,qCAA4F;AAC5F,sDAAyE;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,OAAO,CAAC,OAA6B;IACnD,OAAO,CAAC,MAAc,EAAE,WAA4B,EAAE,KAAc,EAAO,EAAE;QAC3E,MAAM,WAAW,GAAG,IAAA,sBAAe,EAAC,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;QAElE,QAAQ,WAAW,EAAE;YACnB,KAAK,qBAAc,CAAC,UAAU;gBAC5B,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,4BAAgB,IAAI,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;gBAClE,MAAM;YAER,KAAK,qBAAc,CAAC,IAAI;gBACtB,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,2BAAe,EAAE;oBACxC,CAAC,WAAqB,CAAC,EAAE;wBACvB,OAAO;qBACR;iBACF,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;SAC7E;IACH,CAAC,CAAC;AACJ,CAAC;AArBD,0BAqBC","sourcesContent":["import {decoratorTypeOf, DecoratorTypes, Store, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {DI_PARAM_OPTIONS, INJECTABLE_PROP} from \"../constants/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"]}
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Value = void 0;
4
4
  const core_1 = require("@tsed/core");
5
+ const constants_1 = require("../constants/constants");
6
+ const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
5
7
  /**
6
8
  * Return value from Configuration.
7
9
  *
@@ -39,15 +41,13 @@ const core_1 = require("@tsed/core");
39
41
  */
40
42
  function Value(expression, defaultValue) {
41
43
  return (target, propertyKey) => {
42
- Object.defineProperty((0, core_1.prototypeOf)(target), propertyKey, {
43
- get() {
44
- return this.$$injector.settings.get(expression, defaultValue);
45
- },
46
- set(value) {
47
- this.$$injector.settings.set(expression, value);
48
- },
49
- enumerable: true,
50
- configurable: true
44
+ core_1.Store.from(target).merge(constants_1.INJECTABLE_PROP, {
45
+ [propertyKey]: {
46
+ bindingType: InjectablePropertyType_1.InjectablePropertyType.VALUE,
47
+ propertyKey,
48
+ expression,
49
+ defaultValue
50
+ }
51
51
  });
52
52
  };
53
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"value.js","sourceRoot":"","sources":["../../../src/decorators/value.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AAGvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,KAAK,CAAC,UAAe,EAAE,YAAkB;IACvD,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,MAAM,CAAC,cAAc,CAAC,IAAA,kBAAW,EAAC,MAAM,CAAC,EAAE,WAAW,EAAE;YACtD,GAAG;gBACD,OAAQ,IAAI,CAAC,UAA8B,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;YACrF,CAAC;YACD,GAAG,CAAC,KAAU;gBACX,IAAI,CAAC,UAA8B,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;YACvE,CAAC;YACD,UAAU,EAAE,IAAI;YAChB,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAbD,sBAaC","sourcesContent":["import {prototypeOf} from \"@tsed/core\";\nimport {InjectorService} from \"../services/InjectorService\";\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 Object.defineProperty(prototypeOf(target), propertyKey, {\n get() {\n return (this.$$injector as InjectorService).settings.get(expression, defaultValue);\n },\n set(value: any) {\n (this.$$injector as InjectorService).settings.set(expression, value);\n },\n enumerable: true,\n configurable: true\n });\n };\n}\n"]}
1
+ {"version":3,"file":"value.js","sourceRoot":"","sources":["../../../src/decorators/value.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AACjC,sDAAuD;AAEvD,6EAAwE;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,KAAK,CAAC,UAAe,EAAE,YAAkB;IACvD,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,2BAAe,EAAE;YACxC,CAAC,WAAW,CAAC,EAAE;gBACb,WAAW,EAAE,+CAAsB,CAAC,KAAK;gBACzC,WAAW;gBACX,UAAU;gBACV,YAAY;aACb;SACsB,CAAC,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC;AAXD,sBAWC","sourcesContent":["import {Store} from \"@tsed/core\";\nimport {INJECTABLE_PROP} from \"../constants/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"]}
@@ -24,7 +24,7 @@ class DIContext extends Map {
24
24
  });
25
25
  }
26
26
  get env() {
27
- return this.injector.settings.get("env");
27
+ return this.injector.settings.env;
28
28
  }
29
29
  async destroy() {
30
30
  await this.container.destroy();
@@ -1 +1 @@
1
- {"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":";;;AAEA,mDAAoE;AACpE,uDAAkD;AAkBlD,MAAa,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,iCAAe,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,6BAAa,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,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAEtB,aAAa;QACb,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,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;AAzED,8BAyEC","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\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 readonly logger: ContextLogger;\n /**\n *\n */\n readonly 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.get(\"env\");\n }\n\n async destroy() {\n await this.container.destroy();\n this.logger.destroy();\n\n // @ts-ignore\n delete this.container;\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"]}
1
+ {"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":";;;AAEA,mDAAoE;AACpE,uDAAkD;AAkBlD,MAAa,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,iCAAe,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,6BAAa,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;QAEtB,aAAa;QACb,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,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;AAzED,8BAyEC","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\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 readonly logger: ContextLogger;\n /**\n *\n */\n readonly 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\n // @ts-ignore\n delete this.container;\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"]}
package/lib/cjs/index.js CHANGED
@@ -32,8 +32,8 @@ tslib_1.__exportStar(require("./errors/InjectionError"), exports);
32
32
  tslib_1.__exportStar(require("./errors/UndefinedTokenError"), exports);
33
33
  tslib_1.__exportStar(require("./interfaces/DIConfigurationOptions"), exports);
34
34
  tslib_1.__exportStar(require("./interfaces/DILogger"), exports);
35
- tslib_1.__exportStar(require("./interfaces/DILoggerOptions"), exports);
36
35
  tslib_1.__exportStar(require("./interfaces/DIResolver"), exports);
36
+ tslib_1.__exportStar(require("./interfaces/InjectableProperties"), exports);
37
37
  tslib_1.__exportStar(require("./interfaces/InterceptorContext"), exports);
38
38
  tslib_1.__exportStar(require("./interfaces/InterceptorMethods"), exports);
39
39
  tslib_1.__exportStar(require("./interfaces/InvokeOptions"), exports);
@@ -55,7 +55,5 @@ tslib_1.__exportStar(require("./utils/createContainer"), exports);
55
55
  tslib_1.__exportStar(require("./utils/getConfiguration"), exports);
56
56
  tslib_1.__exportStar(require("./utils/resolveControllers"), exports);
57
57
  tslib_1.__exportStar(require("./utils/runInContext"), exports);
58
- tslib_1.__exportStar(require("./utils/setLoggerConfiguration"), exports);
59
- tslib_1.__exportStar(require("./utils/setLoggerFormat"), exports);
60
58
  tslib_1.__exportStar(require("./utils/setLoggerLevel"), exports);
61
59
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,gEAAsC;AACtC,qEAA2C;AAC3C,gEAAsC;AACtC,kEAAwC;AACxC,8DAAoC;AACpC,kEAAwC;AACxC,iEAAuC;AACvC,mEAAyC;AACzC,kEAAwC;AACxC,8DAAoC;AACpC,4DAAkC;AAClC,wEAA8C;AAC9C,6DAAmC;AACnC,+DAAqC;AACrC,+DAAqC;AACrC,6DAAmC;AACnC,6DAAmC;AACnC,iEAAuC;AACvC,6DAAmC;AACnC,0EAAgD;AAChD,mEAAyC;AACzC,4DAAkC;AAClC,iEAAuC;AACvC,gEAAsC;AACtC,kEAAwC;AACxC,uEAA6C;AAC7C,8EAAoD;AACpD,gEAAsC;AACtC,uEAA6C;AAC7C,kEAAwC;AACxC,0EAAgD;AAChD,0EAAgD;AAChD,qEAA2C;AAC3C,iEAAuC;AACvC,8DAAoC;AACpC,oEAA0C;AAC1C,wEAA8C;AAC9C,6EAAmD;AACnD,qEAA2C;AAC3C,kEAAwC;AACxC,uEAA6C;AAC7C,wEAA8C;AAC9C,qEAA2C;AAC3C,8DAAoC;AACpC,4DAAkC;AAClC,qEAA2C;AAC3C,yDAA+B;AAC/B,kEAAwC;AACxC,mEAAyC;AACzC,qEAA2C;AAC3C,+DAAqC;AACrC,yEAA+C;AAC/C,kEAAwC;AACxC,iEAAuC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./constants/constants\";\nexport * from \"./decorators/configuration\";\nexport * from \"./decorators/constant\";\nexport * from \"./decorators/controller\";\nexport * from \"./decorators/inject\";\nexport * from \"./decorators/injectable\";\nexport * from \"./decorators/intercept\";\nexport * from \"./decorators/interceptor\";\nexport * from \"./decorators/lazyInject\";\nexport * from \"./decorators/module\";\nexport * from \"./decorators/opts\";\nexport * from \"./decorators/overrideProvider\";\nexport * from \"./decorators/scope\";\nexport * from \"./decorators/service\";\nexport * from \"./decorators/useOpts\";\nexport * from \"./decorators/value\";\nexport * from \"./domain/Container\";\nexport * from \"./domain/ContextLogger\";\nexport * from \"./domain/DIContext\";\nexport * from \"./domain/InjectablePropertyType\";\nexport * from \"./domain/LocalsContainer\";\nexport * from \"./domain/Provider\";\nexport * from \"./domain/ProviderScope\";\nexport * from \"./domain/ProviderType\";\nexport * from \"./errors/InjectionError\";\nexport * from \"./errors/UndefinedTokenError\";\nexport * from \"./interfaces/DIConfigurationOptions\";\nexport * from \"./interfaces/DILogger\";\nexport * from \"./interfaces/DILoggerOptions\";\nexport * from \"./interfaces/DIResolver\";\nexport * from \"./interfaces/InterceptorContext\";\nexport * from \"./interfaces/InterceptorMethods\";\nexport * from \"./interfaces/InvokeOptions\";\nexport * from \"./interfaces/OnDestroy\";\nexport * from \"./interfaces/OnInit\";\nexport * from \"./interfaces/ProviderOpts\";\nexport * from \"./interfaces/RegistrySettings\";\nexport * from \"./interfaces/ResolvedInvokeOptions\";\nexport * from \"./interfaces/TokenProvider\";\nexport * from \"./interfaces/TokenRoute\";\nexport * from \"./registries/GlobalProviders\";\nexport * from \"./registries/ProviderRegistry\";\nexport * from \"./services/DIConfiguration\";\nexport * from \"./services/DILogger\";\nexport * from \"./services/DITest\";\nexport * from \"./services/InjectorService\";\nexport * from \"./utils/colors\";\nexport * from \"./utils/createContainer\";\nexport * from \"./utils/getConfiguration\";\nexport * from \"./utils/resolveControllers\";\nexport * from \"./utils/runInContext\";\nexport * from \"./utils/setLoggerConfiguration\";\nexport * from \"./utils/setLoggerFormat\";\nexport * from \"./utils/setLoggerLevel\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,gEAAsC;AACtC,qEAA2C;AAC3C,gEAAsC;AACtC,kEAAwC;AACxC,8DAAoC;AACpC,kEAAwC;AACxC,iEAAuC;AACvC,mEAAyC;AACzC,kEAAwC;AACxC,8DAAoC;AACpC,4DAAkC;AAClC,wEAA8C;AAC9C,6DAAmC;AACnC,+DAAqC;AACrC,+DAAqC;AACrC,6DAAmC;AACnC,6DAAmC;AACnC,iEAAuC;AACvC,6DAAmC;AACnC,0EAAgD;AAChD,mEAAyC;AACzC,4DAAkC;AAClC,iEAAuC;AACvC,gEAAsC;AACtC,kEAAwC;AACxC,uEAA6C;AAC7C,8EAAoD;AACpD,gEAAsC;AACtC,kEAAwC;AACxC,4EAAkD;AAClD,0EAAgD;AAChD,0EAAgD;AAChD,qEAA2C;AAC3C,iEAAuC;AACvC,8DAAoC;AACpC,oEAA0C;AAC1C,wEAA8C;AAC9C,6EAAmD;AACnD,qEAA2C;AAC3C,kEAAwC;AACxC,uEAA6C;AAC7C,wEAA8C;AAC9C,qEAA2C;AAC3C,8DAAoC;AACpC,4DAAkC;AAClC,qEAA2C;AAC3C,yDAA+B;AAC/B,kEAAwC;AACxC,mEAAyC;AACzC,qEAA2C;AAC3C,+DAAqC;AACrC,iEAAuC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./constants/constants\";\nexport * from \"./decorators/configuration\";\nexport * from \"./decorators/constant\";\nexport * from \"./decorators/controller\";\nexport * from \"./decorators/inject\";\nexport * from \"./decorators/injectable\";\nexport * from \"./decorators/intercept\";\nexport * from \"./decorators/interceptor\";\nexport * from \"./decorators/lazyInject\";\nexport * from \"./decorators/module\";\nexport * from \"./decorators/opts\";\nexport * from \"./decorators/overrideProvider\";\nexport * from \"./decorators/scope\";\nexport * from \"./decorators/service\";\nexport * from \"./decorators/useOpts\";\nexport * from \"./decorators/value\";\nexport * from \"./domain/Container\";\nexport * from \"./domain/ContextLogger\";\nexport * from \"./domain/DIContext\";\nexport * from \"./domain/InjectablePropertyType\";\nexport * from \"./domain/LocalsContainer\";\nexport * from \"./domain/Provider\";\nexport * from \"./domain/ProviderScope\";\nexport * from \"./domain/ProviderType\";\nexport * from \"./errors/InjectionError\";\nexport * from \"./errors/UndefinedTokenError\";\nexport * from \"./interfaces/DIConfigurationOptions\";\nexport * from \"./interfaces/DILogger\";\nexport * from \"./interfaces/DIResolver\";\nexport * from \"./interfaces/InjectableProperties\";\nexport * from \"./interfaces/InterceptorContext\";\nexport * from \"./interfaces/InterceptorMethods\";\nexport * from \"./interfaces/InvokeOptions\";\nexport * from \"./interfaces/OnDestroy\";\nexport * from \"./interfaces/OnInit\";\nexport * from \"./interfaces/ProviderOpts\";\nexport * from \"./interfaces/RegistrySettings\";\nexport * from \"./interfaces/ResolvedInvokeOptions\";\nexport * from \"./interfaces/TokenProvider\";\nexport * from \"./interfaces/TokenRoute\";\nexport * from \"./registries/GlobalProviders\";\nexport * from \"./registries/ProviderRegistry\";\nexport * from \"./services/DIConfiguration\";\nexport * from \"./services/DILogger\";\nexport * from \"./services/DITest\";\nexport * from \"./services/InjectorService\";\nexport * from \"./utils/colors\";\nexport * from \"./utils/createContainer\";\nexport * from \"./utils/getConfiguration\";\nexport * from \"./utils/resolveControllers\";\nexport * from \"./utils/runInContext\";\nexport * from \"./utils/setLoggerLevel\";\n"]}
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=DILoggerOptions.js.map
3
+ //# sourceMappingURL=InjectableProperties.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InjectableProperties.js","sourceRoot":"","sources":["../../../src/interfaces/InjectableProperties.ts"],"names":[],"mappings":"","sourcesContent":["import type {InjectablePropertyType} from \"../domain/InjectablePropertyType\";\nimport type {TokenProvider} from \"./TokenProvider\";\nimport type {InjectorService} from \"../services/InjectorService\";\nimport type {InvokeOptions} from \"./InvokeOptions\";\n\nexport interface InjectableProperty {\n propertyKey: string;\n}\n\nexport interface InjectablePropertyOptions extends InjectableProperty {\n bindingType: InjectablePropertyType.METHOD | InjectablePropertyType.PROPERTY | InjectablePropertyType.INTERCEPTOR;\n propertyType: string;\n useType?: TokenProvider;\n resolver: (injector: InjectorService, locals: Map<TokenProvider, any>, options: Partial<InvokeOptions> & {options: any}) => () => any;\n options?: any;\n}\n\nexport interface InjectablePropertyValue extends InjectableProperty {\n bindingType: InjectablePropertyType.CONSTANT | InjectablePropertyType.VALUE;\n expression: string;\n defaultValue?: any;\n}\n\nexport interface InjectableProperties {\n [key: string]: InjectablePropertyOptions | InjectablePropertyValue;\n}\n"]}
@@ -11,7 +11,6 @@ class DIConfiguration {
11
11
  resolvers: [],
12
12
  imports: [],
13
13
  routes: [],
14
- logger: {},
15
14
  ...initialProps
16
15
  }).forEach(([key, value]) => {
17
16
  this.default.set(key, value);
@@ -22,60 +21,29 @@ class DIConfiguration {
22
21
  }
23
22
  });
24
23
  }
25
- get version() {
26
- return this.get("version");
27
- }
28
- set version(v) {
29
- this.map.set("version", v);
30
- }
31
- get rootDir() {
32
- return this.get("rootDir");
33
- }
34
- set rootDir(value) {
35
- this.map.set("rootDir", value);
36
- }
37
- get env() {
38
- return this.map.get("env");
39
- }
40
- set env(value) {
41
- this.map.set("env", value);
42
- }
43
24
  get scopes() {
44
- return this.map.get("scopes");
25
+ return this.getRaw("scopes");
45
26
  }
46
27
  set scopes(value) {
47
- this.map.set("scopes", value);
28
+ this.setRaw("scopes", value);
48
29
  }
49
30
  get resolvers() {
50
31
  return this.getRaw("resolvers");
51
32
  }
52
33
  set resolvers(resolvers) {
53
- this.map.set("resolvers", resolvers);
34
+ this.setRaw("resolvers", resolvers);
54
35
  }
55
36
  get imports() {
56
- return this.get("imports");
37
+ return this.getRaw("imports");
57
38
  }
58
39
  set imports(imports) {
59
- this.map.set("imports", imports);
40
+ this.setRaw("imports", imports);
60
41
  }
61
42
  get routes() {
62
- return this.get("routes");
43
+ return this.getRaw("routes");
63
44
  }
64
45
  set routes(routes) {
65
- this.map.set("routes", routes);
66
- }
67
- get logger() {
68
- return this.get("logger");
69
- }
70
- set logger(value) {
71
- const logger = { ...this.logger, ...value };
72
- this.map.set("logger", logger);
73
- }
74
- get debug() {
75
- return this.logger.level === "debug";
76
- }
77
- set debug(debug) {
78
- this.logger = { ...this.logger, level: debug ? "debug" : "info" };
46
+ this.setRaw("routes", routes);
79
47
  }
80
48
  /**
81
49
  *
@@ -94,17 +62,11 @@ class DIConfiguration {
94
62
  */
95
63
  set(propertyKey, value) {
96
64
  if (typeof propertyKey === "string") {
97
- if (propertyKey in this) {
98
- // @ts-ignore
99
- this[propertyKey] = value;
100
- }
101
- else {
102
- this.setRaw(propertyKey, value);
103
- }
65
+ this.setRaw(propertyKey, value);
104
66
  }
105
67
  else {
106
68
  Object.entries(propertyKey).forEach(([key, value]) => {
107
- this.set(key, value);
69
+ this[key] = value;
108
70
  });
109
71
  }
110
72
  return this;
@@ -120,7 +82,7 @@ class DIConfiguration {
120
82
  * @returns {undefined|any}
121
83
  */
122
84
  get(propertyKey, defaultValue) {
123
- return this.getRaw(propertyKey, defaultValue);
85
+ return this.resolve(this.getRaw(propertyKey, defaultValue));
124
86
  }
125
87
  getRaw(propertyKey, defaultValue) {
126
88
  const value = (0, core_1.getValue)(this.map, propertyKey);
@@ -129,13 +91,45 @@ class DIConfiguration {
129
91
  }
130
92
  return (0, core_1.getValue)(this.default, propertyKey, defaultValue);
131
93
  }
94
+ merge(obj) {
95
+ Object.entries(obj).forEach(([key, value]) => {
96
+ const descriptor = Object.getOwnPropertyDescriptor(DIConfiguration.prototype, key);
97
+ const originalValue = this.get(key);
98
+ value = (0, core_1.deepMerge)(value, originalValue);
99
+ if (descriptor && !["default", "set", "map", "get"].includes(key)) {
100
+ this[key] = value;
101
+ }
102
+ });
103
+ }
132
104
  /**
133
105
  *
134
106
  * @param value
135
107
  * @returns {any}
136
108
  */
137
109
  resolve(value) {
138
- return value.replace("${rootDir}", this.rootDir);
110
+ if (typeof value === "object" && value !== null) {
111
+ if (![Array, Object].includes((0, core_1.classOf)(value))) {
112
+ return value;
113
+ }
114
+ return Object.entries(value).reduce((o, [k, v]) => {
115
+ // @ts-ignore
116
+ o[k] = this.resolve(v);
117
+ return o;
118
+ }, Array.isArray(value) ? [] : {});
119
+ }
120
+ if (typeof value === "string") {
121
+ const replacer = (match, key) => (0, core_1.getValue)(this.map, key);
122
+ return value
123
+ .replace(/\${([\w.]+)}/gi, replacer)
124
+ .replace(/<([\w.]+)>/gi, replacer)
125
+ .replace(/{{([\w.]+)}}/gi, replacer);
126
+ }
127
+ return value;
128
+ }
129
+ build() {
130
+ this.forEach((value, key) => this.map.set(key, this.resolve(value)));
131
+ this.set = this.setRaw;
132
+ this.get = this.getRaw = (propertyKey, defaultValue) => (0, core_1.getValue)(this.map, propertyKey, defaultValue);
139
133
  }
140
134
  }
141
135
  exports.DIConfiguration = DIConfiguration;