@tsed/di 8.18.1 → 8.19.0

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 (89) hide show
  1. package/lib/esm/common/decorators/autoInjectable.js +28 -9
  2. package/lib/esm/common/decorators/constant.js +26 -22
  3. package/lib/esm/common/decorators/controller.js +40 -20
  4. package/lib/esm/common/decorators/injectable.js +30 -10
  5. package/lib/esm/common/decorators/intercept.js +41 -3
  6. package/lib/esm/common/decorators/interceptor.js +31 -4
  7. package/lib/esm/common/decorators/module.js +36 -5
  8. package/lib/esm/common/decorators/opts.js +20 -12
  9. package/lib/esm/common/decorators/overrideProvider.js +38 -4
  10. package/lib/esm/common/decorators/scope.js +32 -3
  11. package/lib/esm/common/decorators/service.js +24 -4
  12. package/lib/esm/common/decorators/useOpts.js +21 -13
  13. package/lib/esm/common/decorators/value.js +28 -22
  14. package/lib/esm/common/domain/Container.js +20 -0
  15. package/lib/esm/common/domain/ContextLogger.js +21 -0
  16. package/lib/esm/common/domain/ControllerProvider.js +22 -0
  17. package/lib/esm/common/domain/DIContext.js +22 -0
  18. package/lib/esm/common/domain/InjectablePropertyType.js +16 -0
  19. package/lib/esm/common/domain/LocalsContainer.js +21 -0
  20. package/lib/esm/common/domain/Provider.js +19 -0
  21. package/lib/esm/common/domain/ProviderScope.js +25 -0
  22. package/lib/esm/common/domain/ProviderType.js +25 -0
  23. package/lib/esm/common/errors/InjectionError.js +24 -0
  24. package/lib/esm/common/errors/InvalidPropertyTokenError.js +15 -0
  25. package/lib/esm/common/fn/inject.js +19 -6
  26. package/lib/esm/common/fn/injectMany.js +24 -6
  27. package/lib/esm/common/fn/injectable.js +57 -0
  28. package/lib/esm/common/fn/injector.js +21 -2
  29. package/lib/esm/common/fn/lazyInject.js +48 -1
  30. package/lib/esm/common/fn/localsContainer.js +31 -2
  31. package/lib/esm/common/fn/logger.js +15 -1
  32. package/lib/esm/common/services/DIConfiguration.js +24 -0
  33. package/lib/esm/common/services/DILogger.js +23 -0
  34. package/lib/esm/common/services/InjectorService.js +89 -43
  35. package/lib/types/common/decorators/autoInjectable.d.ts +31 -10
  36. package/lib/types/common/decorators/configuration.d.ts +32 -2
  37. package/lib/types/common/decorators/constant.d.ts +26 -22
  38. package/lib/types/common/decorators/controller.d.ts +40 -20
  39. package/lib/types/common/decorators/inject.d.ts +29 -7
  40. package/lib/types/common/decorators/injectable.d.ts +30 -10
  41. package/lib/types/common/decorators/intercept.d.ts +41 -3
  42. package/lib/types/common/decorators/interceptor.d.ts +31 -4
  43. package/lib/types/common/decorators/lazyInject.d.ts +43 -9
  44. package/lib/types/common/decorators/module.d.ts +36 -5
  45. package/lib/types/common/decorators/opts.d.ts +20 -12
  46. package/lib/types/common/decorators/overrideProvider.d.ts +38 -4
  47. package/lib/types/common/decorators/scope.d.ts +32 -3
  48. package/lib/types/common/decorators/service.d.ts +24 -4
  49. package/lib/types/common/decorators/useOpts.d.ts +21 -13
  50. package/lib/types/common/decorators/value.d.ts +28 -22
  51. package/lib/types/common/domain/Container.d.ts +20 -0
  52. package/lib/types/common/domain/ContextLogger.d.ts +26 -0
  53. package/lib/types/common/domain/ControllerProvider.d.ts +29 -0
  54. package/lib/types/common/domain/DIContext.d.ts +27 -0
  55. package/lib/types/common/domain/InjectablePropertyType.d.ts +16 -0
  56. package/lib/types/common/domain/LocalsContainer.d.ts +21 -0
  57. package/lib/types/common/domain/Provider.d.ts +25 -0
  58. package/lib/types/common/domain/ProviderScope.d.ts +25 -0
  59. package/lib/types/common/domain/ProviderType.d.ts +25 -0
  60. package/lib/types/common/errors/InjectionError.d.ts +24 -0
  61. package/lib/types/common/errors/InvalidPropertyTokenError.d.ts +15 -0
  62. package/lib/types/common/fn/configuration.d.ts +31 -0
  63. package/lib/types/common/fn/constant.d.ts +22 -0
  64. package/lib/types/common/fn/inject.d.ts +19 -6
  65. package/lib/types/common/fn/injectMany.d.ts +24 -6
  66. package/lib/types/common/fn/injectable.d.ts +57 -0
  67. package/lib/types/common/fn/injector.d.ts +21 -2
  68. package/lib/types/common/fn/lazyInject.d.ts +48 -1
  69. package/lib/types/common/fn/localsContainer.d.ts +31 -2
  70. package/lib/types/common/fn/logger.d.ts +15 -1
  71. package/lib/types/common/fn/refValue.d.ts +15 -6
  72. package/lib/types/common/interfaces/AlterRunInContext.d.ts +26 -0
  73. package/lib/types/common/interfaces/DILogger.d.ts +36 -0
  74. package/lib/types/common/interfaces/DILoggerOptions.d.ts +23 -0
  75. package/lib/types/common/interfaces/ImportTokenProviderOpts.d.ts +43 -0
  76. package/lib/types/common/interfaces/InterceptorContext.d.ts +35 -0
  77. package/lib/types/common/interfaces/InterceptorMethods.d.ts +27 -0
  78. package/lib/types/common/interfaces/InvokeOptions.d.ts +8 -0
  79. package/lib/types/common/interfaces/OnDestroy.d.ts +22 -0
  80. package/lib/types/common/interfaces/OnInit.d.ts +22 -0
  81. package/lib/types/common/interfaces/ProviderOpts.d.ts +31 -0
  82. package/lib/types/common/interfaces/ResolvedInvokeOptions.d.ts +9 -0
  83. package/lib/types/common/interfaces/TokenProvider.d.ts +32 -2
  84. package/lib/types/common/interfaces/TokenRoute.d.ts +16 -0
  85. package/lib/types/common/services/DIConfiguration.d.ts +24 -0
  86. package/lib/types/common/services/DILogger.d.ts +28 -0
  87. package/lib/types/common/services/InjectorService.d.ts +89 -43
  88. package/lib/types/node/utils/asyncHookContext.d.ts +0 -1
  89. package/package.json +6 -6
