@tsed/di 6.123.0 → 6.123.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/jest.config.js CHANGED
@@ -5,10 +5,10 @@ module.exports = {
5
5
  ...require("@tsed/jest-config")(__dirname, "di"),
6
6
  coverageThreshold: {
7
7
  global: {
8
- statements: 98.34,
9
- branches: 92.13,
10
- functions: 96.45,
11
- lines: 98.42
8
+ statements: 98.35,
9
+ branches: 92.25,
10
+ functions: 96.49,
11
+ lines: 98.43
12
12
  }
13
13
  }
14
14
  };
@@ -7,14 +7,11 @@ const LocalsContainer_1 = require("./LocalsContainer");
7
7
  class DIContext extends Map {
8
8
  options;
9
9
  opts;
10
- /**
11
- * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
12
- */
13
- container = new LocalsContainer_1.LocalsContainer();
14
10
  /**
15
11
  * Logger attached to the context request.
16
12
  */
17
13
  logger;
14
+ #container;
18
15
  constructor(options) {
19
16
  super();
20
17
  this.options = options;
@@ -57,13 +54,19 @@ class DIContext extends Map {
57
54
  get env() {
58
55
  return this.injector.settings.get("env");
59
56
  }
57
+ /**
58
+ * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
59
+ */
60
+ get container() {
61
+ return (this.#container = this.#container || new LocalsContainer_1.LocalsContainer());
62
+ }
60
63
  async destroy() {
61
- await this.container.destroy();
64
+ await this.#container?.destroy();
62
65
  this.logger.destroy();
63
66
  this.opts = {
64
67
  id: this.opts.id
65
68
  };
66
- this.container = null;
69
+ this.#container = null;
67
70
  }
68
71
  async emit(eventName, ...args) {
69
72
  return this.injector?.emit(eventName, ...args);
@@ -72,6 +75,18 @@ class DIContext extends Map {
72
75
  next = (await this.injector?.alterAsync("$alterRunInContext", next, this)) || next;
73
76
  return (0, runInContext_1.runInContext)(this, next);
74
77
  }
78
+ cache(key, cb) {
79
+ if (!this.has(key)) {
80
+ this.set(key, cb());
81
+ }
82
+ return this.get(key);
83
+ }
84
+ async cacheAsync(key, cb) {
85
+ if (!this.has(key)) {
86
+ this.set(key, await cb());
87
+ }
88
+ return this.get(key);
89
+ }
75
90
  }
76
91
  exports.DIContext = DIContext;
77
92
  //# sourceMappingURL=DIContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":";;;AAEA,wDAAmD;AACnD,mDAAoE;AACpE,uDAAkD;AAkBlD,MAAa,SAAU,SAAQ,GAAa;IAcrB;IAXrB,IAAI,CAAmB;IAEvB;;OAEG;IACI,SAAS,GAAG,IAAI,iCAAe,EAAO,CAAC;IAC9C;;OAEG;IACM,MAAM,CAAgB;IAE/B,YAAqB,OAAyB;QAC5C,KAAK,EAAE,CAAC;QADW,YAAO,GAAP,OAAO,CAAkB;QAE5C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,6BAAa,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IACtB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;IAC/B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAS,CAAC;IAC7B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG;YACV,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE;SACV,CAAC;QACT,IAAI,CAAC,SAAS,GAAG,IAAW,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,SAAiB,EAAE,GAAG,IAAW;QAC1C,OAAO,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,IAAc;QAC/B,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,oBAAoB,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC;QAEnF,OAAO,IAAA,2BAAY,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAClC,CAAC;CACF;AA9ED,8BA8EC","sourcesContent":["import type {Env} from \"@tsed/core\";\nimport {InjectorService} from \"../services/InjectorService\";\nimport {runInContext} from \"../utils/runInContext\";\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 injector: InjectorService;\n logger: any;\n}\n\nexport class DIContext extends Map<any, any> implements ContextMethods {\n [x: string]: any;\n\n opts: DIContextOptions;\n\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 constructor(readonly options: DIContextOptions) {\n super();\n this.opts = options;\n this.logger = new ContextLogger(this);\n }\n\n /**\n * Request id generated by @@contextMiddleware@@.\n *\n * ::: tip\n * By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, \"\"))`.\n * Dash are removed to simplify tracking logs in Kibana\n * :::\n *\n * ::: tip\n * Request id can by customized by changing the server configuration.\n *\n * ```typescript\n * @Configuration({\n * logger: {\n * reqIdBuilder: createUniqId // give your own id generator function\n * }\n * })\n * class Server {\n *\n * }\n * ```\n * :::\n *\n */\n get id() {\n return this.opts.id;\n }\n\n get dateStart() {\n return this.logger.dateStart;\n }\n\n get injector(): InjectorService {\n return this.opts.injector!;\n }\n\n get env() {\n return this.injector.settings.get(\"env\");\n }\n\n async destroy() {\n await this.container.destroy();\n this.logger.destroy();\n this.opts = {\n id: this.opts.id\n } as any;\n this.container = null as any;\n }\n\n async emit(eventName: string, ...args: any[]) {\n return this.injector?.emit(eventName, ...args);\n }\n\n async runInContext(next: Function) {\n next = (await this.injector?.alterAsync(\"$alterRunInContext\", next, this)) || next;\n\n return runInContext(this, next);\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,wDAAmD;AACnD,mDAAoE;AACpE,uDAAkD;AAkBlD,MAAa,SAAU,SAAQ,GAAa;IAWrB;IARrB,IAAI,CAAmB;IACvB;;OAEG;IACM,MAAM,CAAgB;IAE/B,UAAU,CAAkB;IAE5B,YAAqB,OAAyB;QAC5C,KAAK,EAAE,CAAC;QADW,YAAO,GAAP,OAAO,CAAkB;QAE5C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,6BAAa,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IACtB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;IAC/B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAS,C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AC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;SAC3B;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;CACF;AAlGD,8BAkGC","sourcesContent":["import type {Env} from \"@tsed/core\";\nimport {InjectorService} from \"../services/InjectorService\";\nimport {runInContext} from \"../utils/runInContext\";\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 injector: InjectorService;\n logger: any;\n}\n\nexport class DIContext extends Map<any, any> implements ContextMethods {\n [x: string]: any;\n\n opts: DIContextOptions;\n /**\n * Logger attached to the context request.\n */\n readonly logger: ContextLogger;\n\n #container: LocalsContainer;\n\n constructor(readonly options: DIContextOptions) {\n super();\n this.opts = options;\n this.logger = new ContextLogger(this);\n }\n\n /**\n * Request id generated by @@contextMiddleware@@.\n *\n * ::: tip\n * By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, \"\"))`.\n * Dash are removed to simplify tracking logs in Kibana\n * :::\n *\n * ::: tip\n * Request id can by customized by changing the server configuration.\n *\n * ```typescript\n * @Configuration({\n * logger: {\n * reqIdBuilder: createUniqId // give your own id generator function\n * }\n * })\n * class Server {\n *\n * }\n * ```\n * :::\n *\n */\n get id() {\n return this.opts.id;\n }\n\n get dateStart() {\n return this.logger.dateStart;\n }\n\n get injector(): InjectorService {\n return this.opts.injector!;\n }\n\n get env() {\n return this.injector.settings.get(\"env\");\n }\n\n /**\n * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`\n */\n get container() {\n return (this.#container = this.#container || new LocalsContainer<any>());\n }\n\n async destroy() {\n await this.#container?.destroy();\n this.logger.destroy();\n this.opts = {\n id: this.opts.id\n } as any;\n this.#container = null as any;\n }\n\n async emit(eventName: string, ...args: any[]) {\n return this.injector?.emit(eventName, ...args);\n }\n\n async runInContext(next: Function) {\n next = (await this.injector?.alterAsync(\"$alterRunInContext\", next, this)) || next;\n\n return runInContext(this, next);\n }\n\n cache<Value = any>(key: string, cb: () => Value): Value {\n if (!this.has(key)) {\n this.set(key, cb());\n }\n\n return this.get(key);\n }\n\n async cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value> {\n if (!this.has(key)) {\n this.set(key, await cb());\n }\n\n return this.get(key);\n }\n}\n\nexport type BaseContext = DIContext & TsED.Context;\n"]}
@@ -4,14 +4,11 @@ import { LocalsContainer } from "./LocalsContainer.js";
4
4
  export class DIContext extends Map {
5
5
  options;
6
6
  opts;
7
- /**
8
- * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
9
- */
10
- container = new LocalsContainer();
11
7
  /**
12
8
  * Logger attached to the context request.
13
9
  */
14
10
  logger;
11
+ #container;
15
12
  constructor(options) {
16
13
  super();
17
14
  this.options = options;
@@ -54,13 +51,19 @@ export class DIContext extends Map {
54
51
  get env() {
55
52
  return this.injector.settings.get("env");
56
53
  }
54
+ /**
55
+ * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
56
+ */
57
+ get container() {
58
+ return (this.#container = this.#container || new LocalsContainer());
59
+ }
57
60
  async destroy() {
58
- await this.container.destroy();
61
+ await this.#container?.destroy();
59
62
  this.logger.destroy();
60
63
  this.opts = {
61
64
  id: this.opts.id
62
65
  };
63
- this.container = null;
66
+ this.#container = null;
64
67
  }
65
68
  async emit(eventName, ...args) {
66
69
  return this.injector?.emit(eventName, ...args);
@@ -69,5 +72,17 @@ export class DIContext extends Map {
69
72
  next = (await this.injector?.alterAsync("$alterRunInContext", next, this)) || next;
70
73
  return runInContext(this, next);
71
74
  }
75
+ cache(key, cb) {
76
+ if (!this.has(key)) {
77
+ this.set(key, cb());
78
+ }
79
+ return this.get(key);
80
+ }
81
+ async cacheAsync(key, cb) {
82
+ if (!this.has(key)) {
83
+ this.set(key, await cb());
84
+ }
85
+ return this.get(key);
86
+ }
72
87
  }
73
88
  //# sourceMappingURL=DIContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"DIContext.js","sourceRoot":"","sources":["../../../src/domain/DIContext.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,YAAY,EAAC,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAC,aAAa,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAkBlD,MAAM,OAAO,SAAU,SAAQ,GAAa;IAcrB;IAXrB,IAAI,CAAmB;IAEvB;;OAEG;IACI,SAAS,GAAG,IAAI,eAAe,EAAO,CAAC;IAC9C;;OAEG;IACM,MAAM,CAAgB;IAE/B,YAAqB,OAAyB;QAC5C,KAAK,EAAE,CAAC;QADW,YAAO,GAAP,OAAO,CAAkB;QAE5C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IACtB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;IAC/B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAS,CAAC;IAC7B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG;YACV,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE;SACV,CAAC;QACT,IAAI,CAAC,SAAS,GAAG,IAAW,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,SAAiB,EAAE,GAAG,IAAW;QAC1C,OAAO,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,IAAc;QAC/B,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,oBAAoB,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC;QAEnF,OAAO,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAClC,CAAC;CACF","sourcesContent":["import type {Env} from \"@tsed/core\";\nimport {InjectorService} from \"../services/InjectorService\";\nimport {runInContext} from \"../utils/runInContext\";\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 injector: InjectorService;\n logger: any;\n}\n\nexport class DIContext extends Map<any, any> implements ContextMethods {\n [x: string]: any;\n\n opts: DIContextOptions;\n\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 constructor(readonly options: DIContextOptions) {\n super();\n this.opts = options;\n this.logger = new ContextLogger(this);\n }\n\n /**\n * Request id generated by @@contextMiddleware@@.\n *\n * ::: tip\n * By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, \"\"))`.\n * Dash are removed to simplify tracking logs in Kibana\n * :::\n *\n * ::: tip\n * Request id can by customized by changing the server configuration.\n *\n * ```typescript\n * @Configuration({\n * logger: {\n * reqIdBuilder: createUniqId // give your own id generator function\n * }\n * })\n * class Server {\n *\n * }\n * ```\n * :::\n *\n */\n get id() {\n return this.opts.id;\n }\n\n get dateStart() {\n return this.logger.dateStart;\n }\n\n get injector(): InjectorService {\n return this.opts.injector!;\n }\n\n get env() {\n return this.injector.settings.get(\"env\");\n }\n\n async destroy() {\n await this.container.destroy();\n this.logger.destroy();\n this.opts = {\n id: this.opts.id\n } as any;\n this.container = null as any;\n }\n\n async emit(eventName: string, ...args: any[]) {\n return this.injector?.emit(eventName, ...args);\n }\n\n async runInContext(next: Function) {\n next = (await this.injector?.alterAsync(\"$alterRunInContext\", next, this)) || next;\n\n return runInContext(this, next);\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,OAAO,EAAC,YAAY,EAAC,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAC,aAAa,EAAuB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAkBlD,MAAM,OAAO,SAAU,SAAQ,GAAa;IAWrB;IARrB,IAAI,CAAmB;IACvB;;OAEG;IACM,MAAM,CAAgB;IAE/B,UAAU,CAAkB;IAE5B,YAAqB,OAAyB;QAC5C,KAAK,EAAE,CAAC;QADW,YAAO,GAAP,OAAO,CAAkB;QAE5C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,aAAa,CAAC,IAAI,CAAC,CAA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AAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC;SACrB;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,UAAU,CAAc,GAAW,EAAE,EAAwB;QACjE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;SAC3B;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;CACF","sourcesContent":["import type {Env} from \"@tsed/core\";\nimport {InjectorService} from \"../services/InjectorService\";\nimport {runInContext} from \"../utils/runInContext\";\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 injector: InjectorService;\n logger: any;\n}\n\nexport class DIContext extends Map<any, any> implements ContextMethods {\n [x: string]: any;\n\n opts: DIContextOptions;\n /**\n * Logger attached to the context request.\n */\n readonly logger: ContextLogger;\n\n #container: LocalsContainer;\n\n constructor(readonly options: DIContextOptions) {\n super();\n this.opts = options;\n this.logger = new ContextLogger(this);\n }\n\n /**\n * Request id generated by @@contextMiddleware@@.\n *\n * ::: tip\n * By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, \"\"))`.\n * Dash are removed to simplify tracking logs in Kibana\n * :::\n *\n * ::: tip\n * Request id can by customized by changing the server configuration.\n *\n * ```typescript\n * @Configuration({\n * logger: {\n * reqIdBuilder: createUniqId // give your own id generator function\n * }\n * })\n * class Server {\n *\n * }\n * ```\n * :::\n *\n */\n get id() {\n return this.opts.id;\n }\n\n get dateStart() {\n return this.logger.dateStart;\n }\n\n get injector(): InjectorService {\n return this.opts.injector!;\n }\n\n get env() {\n return this.injector.settings.get(\"env\");\n }\n\n /**\n * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`\n */\n get container() {\n return (this.#container = this.#container || new LocalsContainer<any>());\n }\n\n async destroy() {\n await this.#container?.destroy();\n this.logger.destroy();\n this.opts = {\n id: this.opts.id\n } as any;\n this.#container = null as any;\n }\n\n async emit(eventName: string, ...args: any[]) {\n return this.injector?.emit(eventName, ...args);\n }\n\n async runInContext(next: Function) {\n next = (await this.injector?.alterAsync(\"$alterRunInContext\", next, this)) || next;\n\n return runInContext(this, next);\n }\n\n cache<Value = any>(key: string, cb: () => Value): Value {\n if (!this.has(key)) {\n this.set(key, cb());\n }\n\n return this.get(key);\n }\n\n async cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value> {\n if (!this.has(key)) {\n this.set(key, await cb());\n }\n\n return this.get(key);\n }\n}\n\nexport type BaseContext = DIContext & TsED.Context;\n"]}
@@ -16,13 +16,10 @@ export interface DIContextOptions extends Omit<ContextLoggerOptions, "dateStart"
16
16
  logger: any;
17
17
  }
18
18
  export declare class DIContext extends Map<any, any> implements ContextMethods {
19
+ #private;
19
20
  readonly options: DIContextOptions;
20
21
  [x: string]: any;
21
22
  opts: DIContextOptions;
22
- /**
23
- * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
24
- */
25
- container: LocalsContainer<any>;
26
23
  /**
27
24
  * Logger attached to the context request.
28
25
  */
@@ -56,8 +53,14 @@ export declare class DIContext extends Map<any, any> implements ContextMethods {
56
53
  get dateStart(): Date;
57
54
  get injector(): InjectorService;
58
55
  get env(): any;
56
+ /**
57
+ * The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
58
+ */
59
+ get container(): LocalsContainer<any>;
59
60
  destroy(): Promise<void>;
60
61
  emit(eventName: string, ...args: any[]): Promise<void>;
61
62
  runInContext(next: Function): Promise<unknown>;
63
+ cache<Value = any>(key: string, cb: () => Value): Value;
64
+ cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value>;
62
65
  }
63
66
  export declare type BaseContext = DIContext & TsED.Context;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/di",
3
- "version": "6.123.0",
3
+ "version": "6.123.1",
4
4
  "description": "DI module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
@@ -24,14 +24,14 @@
24
24
  "tslib": "2.4.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@tsed/core": "6.123.0",
27
+ "@tsed/core": "6.123.1",
28
28
  "@tsed/logger": ">=6.2.0",
29
- "@tsed/schema": "6.123.0"
29
+ "@tsed/schema": "6.123.1"
30
30
  },
31
31
  "peerDependencies": {
32
- "@tsed/core": "^6.123.0",
32
+ "@tsed/core": "^6.123.1",
33
33
  "@tsed/logger": ">=6.2.0",
34
- "@tsed/schema": "^6.123.0"
34
+ "@tsed/schema": "^6.123.1"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@tsed/core": {