@tsed/di 7.0.0-beta.1 → 7.0.0-beta.10

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 (145) hide show
  1. package/lib/cjs/constants/{index.js → constants.js} +1 -1
  2. package/lib/cjs/constants/constants.js.map +1 -0
  3. package/lib/cjs/decorators/constant.js +1 -1
  4. package/lib/cjs/decorators/constant.js.map +1 -1
  5. package/lib/cjs/decorators/controller.js.map +1 -1
  6. package/lib/cjs/decorators/inject.js +27 -2
  7. package/lib/cjs/decorators/inject.js.map +1 -1
  8. package/lib/cjs/decorators/injectable.js +0 -2
  9. package/lib/cjs/decorators/injectable.js.map +1 -1
  10. package/lib/cjs/decorators/intercept.js +20 -12
  11. package/lib/cjs/decorators/intercept.js.map +1 -1
  12. package/lib/cjs/decorators/module.js +4 -3
  13. package/lib/cjs/decorators/module.js.map +1 -1
  14. package/lib/cjs/decorators/opts.js +1 -1
  15. package/lib/cjs/decorators/opts.js.map +1 -1
  16. package/lib/cjs/decorators/useOpts.js +1 -1
  17. package/lib/cjs/decorators/useOpts.js.map +1 -1
  18. package/lib/cjs/decorators/value.js +1 -1
  19. package/lib/cjs/decorators/value.js.map +1 -1
  20. package/lib/cjs/domain/Container.js.map +1 -1
  21. package/lib/cjs/domain/DIContext.js.map +1 -1
  22. package/lib/cjs/domain/Provider.js.map +1 -1
  23. package/lib/cjs/errors/InjectionError.js.map +1 -1
  24. package/lib/cjs/index.js +53 -17
  25. package/lib/cjs/index.js.map +1 -1
  26. package/lib/cjs/interfaces/{IProvider.js → ProviderOpts.js} +1 -1
  27. package/lib/cjs/interfaces/ProviderOpts.js.map +1 -0
  28. package/lib/cjs/interfaces/RegistrySettings.js.map +1 -1
  29. package/lib/cjs/package.json +3 -0
  30. package/lib/cjs/registries/GlobalProviders.js +4 -4
  31. package/lib/cjs/registries/GlobalProviders.js.map +1 -1
  32. package/lib/cjs/registries/ProviderRegistry.js +7 -6
  33. package/lib/cjs/registries/ProviderRegistry.js.map +1 -1
  34. package/lib/cjs/services/DIConfiguration.js.map +1 -1
  35. package/lib/cjs/services/DITest.js +8 -0
  36. package/lib/cjs/services/DITest.js.map +1 -1
  37. package/lib/cjs/services/InjectorService.js +45 -54
  38. package/lib/cjs/services/InjectorService.js.map +1 -1
  39. package/lib/cjs/utils/runInContext.js +19 -0
  40. package/lib/cjs/utils/runInContext.js.map +1 -0
  41. package/lib/esm/constants/{index.js → constants.js} +1 -1
  42. package/lib/esm/constants/constants.js.map +1 -0
  43. package/lib/esm/decorators/configuration.js +1 -1
  44. package/lib/esm/decorators/constant.js +2 -2
  45. package/lib/esm/decorators/constant.js.map +1 -1
  46. package/lib/esm/decorators/controller.js +1 -1
  47. package/lib/esm/decorators/controller.js.map +1 -1
  48. package/lib/esm/decorators/inject.js +25 -1
  49. package/lib/esm/decorators/inject.js.map +1 -1
  50. package/lib/esm/decorators/injectable.js +1 -3
  51. package/lib/esm/decorators/injectable.js.map +1 -1
  52. package/lib/esm/decorators/intercept.js +21 -13
  53. package/lib/esm/decorators/intercept.js.map +1 -1
  54. package/lib/esm/decorators/interceptor.js +2 -2
  55. package/lib/esm/decorators/lazyInject.js +1 -1
  56. package/lib/esm/decorators/module.js +4 -3
  57. package/lib/esm/decorators/module.js.map +1 -1
  58. package/lib/esm/decorators/opts.js +4 -4
  59. package/lib/esm/decorators/opts.js.map +1 -1
  60. package/lib/esm/decorators/overrideProvider.js +1 -1
  61. package/lib/esm/decorators/scope.js +1 -1
  62. package/lib/esm/decorators/service.js +1 -1
  63. package/lib/esm/decorators/useOpts.js +1 -1
  64. package/lib/esm/decorators/useOpts.js.map +1 -1
  65. package/lib/esm/decorators/value.js +2 -2
  66. package/lib/esm/decorators/value.js.map +1 -1
  67. package/lib/esm/domain/Container.js +2 -2
  68. package/lib/esm/domain/Container.js.map +1 -1
  69. package/lib/esm/domain/DIContext.js +2 -2
  70. package/lib/esm/domain/DIContext.js.map +1 -1
  71. package/lib/esm/domain/Provider.js +2 -2
  72. package/lib/esm/domain/Provider.js.map +1 -1
  73. package/lib/esm/errors/InjectionError.js +1 -1
  74. package/lib/esm/errors/InjectionError.js.map +1 -1
  75. package/lib/esm/index.js +53 -17
  76. package/lib/esm/index.js.map +1 -1
  77. package/lib/esm/interfaces/ProviderOpts.js +2 -0
  78. package/lib/esm/interfaces/ProviderOpts.js.map +1 -0
  79. package/lib/esm/interfaces/RegistrySettings.js.map +1 -1
  80. package/lib/esm/package.json +3 -0
  81. package/lib/esm/registries/GlobalProviders.js +2 -2
  82. package/lib/esm/registries/GlobalProviders.js.map +1 -1
  83. package/lib/esm/registries/ProviderRegistry.js +5 -4
  84. package/lib/esm/registries/ProviderRegistry.js.map +1 -1
  85. package/lib/esm/services/DIConfiguration.js.map +1 -1
  86. package/lib/esm/services/DILogger.js +2 -2
  87. package/lib/esm/services/DITest.js +12 -4
  88. package/lib/esm/services/DITest.js.map +1 -1
  89. package/lib/esm/services/InjectorService.js +27 -36
  90. package/lib/esm/services/InjectorService.js.map +1 -1
  91. package/lib/esm/utils/createContainer.js +2 -2
  92. package/lib/esm/utils/runInContext.js +13 -0
  93. package/lib/esm/utils/runInContext.js.map +1 -0
  94. package/lib/types/constants/{index.d.ts → constants.d.ts} +0 -0
  95. package/lib/types/decorators/controller.d.ts +2 -2
  96. package/lib/types/decorators/inject.d.ts +15 -0
  97. package/lib/types/decorators/injectable.d.ts +2 -4
  98. package/lib/types/decorators/intercept.d.ts +2 -2
  99. package/lib/types/decorators/module.d.ts +3 -2
  100. package/lib/types/domain/Container.d.ts +3 -3
  101. package/lib/types/domain/DIContext.d.ts +0 -6
  102. package/lib/types/domain/Provider.d.ts +4 -6
  103. package/lib/types/errors/InjectionError.d.ts +1 -1
  104. package/lib/types/index.d.ts +47 -11
  105. package/lib/types/interfaces/{IProvider.d.ts → ProviderOpts.d.ts} +5 -4
  106. package/lib/types/interfaces/RegistrySettings.d.ts +1 -1
  107. package/lib/types/registries/GlobalProviders.d.ts +6 -4
  108. package/lib/types/registries/ProviderRegistry.d.ts +4 -4
  109. package/lib/types/services/DIConfiguration.d.ts +3 -2
  110. package/lib/types/services/DITest.d.ts +2 -0
  111. package/lib/types/services/InjectorService.d.ts +11 -17
  112. package/lib/types/utils/runInContext.d.ts +6 -0
  113. package/package.json +24 -9
  114. package/readme.md +37 -41
  115. package/tsconfig.compile.esm.json +1 -0
  116. package/lib/cjs/constants/index.js.map +0 -1
  117. package/lib/cjs/decorators/index.js +0 -19
  118. package/lib/cjs/decorators/index.js.map +0 -1
  119. package/lib/cjs/domain/index.js +0 -12
  120. package/lib/cjs/domain/index.js.map +0 -1
  121. package/lib/cjs/interfaces/IProvider.js.map +0 -1
  122. package/lib/cjs/interfaces/index.js +0 -18
  123. package/lib/cjs/interfaces/index.js.map +0 -1
  124. package/lib/cjs/utils/data/Test1.js +0 -8
  125. package/lib/cjs/utils/data/Test1.js.map +0 -1
  126. package/lib/cjs/utils/data/Test2.js +0 -8
  127. package/lib/cjs/utils/data/Test2.js.map +0 -1
  128. package/lib/esm/constants/index.js.map +0 -1
  129. package/lib/esm/decorators/index.js +0 -16
  130. package/lib/esm/decorators/index.js.map +0 -1
  131. package/lib/esm/domain/index.js +0 -9
  132. package/lib/esm/domain/index.js.map +0 -1
  133. package/lib/esm/interfaces/IProvider.js +0 -2
  134. package/lib/esm/interfaces/IProvider.js.map +0 -1
  135. package/lib/esm/interfaces/index.js +0 -15
  136. package/lib/esm/interfaces/index.js.map +0 -1
  137. package/lib/esm/utils/data/Test1.js +0 -4
  138. package/lib/esm/utils/data/Test1.js.map +0 -1
  139. package/lib/esm/utils/data/Test2.js +0 -4
  140. package/lib/esm/utils/data/Test2.js.map +0 -1
  141. package/lib/types/decorators/index.d.ts +0 -15
  142. package/lib/types/domain/index.d.ts +0 -8
  143. package/lib/types/interfaces/index.d.ts +0 -14
  144. package/lib/types/utils/data/Test1.d.ts +0 -3
  145. package/lib/types/utils/data/Test2.d.ts +0 -3