@@ -5,24 +5,28 @@ import type { InvokeOptions } from "../interfaces/InvokeOptions.js";
5
5
  import type { TokenProvider } from "../interfaces/TokenProvider.js";
6
6
  import { DIConfiguration } from "./DIConfiguration.js";
7
7
  /**
8
- * This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
8
+ * Core dependency injection orchestrator managing provider lifecycle and resolution.
9
9
  *
10
- * ### Example:
10
+ * The main service responsible for registering, resolving, and managing all injectable providers.
11
+ * Handles dependency graph resolution, instance caching, scope management, and lifecycle hooks.
12
+ *
13
+ * ### Usage
11
14
  *
12
15
  * ```typescript
13
16
  * import {InjectorService} from "@tsed/di";
14
17
  *
15
- * // Import the services (all services are decorated with @Service()";
18
+ * // Import the services (all services are decorated with @Service())
16
19
  * import MyService1 from "./services/service1.js";
17
20
  * import MyService2 from "./services/service2.js";
18
21
  * import MyService3 from "./services/service3.js";
19
22
  *
20
- * // When all services are imported, you can load InjectorService.
21
- *
23
+ * // When all services are imported, you can load InjectorService
22
24
  * await injector().load();
23
25
  *
24
26
  * const myService1 = injector.get<MyService1>(MyService1);
25
27
  * ```
28
+ *
29
+ * @public
26
30
  */
