@tsed/di 6.114.5-rc.1 → 6.114.7

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 (46) 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/index.js +1 -0
  16. package/lib/cjs/index.js.map +1 -1
  17. package/lib/cjs/interfaces/InjectableProperties.js +3 -0
  18. package/lib/cjs/interfaces/InjectableProperties.js.map +1 -0
  19. package/lib/cjs/services/InjectorService.js +130 -17
  20. package/lib/cjs/services/InjectorService.js.map +1 -1
  21. package/lib/esm/constants/constants.js +2 -0
  22. package/lib/esm/constants/constants.js.map +1 -1
  23. package/lib/esm/decorators/constant.js +10 -9
  24. package/lib/esm/decorators/constant.js.map +1 -1
  25. package/lib/esm/decorators/inject.js +30 -28
  26. package/lib/esm/decorators/inject.js.map +1 -1
  27. package/lib/esm/decorators/intercept.js +11 -31
  28. package/lib/esm/decorators/intercept.js.map +1 -1
  29. package/lib/esm/decorators/lazyInject.js +14 -13
  30. package/lib/esm/decorators/lazyInject.js.map +1 -1
  31. package/lib/esm/decorators/useOpts.js +6 -2
  32. package/lib/esm/decorators/useOpts.js.map +1 -1
  33. package/lib/esm/decorators/value.js +10 -10
  34. package/lib/esm/decorators/value.js.map +1 -1
  35. package/lib/esm/index.js +1 -0
  36. package/lib/esm/index.js.map +1 -1
  37. package/lib/esm/interfaces/InjectableProperties.js +2 -0
  38. package/lib/esm/interfaces/InjectableProperties.js.map +1 -0
  39. package/lib/esm/services/InjectorService.js +132 -19
  40. package/lib/esm/services/InjectorService.js.map +1 -1
  41. package/lib/types/constants/constants.d.ts +2 -0
  42. package/lib/types/decorators/inject.d.ts +2 -5
  43. package/lib/types/index.d.ts +1 -0
  44. package/lib/types/interfaces/InjectableProperties.d.ts +24 -0
  45. package/lib/types/services/InjectorService.d.ts +44 -3
  46. package/package.json +6 -9
@@ -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"]}
package/lib/cjs/index.js CHANGED
@@ -34,6 +34,7 @@ tslib_1.__exportStar(require("./interfaces/DIConfigurationOptions"), exports);
34
34
  tslib_1.__exportStar(require("./interfaces/DILogger"), exports);
35
35
  tslib_1.__exportStar(require("./interfaces/DILoggerOptions"), exports);
36
36
  tslib_1.__exportStar(require("./interfaces/DIResolver"), exports);
37
+ tslib_1.__exportStar(require("./interfaces/InjectableProperties"), exports);
37
38
  tslib_1.__exportStar(require("./interfaces/InterceptorContext"), exports);
38
39
  tslib_1.__exportStar(require("./interfaces/InterceptorMethods"), exports);
39
40
  tslib_1.__exportStar(require("./interfaces/InvokeOptions"), exports);
@@ -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,uEAA6C;AAC7C,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,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/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/setLoggerConfiguration\";\nexport * from \"./utils/setLoggerFormat\";\nexport * from \"./utils/setLoggerLevel\";\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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,14 +11,15 @@ const injectable_1 = require("../decorators/injectable");
11
11
  const Container_1 = require("../domain/Container");
12
12
  const LocalsContainer_1 = require("../domain/LocalsContainer");
13
13
  const Provider_1 = require("../domain/Provider");
14
- const ProviderScope_1 = require("../domain/ProviderScope");
15
14
  const InjectionError_1 = require("../errors/InjectionError");
16
15
  const UndefinedTokenError_1 = require("../errors/UndefinedTokenError");
17
16
  const GlobalProviders_1 = require("../registries/GlobalProviders");
18
17
  const createContainer_1 = require("../utils/createContainer");
19
- const resolveControllers_1 = require("../utils/resolveControllers");
20
- const runInContext_1 = require("../utils/runInContext");
21
18
  const DIConfiguration_1 = require("./DIConfiguration");
19
+ const ProviderScope_1 = require("../domain/ProviderScope");
20
+ const InjectablePropertyType_1 = require("../domain/InjectablePropertyType");
21
+ const runInContext_1 = require("../utils/runInContext");
22
+ const resolveControllers_1 = require("../utils/resolveControllers");
22
23
  /**
23
24
  * This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
24
25
  *
@@ -302,22 +303,134 @@ let InjectorService = InjectorService_1 = class InjectorService extends Containe
302
303
  * @param options
303
304
  */