@@ -0,0 +1,13 @@
1
+ import { AsyncLocalStorage } from "async_hooks";
2
+ let store;
3
+ export function getAsyncStore() {
4
+ store = store || new AsyncLocalStorage();
5
+ return store;
6
+ }
7
+ export function getContext() {
8
+ return store === null || store === void 0 ? void 0 : store.getStore();
9
+ }
10
+ export function runInContext(ctx, cb) {
11
+ return getAsyncStore().run(ctx, cb);
12
+ }
13
+ //# sourceMappingURL=runInContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runInContext.js","sourceRoot":"","sources":["../../../src/utils/runInContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAC;AAG9C,IAAI,KAAmC,CAAC;AAExC,MAAM,UAAU,aAAa;IAC3B,KAAK,GAAG,KAAK,IAAI,IAAI,iBAAiB,EAAE,CAAC;IACzC,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,UAAU;IACxB,OAAO,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,QAAQ,EAAE,CAAC;AAC3B,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,GAAc,EAAE,EAAO;IAClD,OAAO,aAAa,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import {AsyncLocalStorage} from \"async_hooks\";\nimport {DIContext} from \"../domain/DIContext\";\n\nlet store: AsyncLocalStorage<DIContext>;\n\nexport function getAsyncStore() {\n store = store || new AsyncLocalStorage();\n return store;\n}\n\nexport function getContext() {\n return store?.getStore();\n}\n\nexport function runInContext(ctx: DIContext, cb: any) {\n return getAsyncStore().run(ctx, cb);\n}\n"]}
@@ -1,12 +1,12 @@
1
1
  import { Type } from "@tsed/core";