27
31
  export declare class InjectorService extends Container {
28
32
  #private;
@@ -32,105 +36,147 @@ export declare class InjectorService extends Container {
32
36
  constructor();
33
37
  isLoaded(): boolean;
34
38
  /**
35
- * Return a list of instance build by the injector.
39
+ * Return a list of all instances built by the injector.
40
+ *
41
+ * @returns An array of all cached provider instances
36
42
  */
37
43
  toArray(): any[];
38
44
  /**
39
- * Get a service or factory already constructed from his symbol or class.
45
+ * Get a service or factory already constructed from its symbol or class.
40
46
  *
41
- * #### Example
47
+ * Returns the cached instance if available, otherwise resolves and caches the provider.
48
+ *
49
+ * ### Usage
42
50
  *
43
51
  * ```typescript
44
52
  * import {InjectorService} from "@tsed/di";
45
53
  * import MyService from "./services.js";
46
54
  *
47
55
  * class OtherService {
48
- * constructor(injectorService: InjectorService) {
49
- * const myService = injectorService.get<MyService>(MyService);
50
- * }
56
+ * constructor(injectorService: InjectorService) {
57
+ * const myService = injectorService.get<MyService>(MyService);
58
+ * }
51
59
  * }
52
60
  * ```
53
61
  *
54
- * @param token The class or symbol registered in InjectorService.
55
- * @param options
62
+ * @param token The class or symbol registered in InjectorService
63
+ * @param options Optional invocation options
64
+ * @returns The resolved provider instance
56
65
  */
57
66
  get<T = any>(token: TokenProvider<T>, options?: Partial<InvokeOptions>): T;
58
67
  /**
59
- * Return all instance of the same provider type
68
+ * Return all instances of providers matching the specified type.
69
+ *
70
+ * Retrieves all providers of a given type and resolves their instances.
71
+ *
72
+ * @param type The provider type to filter by (e.g., ProviderType.CONTROLLER)
73
+ * @param options Optional invocation options
74
+ * @returns Array of resolved instances matching the type
60
75
  */
61
76
  getMany<Type = any>(type: any, options?: Partial<InvokeOptions>): Type[];
62
77
  /**
63
- * The has() method returns a boolean indicating whether an element with the specified key exists or not.
78
+ * Check if a provider instance exists in the cache.
79
+ *
80
+ * @param token The token to check for
81
+ * @returns `true` if the instance is cached, `false` otherwise
64
82
  */
65
83
  has(token: TokenProvider): boolean;
66
84
  /**
67
- * Declare an alias for a given token.
85
+ * Create an alias for a provider token.
86
+ *
87
+ * Allows the same provider instance to be retrieved using a different token.
88
+ *
89
+ * @param token The original provider token
90
+ * @param alias The alias token to register
91
+ * @returns The injector instance for chaining
68
92
  */
69
93
  alias(token: TokenProvider, alias: TokenProvider): this;
70
94
  /**
71
- * Resolve the token depending on his provider configuration.
95
+ * Resolve a provider token and return its instance.
72
96
  *
73
- * If the token isn't cached, the injector will invoke the provider and cache the result.
97
+ * If the token isn't cached, the injector creates the instance according to the provider configuration
98
+ * and caches it based on the provider's scope (singleton, request, or instance).
74
99
  *
75
- * #### Example
100
+ * ### Usage
76
101
  *
77
102
  * ```typescript
78
103
  * import {InjectorService} from "@tsed/di";
79
104
  * import MyService from "./services.js";
80
105
  *
81
106
  * class OtherService {
82
- * constructor(injectorService: InjectorService) {
83
- * const myService = injectorService.invoke<MyService>(MyService);
84
- * }
85
- * }
107
+ * constructor(injectorService: InjectorService) {
108
+ * const myService = injectorService.resolve<MyService>(MyService);
109
+ * }
110
+ * }
86
111
  * ```
87
112
  *
88
- * @param token The injectable class to invoke. Class parameters are injected according constructor signature.
89
- * @param options {InvokeOptions} Optional options to invoke the class.
90
- * @returns {Type} The class constructed.
113
+ * @param token The injectable class to invoke. Constructor parameters are injected automatically
114
+ * @param options Optional invocation options (locals, rebuild, useOpts, etc.)
115
+ * @returns The resolved provider instance
91
116
  */
92
117
  resolve<Type = any>(token: TokenProvider<Type>, options?: Partial<InvokeOptions>): Type;
93
118
  /**
94
- * Resolve the token depending on his provider configuration.
119
+ * Invoke a provider token and return its instance.
95
120
  *
96
- * If the token isn't cached, the injector will invoke the provider and cache the result.
121
+ * Alias for `resolve()`. If the token isn't cached, the injector creates the instance
122
+ * according to the provider configuration.
97
123
  *
98
- * #### Example
124
+ * ### Usage
99
125
  *
100
126
  * ```typescript
101
127
  * import {InjectorService} from "@tsed/di";
102
128
  * import MyService from "./services.js";
103
129
  *
104
130
  * class OtherService {
105
- * constructor(injectorService: InjectorService) {
106
- * const myService = injectorService.invoke<MyService>(MyService);
107
- * }
108
- * }
131
+ * constructor(injectorService: InjectorService) {
132
+ * const myService = injectorService.invoke<MyService>(MyService);
133
+ * }
134
+ * }
109
135
  * ```
110
136
  *
111
- * @param token The injectable class to invoke. Class parameters are injected according constructor signature.
112
- * @param options {InvokeOptions} Optional options to invoke the class.
113
- * @returns {Type} The class constructed.
114
- * @alias InjectorService.resolve
137
+ * @param token The injectable class to invoke. Constructor parameters are injected automatically
138
+ * @param options Optional invocation options (locals, rebuild, useOpts, etc.)
139
+ * @returns The resolved provider instance
140
+ * @see resolve
115
141
  */
116
142
  invoke<Type = any>(token: TokenProvider<Type>, options?: Partial<InvokeOptions>): Type;
117
143
  /**
118
- * Build only providers which are asynchronous.
144
+ * Build all asynchronous providers.
145
+ *
146
+ * Resolves providers that have async factories or async initialization.
147
+ * Called automatically during the `load()` process.
119
148
  */
120
149
  loadAsync(): Promise<void>;
121
150
  /**
122
- * Build only providers which are synchronous.
151
+ * Build all synchronous singleton providers.
152
+ *
153
+ * Instantiates all singleton-scoped providers that haven't been cached yet.
154
+ * Called automatically during the `load()` process after async providers.
123
155
  */
124
156
  loadSync(): void;
125
157
  /**
126
- * Build all providers from given container (or GlobalProviders) and emit `$onInit` event.
158
+ * Load and initialize all registered providers.
127
159
  *
128
- * @param container
160
+ * Bootstraps the DI system by resolving configuration, building provider instances,
161
+ * and triggering lifecycle hooks (`$beforeInit`, `$onInit`).
162
+ *
163
+ * ### Usage
164
+ *
165
+ * ```typescript
166
+ * import {injector} from "@tsed/di";
167
+ *
168
+ * await injector().load();
169
+ * ```
170
+ *
171
+ * @param container Optional container with additional providers to merge
129
172
  */
130
173
  load(container?: Container): Promise<void>;
131
174
  /**
132
- * Load all configurations registered on providers via @Configuration decorator.
133
- * It inspects for each provider some fields like imports, mount, etc. to resolve the configuration.
175
+ * Resolve and merge all provider configurations.
176
+ *
177
+ * Collects configuration from all providers decorated with `@Configuration`,
178
+ * merges them, and stores the result in the injector settings.
179
+ * Called automatically during the load process.
134
180
  */
135
181
  resolveConfiguration(): Promise<void>;
136
182
  /**
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { AsyncLocalStorage } from "node:async_hooks";
3
2
  import type { DIContext } from "../../common/domain/DIContext.js";
4
3
  import { InjectorService } from "../../common/index.js";
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/di",
3
3
  "description": "DI module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.18.1",
5
+ "version": "8.19.0",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -29,14 +29,14 @@
29
29
  "uuid": "^10.0.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@tsed/barrels": "8.18.1",
33
- "@tsed/core": "8.18.1",
34
- "@tsed/hooks": "8.18.1",
32
+ "@tsed/barrels": "8.19.0",
33
+ "@tsed/core": "8.19.0",
34
+ "@tsed/hooks": "8.19.0",
35
35
  "@tsed/logger": "^8.0.0",
36
- "@tsed/typescript": "8.18.1",
36
+ "@tsed/typescript": "8.19.0",
37
37
  "cross-env": "7.0.3",
38
38
  "eslint": "9.12.0",
39
- "typescript": "5.4.5",
39
+ "typescript": "5.9.3",
40
40
  "vite": "7.1.11",
41
41
  "vitest": "3.2.4"
42
42
  },