304
305
  bindInjectableProperties(instance, locals, options) {
305
- (0, core_1.catchError)(() => Object.defineProperty(instance, "$$injector", {
306
- configurable: true,
307
- enumerable: false,
308
- get: () => this
309
- }));
310
- (0, core_1.catchError)(() => Object.defineProperty(instance, "$$locals", {
311
- configurable: true,
312
- enumerable: false,
313
- get: () => locals
314
- }));
315
- (0, core_1.catchError)(() => Object.defineProperty(instance, "$$invokeOptions", {
316
- configurable: true,
317
- enumerable: false,
318
- get: () => options
306
+ const properties = (0, core_1.ancestorsOf)((0, core_1.classOf)(instance)).reduce((properties, target) => {
307
+ const store = core_1.Store.from(target);
308
+ return {
309
+ ...properties,
310
+ ...(store.get(constants_1.INJECTABLE_PROP) || {})
311
+ };
312
+ }, {});
313
+ Object.values(properties).forEach((definition) => {
314
+ switch (definition.bindingType) {
315
+ case InjectablePropertyType_1.InjectablePropertyType.METHOD:
316
+ this.bindMethod(instance, definition);
317
+ break;
318
+ case InjectablePropertyType_1.InjectablePropertyType.PROPERTY:
319
+ this.bindProperty(instance, definition, locals, options);
320
+ break;
321
+ case InjectablePropertyType_1.InjectablePropertyType.CONSTANT:
322
+ this.bindConstant(instance, definition);
323
+ break;
324
+ case InjectablePropertyType_1.InjectablePropertyType.VALUE:
325
+ this.bindValue(instance, definition);
326
+ break;
327
+ case InjectablePropertyType_1.InjectablePropertyType.INTERCEPTOR:
328
+ this.bindInterceptor(instance, definition);
329
+ break;
330
+ }
331
+ });
332
+ }
333
+ /**
334
+ *
335
+ * @param instance
336
+ * @param {string} propertyKey
337
+ */
338
+ bindMethod(instance, { propertyKey }) {
339
+ const target = (0, core_1.classOf)(instance);
340
+ const originalMethod = instance[propertyKey];
341
+ const deps = core_1.Metadata.getParamTypes((0, core_1.prototypeOf)(target), propertyKey);
342
+ instance[propertyKey] = () => {
343
+ const services = deps.map((dependency) => this.get(dependency));
344
+ return originalMethod.call(instance, ...services);
345
+ };
346
+ }
347
+ /**
348
+ * Create an injectable property.
349
+ *
350
+ * @param instance
351
+ * @param {string} propertyKey
352
+ * @param {any} useType
353
+ * @param resolver
354
+ * @param options
355
+ * @param locals
356
+ * @param invokeOptions
357
+ */
358
+ bindProperty(instance, { propertyKey, resolver, options = {} }, locals, invokeOptions) {
359
+ let get;
360
+ get = resolver(this, locals, { ...invokeOptions, options });
361
+ (0, core_1.catchError)(() => Object.defineProperty(instance, propertyKey, {
362
+ get
319
363
  }));
320
364
  }
365
+ /**
366
+ *
367
+ * @param instance
368
+ * @param {string} propertyKey
369
+ * @param {any} useType
370
+ */
371
+ bindValue(instance, { propertyKey, expression, defaultValue }) {
372
+ const descriptor = {
373
+ get: () => this.settings.get(expression) || defaultValue,
374
+ set: (value) => this.settings.set(expression, value),
375
+ enumerable: true,
376
+ configurable: true
377
+ };
378
+ (0, core_1.catchError)(() => Object.defineProperty(instance, propertyKey, descriptor));
379
+ }
380
+ /**
381
+ *
382
+ * @param instance
383
+ * @param {string} propertyKey
384
+ * @param {any} useType
385
+ */
386
+ bindConstant(instance, { propertyKey, expression, defaultValue }) {
387
+ let bean;
388
+ const get = () => {
389
+ if (bean !== undefined) {
390
+ return bean;
391
+ }
392
+ const value = this.settings.get(expression, defaultValue);
393
+ bean = Object.freeze((0, core_1.deepClone)(value));
394
+ return bean;
395
+ };
396
+ const descriptor = {
397
+ get,
398
+ enumerable: true,
399
+ configurable: true
400
+ };
401
+ (0, core_1.catchError)(() => Object.defineProperty(instance, propertyKey, descriptor));
402
+ }
403
+ /**
404
+ *
405
+ * @param instance
406
+ * @param propertyKey
407
+ * @param useType
408
+ * @param options
409
+ */
410
+ bindInterceptor(instance, { propertyKey, useType, options }) {
411
+ const target = (0, core_1.classOf)(instance);
412
+ const originalMethod = instance[propertyKey];
413
+ instance[propertyKey] = (...args) => {
414
+ const next = (err) => {
415
+ if (!err) {
416
+ return originalMethod.apply(instance, args);
417
+ }
418
+ throw err;
419
+ };
420
+ const context = {
421
+ target,
422
+ propertyKey,
423
+ args,
424
+ options,
425
+ next
426
+ };
427
+ const interceptor = this.get(useType);
428
+ return interceptor.intercept({
429
+ ...context,
430
+ options
431
+ }, next);
432
+ };
433
+ }
321
434
  async lazyInvoke(token) {
322
435
  let instance = this.getInstance(token);
323
436
  if (!instance) {