2
- import { IProvider } from "../interfaces/IProvider";
2
+ import { ProviderOpts } from "../interfaces/ProviderOpts";
3
3
  export declare type PathType = string | RegExp | (string | RegExp)[];
4
4
  export interface ControllerMiddlewares {
5
5
  useBefore: any[];
6
6
  use: any[];
7
7
  useAfter: any[];
8
8
  }
9
- export interface ControllerOptions extends Partial<IProvider<any>> {
9
+ export interface ControllerOptions extends Partial<ProviderOpts<any>> {
10
10
  path?: PathType;
11
11
  children?: Type<any>[];
12
12
  routerOptions?: any;
@@ -19,3 +19,18 @@ export declare function injectProperty(target: any, propertyKey: string, options
19
19
  * @decorator
20
20
  */
21
21
  export declare function Inject(symbol?: any, onGet?: (bean: any) => any): Function;
22
+ /**
23
+ * Inject a context like PlatformContext or any BaseContext.
24
+ *
25
+ * ```typescript
26
+ * @Injectable()
27
+ * export class MyService {
28
+ * @InjectContext()
29
+ * ctx?: Context;
30
+ * }
31
+ * ```
32
+ *
33
+ * @returns {Function}
34
+ * @decorator
35
+ */
36
+ export declare function InjectContext(): PropertyDecorator;
@@ -1,10 +1,8 @@
1
- import type { IProvider } from "../interfaces";
1
+ import type { ProviderOpts } from "../interfaces/ProviderOpts";
2
2
  /**
3
3
  * The decorators `@Injectable()` declare a new service can be injected in other service, controller, interceptor, etc.. on there `constructor`.
4
4
  * All classes annotated with `@Injectable()` are built one time, excepted if you change the default provider configuration.
5
5
  *
6
- * <<< @/docs/snippets/providers/getting-started-injectable.ts
7
- *
8
6
  * ::: tip
9
7
  * `@Injectable()` use the `reflect-metadata` to collect and inject the built provided to other services.
10
8
  * :::
@@ -19,4 +17,4 @@ import type { IProvider } from "../interfaces";
19
17
  * @returns {Function}
20
18
  * @decorator
21
19
  */
22
- export declare function Injectable(options?: Partial<IProvider>): ClassDecorator;
20
+ export declare function Injectable(options?: Partial<ProviderOpts>): ClassDecorator;
@@ -1,5 +1,5 @@
1
1
  import { Type } from "@tsed/core";
2
- import type { InterceptorMethods } from "../interfaces";
2
+ import type { InterceptorMethods } from "../interfaces/InterceptorMethods";
3
3
  /**
4
4
  * Attaches interceptor to method call and executes the before and after methods
5
5
  *
@@ -7,4 +7,4 @@ import type { InterceptorMethods } from "../interfaces";
7
7
  * @param options
8
8
  * @decorator
9
9
  */
10
- export declare function Intercept<T extends InterceptorMethods>(interceptor: Type<T>, options?: any): MethodDecorator;
10
+ export declare function Intercept<T extends InterceptorMethods>(interceptor: Type<T>, options?: any): any;
@@ -1,5 +1,6 @@
1
- import { ProviderScope } from "../domain";
2
- import type { DIResolver, TokenProvider } from "../interfaces";
1
+ import { ProviderScope } from "../domain/ProviderScope";
2
+ import { TokenProvider } from "../interfaces/TokenProvider";
3
+ import { DIResolver } from "../interfaces/DIResolver";
3
4
  export interface ModuleOptions extends Omit<TsED.Configuration, "scopes"> {
4
5
  /**
5
6
  * Provider scope
@@ -1,4 +1,4 @@
1
- import type { IProvider } from "../interfaces/IProvider";
1
+ import type { ProviderOpts } from "../interfaces/ProviderOpts";
2
2
  import type { ProviderType } from "./ProviderType";
3
3
  import type { TokenProvider } from "../interfaces/TokenProvider";
4
4
  import { Provider } from "./Provider";
@@ -8,13 +8,13 @@ export declare class Container extends Map<TokenProvider, Provider> {
8
8
  * @param token
9
9
  * @param settings
10
10
  */
11
- add(token: TokenProvider, settings?: Partial<IProvider>): this;
11
+ add(token: TokenProvider, settings?: Partial<ProviderOpts>): this;
12
12
  /**
13
13
  * Add a provider to the
14
14
  * @param token
15
15
  * @param settings
16
16
  */
17
- addProvider(token: TokenProvider, settings?: Partial<IProvider>): this;
17
+ addProvider(token: TokenProvider, settings?: Partial<ProviderOpts>): this;
18
18
  /**
19
19
  *
20
20
  * @param token
@@ -10,12 +10,6 @@ export interface ContextMethods extends Map<any, any> {
10
10
  readonly env: Env;
11
11
  destroy(): any;
12
12
  }
13
- declare global {
14
- namespace TsED {
15
- interface Context {
16
- }
17
- }
18
- }
19
13
  export interface DIContextOptions extends Omit<ContextLoggerOptions, "dateStart"> {
20
14
  id: string;
21
15
  logger: any;
@@ -1,18 +1,16 @@
1
1
  import { Store, Type } from "@tsed/core";
2
- import { IProvider, TokenProvider } from "../interfaces";
2
+ import { ProviderOpts } from "../interfaces/ProviderOpts";
3
+ import { TokenProvider } from "../interfaces/TokenProvider";
3
4
  import { ProviderScope } from "./ProviderScope";
4
5
  import { ProviderType } from "./ProviderType";
5
- export declare class Provider<T = any> implements IProvider<T> {
6
+ export declare class Provider<T = any> implements ProviderOpts<T> {
6
7
  type: ProviderType | any;
7
- /**
8
- * @deprecated Use injector.get(provider.token) instead.
9
- */
10
- instance: T;
11
8
  deps: TokenProvider[];
12
9
  imports: any[];
13
10
  useFactory: Function;
14
11
  useAsyncFactory: Function;
15
12
  useValue: any;
13
+ hooks?: Record<string, (instance: T, ...args: any[]) => Promise<void> | void>;
16
14
  private _useClass;
17
15
  private _provide;
18
16
  private _store;
@@ -1,4 +1,4 @@
1
- import { TokenProvider } from "../interfaces";
1
+ import { TokenProvider } from "../interfaces/TokenProvider";
2
2
  export declare class InjectionError extends Error {
3
3
  name: string;
4
4
  tokens: TokenProvider[];
@@ -1,17 +1,53 @@
1
- export * from "./constants/index";
2
- export * from "./domain";
3
- export * from "./interfaces";
4
- export * from "./decorators";
5
- export * from "./registries/ProviderRegistry";
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./constants/constants";
5
+ export * from "./decorators/configuration";
6
+ export * from "./decorators/constant";
7
+ export * from "./decorators/controller";
8
+ export * from "./decorators/inject";
9
+ export * from "./decorators/injectable";
10
+ export * from "./decorators/intercept";
11
+ export * from "./decorators/interceptor";
12
+ export * from "./decorators/lazyInject";
13
+ export * from "./decorators/module";
14
+ export * from "./decorators/opts";
15
+ export * from "./decorators/overrideProvider";
16
+ export * from "./decorators/scope";
17
+ export * from "./decorators/service";
18
+ export * from "./decorators/useOpts";
19
+ export * from "./decorators/value";
20
+ export * from "./domain/Container";
21
+ export * from "./domain/ContextLogger";
22
+ export * from "./domain/DIContext";
23
+ export * from "./domain/InjectablePropertyType";
24
+ export * from "./domain/LocalsContainer";
25
+ export * from "./domain/Provider";
26
+ export * from "./domain/ProviderScope";
27
+ export * from "./domain/ProviderType";
28
+ export * from "./errors/InjectionError";
29
+ export * from "./errors/UndefinedTokenError";
30
+ export * from "./interfaces/DIConfigurationOptions";
31
+ export * from "./interfaces/DILogger";
32
+ export * from "./interfaces/DIResolver";
33
+ export * from "./interfaces/InjectableProperties";
34
+ export * from "./interfaces/InterceptorContext";
35
+ export * from "./interfaces/InterceptorMethods";
36
+ export * from "./interfaces/InvokeOptions";
37
+ export * from "./interfaces/OnDestroy";
38
+ export * from "./interfaces/OnInit";
39
+ export * from "./interfaces/ProviderOpts";
40
+ export * from "./interfaces/RegistrySettings";
41
+ export * from "./interfaces/ResolvedInvokeOptions";
42
+ export * from "./interfaces/TokenProvider";
6
43
  export * from "./registries/GlobalProviders";
7
- export * from "./services/InjectorService";
8
- export * from "./services/DILogger";
44
+ export * from "./registries/ProviderRegistry";
9
45
  export * from "./services/DIConfiguration";
46
+ export * from "./services/DILogger";
10
47
  export * from "./services/DITest";
11
- export * from "./errors/InjectionError";
12
- export * from "./errors/UndefinedTokenError";
48
+ export * from "./services/InjectorService";
49
+ export * from "./utils/colors";
13
50
  export * from "./utils/createContainer";
14
51
  export * from "./utils/getConfiguration";
15
- export * from "./utils/getConfiguration";
52
+ export * from "./utils/runInContext";
16
53
  export * from "./utils/setLoggerLevel";
17
- export * from "./utils/colors";
@@ -3,10 +3,7 @@ import type { DIResolver } from "./DIResolver";
3
3
  import type { ProviderScope } from "../domain/ProviderScope";
4
4
  import type { ProviderType } from "../domain/ProviderType";
5
5
  import type { TokenProvider } from "./TokenProvider";
6
- /**
7
- *
8
- */
9
- export interface IProvider<T = any> {
6
+ export interface ProviderOpts<T = any> {
10
7
  /**
11
8
  * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
12
9
  */
@@ -47,6 +44,10 @@ export interface IProvider<T = any> {
47
44
  * A list of resolvers which will be used to resolve missing Symbol/Class when injector invoke a Class. This property allow external DI usage.
48
45
  */
49
46
  resolvers?: DIResolver[];
47
+ /**
48
+ * hooks to intercept custom events
49
+ */
50
+ hooks?: Record<string, (instance: T, ...args: any[]) => Promise<any> | any>;
50
51
  /**
51
52
  *
52
53
  */
@@ -1,5 +1,5 @@
1
1
  import { Type } from "@tsed/core";
2
- import type { Provider } from "../domain";
2
+ import type { Provider } from "../domain/Provider";
3
3
  import type { InjectorService } from "../services/InjectorService";
4
4
  import type { ResolvedInvokeOptions } from "./ResolvedInvokeOptions";
5
5
  /**
@@ -1,8 +1,10 @@
1
1
  import { Type } from "@tsed/core";
2
2
  import { Provider } from "../domain/Provider";
3
- import type { IProvider, RegistrySettings, TokenProvider } from "../interfaces";
4
3
  import type { InjectorService } from "../services/InjectorService";
5
4
  import { ResolvedInvokeOptions } from "../interfaces/ResolvedInvokeOptions";
5
+ import { TokenProvider } from "../interfaces/TokenProvider";
6
+ import { RegistrySettings } from "../interfaces/RegistrySettings";
7
+ import { ProviderOpts } from "../interfaces/ProviderOpts";
6
8
  export declare class GlobalProviderRegistry extends Map<TokenProvider, Provider> {
7
9
  #private;
8
10
  /**
@@ -28,7 +30,7 @@ export declare class GlobalProviderRegistry extends Map<TokenProvider, Provider>
28
30
  * @param target
29
31
  * @param options
30
32
  */
31
- merge(target: TokenProvider, options: Partial<IProvider>): void;
33
+ merge(target: TokenProvider, options: Partial<ProviderOpts>): void;
32
34
  /**
33
35
  * The delete() method removes the specified element from a Map object.
34
36
  * @param key Required. The key of the element to remove from the Map object.
@@ -40,13 +42,13 @@ export declare class GlobalProviderRegistry extends Map<TokenProvider, Provider>
40
42
  injector: InjectorService;
41
43
  }): void;
42
44
  getRegistrySettings(target: string | TokenProvider): RegistrySettings;
43
- createRegisterFn(type: string): (provider: any | IProvider, instance?: any) => void;
45
+ createRegisterFn(type: string): (provider: any | ProviderOpts, instance?: any) => void;
44
46
  /**
45
47
  *
46
48
  * @param key
47
49
  * @param options
48
50
  */
49
- protected createIfNotExists(key: TokenProvider, options: Partial<IProvider>): Provider;
51
+ protected createIfNotExists(key: TokenProvider, options: Partial<ProviderOpts>): Provider;
50
52
  }
51
53
  /**
52
54
  *
@@ -1,9 +1,9 @@
1
- import type { IProvider } from "../interfaces";
1
+ import type { ProviderOpts } from "../interfaces/ProviderOpts";
2
2
  /**
3
3
  * Register a provider configuration.
4
- * @param {IProvider<any>} provider
4
+ * @param {ProviderOpts<any>} provider
5
5
  */
6
- export declare function registerProvider(provider: Partial<IProvider>): void;
6
+ export declare function registerProvider<Type = any>(provider: Partial<ProviderOpts<Type>>): void;
7
7
  /**
8
8
  * Add a new value in the `ProviderRegistry`.
9
9
  *
@@ -25,7 +25,7 @@ export declare function registerProvider(provider: Partial<IProvider>): void;
25
25
  * }
26
26
  * ```
27
27
  */
28
- export declare const registerValue: (provider: any | IProvider<any>, value?: any) => void;
28
+ export declare const registerValue: (provider: any | ProviderOpts<any>, value?: any) => void;
29
29
  /**
30
30
  * Add a new controller in the `ProviderRegistry`. This controller will be built when `InjectorService` will be loaded.
31
31
  *
@@ -1,5 +1,6 @@
1
- import { ProviderScope } from "../domain";
2
- import type { DIResolver, TokenProvider } from "../interfaces";
1
+ import { ProviderScope } from "../domain/ProviderScope";
2
+ import type { DIResolver } from "../interfaces/DIResolver";
3
+ import type { TokenProvider } from "../interfaces/TokenProvider";
3
4
  export declare class DIConfiguration {
4
5
  readonly default: Map<string, any>;
5
6
  protected map: Map<string, any>;
@@ -1,5 +1,6 @@
1
1
  import { TokenProvider } from "../interfaces/TokenProvider";
2
2
  import { InjectorService } from "./InjectorService";
3
+ import { DIContext } from "../domain/DIContext";
3
4
  export interface DITestInvokeOptions {
4
5
  token?: TokenProvider;
5
6
  use: any;
@@ -37,5 +38,6 @@ export declare class DITest {
37
38
  * @param options
38
39
  */
39
40
  static get<T = any>(target: TokenProvider, options?: any): T;
41
+ static createDIContext(): DIContext;
40
42
  protected static configure(settings?: Partial<TsED.Configuration>): Partial<TsED.Configuration>;
41
43
  }
@@ -1,9 +1,12 @@
1
- import { DIContext, ProviderScope } from "../domain";
2
1
  import { Container } from "../domain/Container";
3
2
  import { LocalsContainer } from "../domain/LocalsContainer";
4
3
  import { Provider } from "../domain/Provider";
5
- import { DILogger, InjectablePropertyOptions, InjectablePropertyValue, InvokeOptions, IProvider, TokenProvider } from "../interfaces";
6
4
  import { DIConfiguration } from "./DIConfiguration";
5
+ import { ProviderScope } from "../domain/ProviderScope";
6
+ import { DILogger } from "../interfaces/DILogger";
7
+ import { TokenProvider } from "../interfaces/TokenProvider";
8
+ import { InvokeOptions } from "../interfaces/InvokeOptions";
9
+ import { InjectablePropertyOptions, InjectablePropertyValue } from "../interfaces/InjectableProperties";
7
10
  /**
8
11
  * This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
9
12
  *
@@ -31,7 +34,7 @@ export declare class InjectorService extends Container {
31
34
  logger: DILogger;
32
35
  private resolvedConfiguration;
33
36
  constructor();
34
- get resolvers(): import("../interfaces").DIResolver[];
37
+ get resolvers(): import("..").DIResolver[];
35
38
  get scopes(): {
36
39
  [key: string]: ProviderScope;
37
40
  } & {
@@ -42,13 +45,6 @@ export declare class InjectorService extends Container {
42
45
  * @param provider
43
46
  */
44
47
  scopeOf(provider: Provider): ProviderScope;
45
- /**
46
- * Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
47
- * @param token
48
- * @param settings
49
- * @deprecated
50
- */
51
- forkProvider(token: TokenProvider, settings?: Partial<IProvider<any>>): Provider;
52
48
  /**
53
49
  * Return a list of instance build by the injector.
54
50
  */
@@ -74,6 +70,11 @@ export declare class InjectorService extends Container {
74
70
  * @returns {boolean}
75
71
  */
76
72
  get<T = any>(token: TokenProvider, options?: any): T | undefined;
73
+ /**
74
+ * Return all instance of the same provider type
75
+ * @param type
76
+ */
77
+ getAll(type: string): any[];
77
78
  /**
78
79
  * The has() method returns a boolean indicating whether an element with the specified key exists or not.
79
80
  * @returns {boolean}
@@ -171,13 +172,6 @@ export declare class InjectorService extends Container {
171
172
  * @param options
172
173
  */
173
174
  bindInterceptor(instance: any, { propertyKey, useType, options }: InjectablePropertyOptions): void;
174
- /**
175
- * Allow handler hack for AsyncHookContext plugin.
176
- * @param ctx
177
- * @param cb
178
- * @protected
179
- */
180
- runInContext(ctx: DIContext, cb: any): any;
181
175
  lazyInvoke<T = any>(token: TokenProvider): Promise<any>;
182
176
  /**
183
177
  * Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ import { AsyncLocalStorage } from "async_hooks";
3
+ import { DIContext } from "../domain/DIContext";
4
+ export declare function getAsyncStore(): AsyncLocalStorage<DIContext>;
5
+ export declare function getContext(): DIContext | undefined;
6
+ export declare function runInContext(ctx: DIContext, cb: any): unknown;
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@tsed/di",
3
- "version": "7.0.0-beta.1",
3
+ "version": "7.0.0-beta.10",
4
4
  "description": "DI module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/cjs/index.js",
8
+ "module": "./lib/esm/index.js",
8
9
  "typings": "./lib/types/index.d.ts",
9
10
  "exports": {
10
11
  "types": "./lib/types/index.d.ts",
@@ -13,26 +14,40 @@
13
14
  "default": "./lib/esm/index.js"
14
15
  },
15
16
  "scripts": {
16
- "build": "yarn run build:esm && yarn run build:cjs",
17
+ "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
17
18
  "build:cjs": "tsc --build tsconfig.compile.json",
18
- "build:esm": "tsc --build tsconfig.compile.esm.json"
19
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
20
+ "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\""
19
21
  },
20
22
  "dependencies": {
21
- "@tsed/core": "7.0.0-beta.1",
22
- "@tsed/schema": "7.0.0-beta.1",
23
- "tslib": "2.2.0"
23
+ "tslib": "2.3.1"
24
24
  },
25
25
  "devDependencies": {
26
- "@tsed/logger": "^6.0.0"
26
+ "@tsed/core": "7.0.0-beta.10",
27
+ "@tsed/logger": ">=6.1.1",
28
+ "@tsed/schema": "7.0.0-beta.10"
27
29
  },
28
30
  "peerDependencies": {
29
- "@tsed/logger": ">=6.0.0"
31
+ "@tsed/core": "^7.0.0-beta.10",
32
+ "@tsed/logger": ">=6.1.1",
33
+ "@tsed/schema": "^7.0.0-beta.10"
34
+ },
35
+ "peerDependenciesMeta": {
36
+ "@tsed/core": {
37
+ "optional": false
38
+ },
39
+ "@tsed/logger": {
40
+ "optional": false
41
+ },
42
+ "@tsed/schema": {
43
+ "optional": false
44
+ }
30
45
  },
31
46
  "repository": "https://github.com/tsedio/tsed",
32
47
  "bugs": {
33
48
  "url": "https://github.com/tsedio/tsed/issues"
34
49
  },
35
- "homepage": "https://github.com/tsedio/tsed/tree/alpha/packages/di",
50
+ "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/di",
36
51
  "author": "Romain Lenzotti",
37
52
  "license": "MIT"
38
53
  }
package/readme.md CHANGED
@@ -32,39 +32,36 @@ A package of Ts.ED framework. See website: https://tsed.io/
32
32
  # Installation
33
33
 
34
34
  You can get the latest release and the type definitions using npm:
35
+
35
36
  ```bash
36
37
  npm install --save @tsed/di
37
38
  ```
38
39
 
39
- > **Important!** TsExpressDecorators requires Node >= 6, Express >= 4, TypeScript >= 2.0 and
40
- the `experimentalDecorators`, `emitDecoratorMetadata`, `types` and `lib` compilation
41
- options in your `tsconfig.json` file.
40
+ > **Important!** TsExpressDecorators requires Node >= 6, Express >= 4, TypeScript >= 2.0 and
41
+ > the `experimentalDecorators`, `emitDecoratorMetadata`, `types` and `lib` compilation
42
+ > options in your `tsconfig.json` file.
42
43
 
43
44
  ```json
44
45
  {
45
46
  "compilerOptions": {
46
47
  "target": "es2016",
47
48
  "lib": ["es2016"],
48
- "typeRoots": [
49
- "./node_modules/@types"
50
- ],
49
+ "typeRoots": ["./node_modules/@types"],
51
50
  "module": "commonjs",
52
51
  "moduleResolution": "node",
53
- "experimentalDecorators":true,
52
+ "experimentalDecorators": true,
54
53
  "emitDecoratorMetadata": true,
55
54
  "allowSyntheticDefaultImports": true
56
55
  },
57
- "exclude": [
58
- "node_modules"
59
- ]
56
+ "exclude": ["node_modules"]
60
57
  }
61
58
  ```
62
59
 
63
60
  ## Introduction
64
61
 
65
- Basically, almost everything may be considered as a provider – service, factory, intereceptors, and so on.
62
+ Basically, almost everything may be considered as a provider – service, factory, intereceptors, and so on.
66
63
  All of them can inject dependencies, meaning, they can create various relationships with each other.
67
- But in fact, a provider is nothing else than just a simple class annotated with an `@Injectable()` decorator.
64
+ But in fact, a provider is nothing else than just a simple class annotated with an `@Injectable()` decorator.
68
65
 
69
66
  ## Usage
70
67
 
@@ -77,27 +74,28 @@ import {Calendar} from "./models/calendar";
77
74
  @Injectable()
78
75
  export class CalendarsService {
79
76
  private readonly calendars: Calendar[] = [];
80
-
81
- create(calendar: Calendar) {
82
- this.calendars.push(calendar);
83
- }
84
-
85
- findAll(): Calendar[] {
86
- return this.calendars;
87
- }
77
+
78
+ create(calendar: Calendar) {
79
+ this.calendars.push(calendar);
80
+ }
81
+
82
+ findAll(): Calendar[] {
83
+ return this.calendars;
84
+ }
88
85
  }
89
86
  ```
90
87
 
91
88
  Here's a CalendarsService, a basic class with one property and two methods. The only new trait is that it uses the `@Injectable()` decorator.
92
89
  The `@Injectable()` attaches the metadata, thereby Ts.ED knows that this class is a provider.
93
-
90
+
94
91
  Now we have the service class already done, let's use it inside a controller:
92
+
95
93
  ```typescript
96
- import { Controller, Post, Body, Get } from '@tsed/common';
97
- import { CalendarsService } from './CalendarsService';
98
- import { Calendar } from './models/Calendar';
94
+ import {Controller, Post, Body, Get} from "@tsed/common";
95
+ import {CalendarsService} from "./CalendarsService";
96
+ import {Calendar} from "./models/Calendar";
99
97
 
100
- @Controller('/calendars')
98
+ @Controller("/calendars")
101
99
  export class CalendarCtrl {
102
100
  constructor(private readonly calendarsService: CalendarsService) {}
103
101
 
@@ -112,30 +110,31 @@ export class CalendarCtrl {
112
110
  }
113
111
  }
114
112
  ```
115
- > Note: Controller isn't a part of `@tsed/di`. `@Controller` decorator is exposed by `@tsed/common` package because it's a specific provider
116
- used by the Ts.ED framework. Ts.ED DI allow you to define your own Provider and decorator.
117
113
 
114
+ > Note: Controller isn't a part of `@tsed/di`. `@Controller` decorator is exposed by `@tsed/common` package because it's a specific provider
115
+ > used by the Ts.ED framework. Ts.ED DI allow you to define your own Provider and decorator.
118
116
 
119
117
  Finally, we can load the injector and use:
118
+
120
119
  ```typescript
121
120
  import {InjectorService} from "@tsed/di";
122
121
  import {CalendarCtrl} from "./CalendarCtrl";
123
122
 
124
123
  async function bootstrap() {
125
- const injector = new InjectorService()
126
-
124
+ const injector = new InjectorService();
125
+
127
126
  // Load all providers registered via @Injectable decorator
128
- await injector.load()
129
-
130
- const calendarController = injector.get<CalendarCtrl>()
131
-
132
- await calendarController.create(new Calendar())
133
-
127
+ await injector.load();
128
+
129
+ const calendarController = injector.get<CalendarCtrl>();
130
+
131
+ await calendarController.create(new Calendar());
132
+
134
133
  // And finally destroy injector and his instances (see injector hooks)
135
- await injector.destroy()
134
+ await injector.destroy();
136
135
  }
137
136
 
138
- bootstrap()
137
+ bootstrap();
139
138
  ```
140
139
 
141
140
  ## Custom providers
@@ -147,23 +146,20 @@ To organize your code Ts.ED DI provide different kind of providers:
147
146
  - Interceptor can be declared with `@Interceptor`,
148
147
  - Factory and Value can be declared with `registerProvider` and `registerValue`.
149
148
 
150
-
151
149
  See more details on our documentation https://tsed.io/providers.html
152
150
 
153
-
154
151
  ## Contributors
152
+
155
153
  Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
156
154
 
157
155
  <a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
158
156
 
159
-
160
157
  ## Backers
161
158
 
162
159
  Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
163
160
 
164
161
  <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
165
162
 
166
-
167
163
  ## Sponsors
168
164
 
169
165
  Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
@@ -8,6 +8,7 @@
8
8
  "declaration": true,
9
9
  "declarationDir": "./lib/types"
10
10
  },
11
+ "include": ["src"],
11
12
  "exclude": [
12
13
  "node_modules",
13
14
  "test",