@tsed/di 8.18.2 → 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.
- package/lib/esm/common/decorators/autoInjectable.js +28 -9
- package/lib/esm/common/decorators/constant.js +26 -22
- package/lib/esm/common/decorators/controller.js +40 -20
- package/lib/esm/common/decorators/injectable.js +30 -10
- package/lib/esm/common/decorators/intercept.js +41 -3
- package/lib/esm/common/decorators/interceptor.js +31 -4
- package/lib/esm/common/decorators/module.js +36 -5
- package/lib/esm/common/decorators/opts.js +20 -12
- package/lib/esm/common/decorators/overrideProvider.js +38 -4
- package/lib/esm/common/decorators/scope.js +32 -3
- package/lib/esm/common/decorators/service.js +24 -4
- package/lib/esm/common/decorators/useOpts.js +21 -13
- package/lib/esm/common/decorators/value.js +28 -22
- package/lib/esm/common/domain/Container.js +20 -0
- package/lib/esm/common/domain/ContextLogger.js +21 -0
- package/lib/esm/common/domain/ControllerProvider.js +22 -0
- package/lib/esm/common/domain/DIContext.js +22 -0
- package/lib/esm/common/domain/InjectablePropertyType.js +16 -0
- package/lib/esm/common/domain/LocalsContainer.js +21 -0
- package/lib/esm/common/domain/Provider.js +19 -0
- package/lib/esm/common/domain/ProviderScope.js +25 -0
- package/lib/esm/common/domain/ProviderType.js +25 -0
- package/lib/esm/common/errors/InjectionError.js +24 -0
- package/lib/esm/common/errors/InvalidPropertyTokenError.js +15 -0
- package/lib/esm/common/fn/inject.js +19 -6
- package/lib/esm/common/fn/injectMany.js +24 -6
- package/lib/esm/common/fn/injectable.js +57 -0
- package/lib/esm/common/fn/injector.js +21 -2
- package/lib/esm/common/fn/lazyInject.js +48 -1
- package/lib/esm/common/fn/localsContainer.js +31 -2
- package/lib/esm/common/fn/logger.js +15 -1
- package/lib/esm/common/services/DIConfiguration.js +24 -0
- package/lib/esm/common/services/DILogger.js +23 -0
- package/lib/esm/common/services/InjectorService.js +89 -43
- package/lib/types/common/decorators/autoInjectable.d.ts +31 -10
- package/lib/types/common/decorators/configuration.d.ts +32 -2
- package/lib/types/common/decorators/constant.d.ts +26 -22
- package/lib/types/common/decorators/controller.d.ts +40 -20
- package/lib/types/common/decorators/inject.d.ts +29 -7
- package/lib/types/common/decorators/injectable.d.ts +30 -10
- package/lib/types/common/decorators/intercept.d.ts +41 -3
- package/lib/types/common/decorators/interceptor.d.ts +31 -4
- package/lib/types/common/decorators/lazyInject.d.ts +43 -9
- package/lib/types/common/decorators/module.d.ts +36 -5
- package/lib/types/common/decorators/opts.d.ts +20 -12
- package/lib/types/common/decorators/overrideProvider.d.ts +38 -4
- package/lib/types/common/decorators/scope.d.ts +32 -3
- package/lib/types/common/decorators/service.d.ts +24 -4
- package/lib/types/common/decorators/useOpts.d.ts +21 -13
- package/lib/types/common/decorators/value.d.ts +28 -22
- package/lib/types/common/domain/Container.d.ts +20 -0
- package/lib/types/common/domain/ContextLogger.d.ts +26 -0
- package/lib/types/common/domain/ControllerProvider.d.ts +29 -0
- package/lib/types/common/domain/DIContext.d.ts +27 -0
- package/lib/types/common/domain/InjectablePropertyType.d.ts +16 -0
- package/lib/types/common/domain/LocalsContainer.d.ts +21 -0
- package/lib/types/common/domain/Provider.d.ts +25 -0
- package/lib/types/common/domain/ProviderScope.d.ts +25 -0
- package/lib/types/common/domain/ProviderType.d.ts +25 -0
- package/lib/types/common/errors/InjectionError.d.ts +24 -0
- package/lib/types/common/errors/InvalidPropertyTokenError.d.ts +15 -0
- package/lib/types/common/fn/configuration.d.ts +31 -0
- package/lib/types/common/fn/constant.d.ts +22 -0
- package/lib/types/common/fn/inject.d.ts +19 -6
- package/lib/types/common/fn/injectMany.d.ts +24 -6
- package/lib/types/common/fn/injectable.d.ts +57 -0
- package/lib/types/common/fn/injector.d.ts +21 -2
- package/lib/types/common/fn/lazyInject.d.ts +48 -1
- package/lib/types/common/fn/localsContainer.d.ts +31 -2
- package/lib/types/common/fn/logger.d.ts +15 -1
- package/lib/types/common/fn/refValue.d.ts +15 -6
- package/lib/types/common/interfaces/AlterRunInContext.d.ts +26 -0
- package/lib/types/common/interfaces/DILogger.d.ts +36 -0
- package/lib/types/common/interfaces/DILoggerOptions.d.ts +23 -0
- package/lib/types/common/interfaces/ImportTokenProviderOpts.d.ts +43 -0
- package/lib/types/common/interfaces/InterceptorContext.d.ts +35 -0
- package/lib/types/common/interfaces/InterceptorMethods.d.ts +27 -0
- package/lib/types/common/interfaces/InvokeOptions.d.ts +8 -0
- package/lib/types/common/interfaces/OnDestroy.d.ts +22 -0
- package/lib/types/common/interfaces/OnInit.d.ts +22 -0
- package/lib/types/common/interfaces/ProviderOpts.d.ts +31 -0
- package/lib/types/common/interfaces/ResolvedInvokeOptions.d.ts +9 -0
- package/lib/types/common/interfaces/TokenProvider.d.ts +32 -2
- package/lib/types/common/interfaces/TokenRoute.d.ts +16 -0
- package/lib/types/common/services/DIConfiguration.d.ts +24 -0
- package/lib/types/common/services/DILogger.d.ts +28 -0
- package/lib/types/common/services/InjectorService.d.ts +89 -43
- package/lib/types/node/utils/asyncHookContext.d.ts +0 -1
- package/package.json +6 -6
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { injector } from "./injector.js";
|
|
2
2
|
/**
|
|
3
|
-
* Get the current
|
|
3
|
+
* Get the current logger instance from the injector.
|
|
4
|
+
*
|
|
5
|
+
* Returns the configured logger (defaults to console if not set).
|
|
6
|
+
*
|
|
7
|
+
* ### Usage
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import {logger} from "@tsed/di";
|
|
11
|
+
*
|
|
12
|
+
* logger().info("Application started");
|
|
13
|
+
* logger().error("An error occurred", error);
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @returns The logger instance
|
|
17
|
+
* @public
|
|
4
18
|
*/
|
|
5
19
|
export function logger() {
|
|
6
20
|
return injector().logger;
|
|
@@ -2,6 +2,30 @@ import { getValue } from "@tsed/core/utils/getValue.js";
|
|
|
2
2
|
import { isFunction } from "@tsed/core/utils/isFunction.js";
|
|
3
3
|
import { setValue } from "@tsed/core/utils/setValue.js";
|
|
4
4
|
import { $alter } from "@tsed/hooks";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration management service for the DI system.
|
|
7
|
+
*
|
|
8
|
+
* Stores and manages application configuration settings including imports, routes, logger options,
|
|
9
|
+
* and custom properties. Provides type-safe accessors for common configuration keys and supports
|
|
10
|
+
* nested property access via `get()` and `set()`.
|
|
11
|
+
*
|
|
12
|
+
* ### Usage
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import {DIConfiguration} from "@tsed/di";
|
|
16
|
+
*
|
|
17
|
+
* const config = new DIConfiguration({
|
|
18
|
+
* rootDir: __dirname,
|
|
19
|
+
* env: Env.PROD,
|
|
20
|
+
* logger: {level: "info"}
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* config.set("custom.nested.key", "value");
|
|
24
|
+
* const value = config.get("custom.nested.key");
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
5
29
|
export class DIConfiguration {
|
|
6
30
|
constructor(initialProps = {}) {
|
|
7
31
|
this.default = new Map();
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
import { injectable } from "../fn/injectable.js";
|
|
2
2
|
import { injector } from "../fn/injector.js";
|
|
3
|
+
/**
|
|
4
|
+
* Injection token for the DI logger service.
|
|
5
|
+
*
|
|
6
|
+
* Use this symbol to inject the configured logger instance into your services.
|
|
7
|
+
*
|
|
8
|
+
* ### Usage
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import {Injectable, Inject, LOGGER} from "@tsed/di";
|
|
12
|
+
*
|
|
13
|
+
* @Injectable()
|
|
14
|
+
* class MyService {
|
|
15
|
+
* @Inject(LOGGER)
|
|
16
|
+
* logger: LOGGER;
|
|
17
|
+
*
|
|
18
|
+
* doSomething() {
|
|
19
|
+
* this.logger.info("Doing something");
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
3
26
|
export const LOGGER = Symbol.for("LOGGER");
|
|
4
27
|
injectable(LOGGER).factory(() => injector().logger);
|
|
@@ -21,24 +21,28 @@ import { getConstructorDependencies } from "../utils/getConstructorDependencies.
|
|
|
21
21
|
import { DIConfiguration } from "./DIConfiguration.js";
|
|
22
22
|
const EXCLUDED_CONFIGURATION_KEYS = ["mount", "imports"];
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
24
|
+
* Core dependency injection orchestrator managing provider lifecycle and resolution.
|
|
25
25
|
*
|
|
26
|
-
*
|
|
26
|
+
* The main service responsible for registering, resolving, and managing all injectable providers.
|
|
27
|
+
* Handles dependency graph resolution, instance caching, scope management, and lifecycle hooks.
|
|
28
|
+
*
|
|
29
|
+
* ### Usage
|
|
27
30
|
*
|
|
28
31
|
* ```typescript
|
|
29
32
|
* import {InjectorService} from "@tsed/di";
|
|
30
33
|
*
|
|
31
|
-
* // Import the services (all services are decorated with @Service()
|
|
34
|
+
* // Import the services (all services are decorated with @Service())
|
|
32
35
|
* import MyService1 from "./services/service1.js";
|
|
33
36
|
* import MyService2 from "./services/service2.js";
|
|
34
37
|
* import MyService3 from "./services/service3.js";
|
|
35
38
|
*
|
|
36
|
-
* // When all services are imported, you can load InjectorService
|
|
37
|
-
*
|
|
39
|
+
* // When all services are imported, you can load InjectorService
|
|
38
40
|
* await injector().load();
|
|
39
41
|
*
|
|
40
42
|
* const myService1 = injector.get<MyService1>(MyService1);
|
|
41
43
|
* ```
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
42
46
|
*/
|
|
43
47
|
export class InjectorService extends Container {
|
|
44
48
|
#cache;
|
|
@@ -58,29 +62,34 @@ export class InjectorService extends Container {
|
|
|
58
62
|
return this.#loaded;
|
|
59
63
|
}
|
|
60
64
|
/**
|
|
61
|
-
* Return a list of
|
|
65
|
+
* Return a list of all instances built by the injector.
|
|
66
|
+
*
|
|
67
|
+
* @returns An array of all cached provider instances
|
|
62
68
|
*/
|
|
63
69
|
toArray() {
|
|
64
70
|
return [...this.#cache.values()];
|
|
65
71
|
}
|
|
66
72
|
/**
|
|
67
|
-
* Get a service or factory already constructed from
|
|
73
|
+
* Get a service or factory already constructed from its symbol or class.
|
|
68
74
|
*
|
|
69
|
-
*
|
|
75
|
+
* Returns the cached instance if available, otherwise resolves and caches the provider.
|
|
76
|
+
*
|
|
77
|
+
* ### Usage
|
|
70
78
|
*
|
|
71
79
|
* ```typescript
|
|
72
80
|
* import {InjectorService} from "@tsed/di";
|
|
73
81
|
* import MyService from "./services.js";
|
|
74
82
|
*
|
|
75
83
|
* class OtherService {
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
84
|
+
* constructor(injectorService: InjectorService) {
|
|
85
|
+
* const myService = injectorService.get<MyService>(MyService);
|
|
86
|
+
* }
|
|
79
87
|
* }
|
|
80
88
|
* ```
|
|
81
89
|
*
|
|
82
|
-
* @param token The class or symbol registered in InjectorService
|
|
83
|
-
* @param options
|
|
90
|
+
* @param token The class or symbol registered in InjectorService
|
|
91
|
+
* @param options Optional invocation options
|
|
92
|
+
* @returns The resolved provider instance
|
|
84
93
|
*/
|
|
85
94
|
get(token, options) {
|
|
86
95
|
if (!this.has(token)) {
|
|
@@ -89,7 +98,13 @@ export class InjectorService extends Container {
|
|
|
89
98
|
return this.#cache.get(token);
|
|
90
99
|
}
|
|
91
100
|
/**
|
|
92
|
-
* Return all
|
|
101
|
+
* Return all instances of providers matching the specified type.
|
|
102
|
+
*
|
|
103
|
+
* Retrieves all providers of a given type and resolves their instances.
|
|
104
|
+
*
|
|
105
|
+
* @param type The provider type to filter by (e.g., ProviderType.CONTROLLER)
|
|
106
|
+
* @param options Optional invocation options
|
|
107
|
+
* @returns Array of resolved instances matching the type
|
|
93
108
|
*/
|
|
94
109
|
getMany(type, options) {
|
|
95
110
|
return this.getProviders(type).map((provider) => {
|
|
@@ -97,39 +112,49 @@ export class InjectorService extends Container {
|
|
|
97
112
|
});
|
|
98
113
|
}
|
|
99
114
|
/**
|
|
100
|
-
*
|
|
115
|
+
* Check if a provider instance exists in the cache.
|
|
116
|
+
*
|
|
117
|
+
* @param token The token to check for
|
|
118
|
+
* @returns `true` if the instance is cached, `false` otherwise
|
|
101
119
|
*/
|
|
102
120
|
has(token) {
|
|
103
121
|
return this.#cache.get(token) !== undefined;
|
|
104
122
|
}
|
|
105
123
|
/**
|
|
106
|
-
*
|
|
124
|
+
* Create an alias for a provider token.
|
|
125
|
+
*
|
|
126
|
+
* Allows the same provider instance to be retrieved using a different token.
|
|
127
|
+
*
|
|
128
|
+
* @param token The original provider token
|
|
129
|
+
* @param alias The alias token to register
|
|
130
|
+
* @returns The injector instance for chaining
|
|
107
131
|
*/
|
|
108
132
|
alias(token, alias) {
|
|
109
133
|
this.#cache.set(alias, this.#cache.get(token));
|
|
110
134
|
return this;
|
|
111
135
|
}
|
|
112
136
|
/**
|
|
113
|
-
* Resolve
|
|
137
|
+
* Resolve a provider token and return its instance.
|
|
114
138
|
*
|
|
115
|
-
* If the token isn't cached, the injector
|
|
139
|
+
* If the token isn't cached, the injector creates the instance according to the provider configuration
|
|
140
|
+
* and caches it based on the provider's scope (singleton, request, or instance).
|
|
116
141
|
*
|
|
117
|
-
*
|
|
142
|
+
* ### Usage
|
|
118
143
|
*
|
|
119
144
|
* ```typescript
|
|
120
145
|
* import {InjectorService} from "@tsed/di";
|
|
121
146
|
* import MyService from "./services.js";
|
|
122
147
|
*
|
|
123
148
|
* class OtherService {
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
149
|
+
* constructor(injectorService: InjectorService) {
|
|
150
|
+
* const myService = injectorService.resolve<MyService>(MyService);
|
|
151
|
+
* }
|
|
152
|
+
* }
|
|
128
153
|
* ```
|
|
129
154
|
*
|
|
130
|
-
* @param token The injectable class to invoke.
|
|
131
|
-
* @param options
|
|
132
|
-
* @returns
|
|
155
|
+
* @param token The injectable class to invoke. Constructor parameters are injected automatically
|
|
156
|
+
* @param options Optional invocation options (locals, rebuild, useOpts, etc.)
|
|
157
|
+
* @returns The resolved provider instance
|
|
133
158
|
*/
|
|
134
159
|
resolve(token, options = {}) {
|
|
135
160
|
let instance = options.locals ? options.locals.get(token) : undefined;
|
|
@@ -173,33 +198,37 @@ export class InjectorService extends Container {
|
|
|
173
198
|
return instance;
|
|
174
199
|
}
|
|
175
200
|
/**
|
|
176
|
-
*
|
|
201
|
+
* Invoke a provider token and return its instance.
|
|
177
202
|
*
|
|
178
|
-
* If the token isn't cached, the injector
|
|
203
|
+
* Alias for `resolve()`. If the token isn't cached, the injector creates the instance
|
|
204
|
+
* according to the provider configuration.
|
|
179
205
|
*
|
|
180
|
-
*
|
|
206
|
+
* ### Usage
|
|
181
207
|
*
|
|
182
208
|
* ```typescript
|
|
183
209
|
* import {InjectorService} from "@tsed/di";
|
|
184
210
|
* import MyService from "./services.js";
|
|
185
211
|
*
|
|
186
212
|
* class OtherService {
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
213
|
+
* constructor(injectorService: InjectorService) {
|
|
214
|
+
* const myService = injectorService.invoke<MyService>(MyService);
|
|
215
|
+
* }
|
|
216
|
+
* }
|
|
191
217
|
* ```
|
|
192
218
|
*
|
|
193
|
-
* @param token The injectable class to invoke.
|
|
194
|
-
* @param options
|
|
195
|
-
* @returns
|
|
196
|
-
* @
|
|
219
|
+
* @param token The injectable class to invoke. Constructor parameters are injected automatically
|
|
220
|
+
* @param options Optional invocation options (locals, rebuild, useOpts, etc.)
|
|
221
|
+
* @returns The resolved provider instance
|
|
222
|
+
* @see resolve
|
|
197
223
|
*/
|
|
198
224
|
invoke(token, options = {}) {
|
|
199
225
|
return this.resolve(token, options);
|
|
200
226
|
}
|
|
201
227
|
/**
|
|
202
|
-
* Build
|
|
228
|
+
* Build all asynchronous providers.
|
|
229
|
+
*
|
|
230
|
+
* Resolves providers that have async factories or async initialization.
|
|
231
|
+
* Called automatically during the `load()` process.
|
|
203
232
|
*/
|
|
204
233
|
async loadAsync() {
|
|
205
234
|
for (const [, provider] of this) {
|
|
@@ -209,7 +238,10 @@ export class InjectorService extends Container {
|
|
|
209
238
|
}
|
|
210
239
|
}
|
|
211
240
|
/**
|
|
212
|
-
* Build
|
|
241
|
+
* Build all synchronous singleton providers.
|
|
242
|
+
*
|
|
243
|
+
* Instantiates all singleton-scoped providers that haven't been cached yet.
|
|
244
|
+
* Called automatically during the `load()` process after async providers.
|
|
213
245
|
*/
|
|
214
246
|
loadSync() {
|
|
215
247
|
for (const [, provider] of this) {
|
|
@@ -220,9 +252,20 @@ export class InjectorService extends Container {
|
|
|
220
252
|
}
|
|
221
253
|
}
|
|
222
254
|
/**
|
|
223
|
-
*
|
|
255
|
+
* Load and initialize all registered providers.
|
|
224
256
|
*
|
|
225
|
-
*
|
|
257
|
+
* Bootstraps the DI system by resolving configuration, building provider instances,
|
|
258
|
+
* and triggering lifecycle hooks (`$beforeInit`, `$onInit`).
|
|
259
|
+
*
|
|
260
|
+
* ### Usage
|
|
261
|
+
*
|
|
262
|
+
* ```typescript
|
|
263
|
+
* import {injector} from "@tsed/di";
|
|
264
|
+
*
|
|
265
|
+
* await injector().load();
|
|
266
|
+
* ```
|
|
267
|
+
*
|
|
268
|
+
* @param container Optional container with additional providers to merge
|
|
226
269
|
*/
|
|
227
270
|
async load(container = createContainer()) {
|
|
228
271
|
// avoid provider registration in the GlobalContainer during the loading phase
|
|
@@ -237,8 +280,11 @@ export class InjectorService extends Container {
|
|
|
237
280
|
await $asyncEmit("$onInit");
|
|
238
281
|
}
|
|
239
282
|
/**
|
|
240
|
-
*
|
|
241
|
-
*
|
|
283
|
+
* Resolve and merge all provider configurations.
|
|
284
|
+
*
|
|
285
|
+
* Collects configuration from all providers decorated with `@Configuration`,
|
|
286
|
+
* merges them, and stores the result in the injector settings.
|
|
287
|
+
* Called automatically during the load process.
|
|
242
288
|
*/
|
|
243
289
|
async resolveConfiguration() {
|
|
244
290
|
if (this.resolvedConfiguration) {
|
|
@@ -1,23 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Enable automatic dependency injection for a class constructor.
|
|
3
|
+
*
|
|
4
|
+
* Automatically resolves and injects constructor dependencies while preserving
|
|
5
|
+
* the ability to manually pass arguments. Useful for classes that need both
|
|
6
|
+
* automatic DI and manual instantiation flexibility.
|
|
7
|
+
*
|
|
8
|
+
* ### Usage
|
|
3
9
|
*
|
|
4
|
-
* @example
|
|
5
10
|
* ```typescript
|
|
11
|
+
* import {AutoInjectable} from "@tsed/di";
|
|
12
|
+
*
|
|
6
13
|
* @AutoInjectable()
|
|
7
14
|
* class UserService {
|
|
8
15
|
* constructor(
|
|
9
|
-
* private database: Database,
|
|
10
|
-
*
|
|
16
|
+
* private database: Database, // Automatically injected
|
|
17
|
+
* private logger: Logger, // Automatically injected
|
|
18
|
+
* customConfig?: Config // Can be passed manually
|
|
11
19
|
* ) {}
|
|
12
20
|
* }
|
|
21
|
+
*
|
|
22
|
+
* // Automatic injection
|
|
23
|
+
* const service1 = new UserService();
|
|
24
|
+
*
|
|
25
|
+
* // Manual override
|
|
26
|
+
* const service2 = new UserService(customDb, customLogger, {debug: true});
|
|
27
|
+
*
|
|
28
|
+
* // Partial override (remaining deps auto-injected)
|
|
29
|
+
* const service3 = new UserService(undefined, undefined, {debug: true});
|
|
13
30
|
* ```
|
|
14
31
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* - Automatically resolves
|
|
18
|
-
* -
|
|
32
|
+
* ### Behavior
|
|
33
|
+
*
|
|
34
|
+
* - Automatically resolves dependencies based on TypeScript metadata
|
|
35
|
+
* - Manual arguments override automatic injection for that position
|
|
36
|
+
* - `undefined` arguments trigger automatic injection
|
|
19
37
|
*
|
|
38
|
+
* @returns Class decorator function
|
|
39
|
+
* @public
|
|
20
40
|
* @decorator
|
|
21
|
-
* @returns {ClassDecorator} A class decorator that enables automatic dependency injection
|
|
22
41
|
*/
|
|
23
|
-
export declare function AutoInjectable(): <T extends
|
|
42
|
+
export declare function AutoInjectable(): <T extends {
|
|
43
|
+
new (...args: any[]): NonNullable<unknown>;
|
|
44
|
+
}>(constr: T) => T;
|
|
@@ -1,8 +1,38 @@
|
|
|
1
1
|
import { DIConfiguration } from "../services/DIConfiguration.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Associate configuration with a class or inject configuration into a constructor parameter.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* When used as a class decorator, stores configuration metadata on the provider.
|
|
6
|
+
* When used as a parameter decorator, injects the `DIConfiguration` instance.
|
|
7
|
+
*
|
|
8
|
+
* ### Usage
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import {Configuration, Module} from "@tsed/di";
|
|
12
|
+
*
|
|
13
|
+
* // As class decorator
|
|
14
|
+
* @Configuration({
|
|
15
|
+
* rootDir: __dirname,
|
|
16
|
+
* port: 3000,
|
|
17
|
+
* mount: {
|
|
18
|
+
* "/api": []
|
|
19
|
+
* }
|
|
20
|
+
* })
|
|
21
|
+
* @Module({})
|
|
22
|
+
* export class Server {}
|
|
23
|
+
*
|
|
24
|
+
* // As parameter decorator
|
|
25
|
+
* @Injectable()
|
|
26
|
+
* export class MyService {
|
|
27
|
+
* constructor(@Configuration() config: Configuration) {
|
|
28
|
+
* console.log(config.get("port"));
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param settings Optional configuration object to associate with the class
|
|
34
|
+
* @returns Decorator function (class or parameter)
|
|
35
|
+
* @public
|
|
6
36
|
*/
|
|
7
37
|
export declare function Configuration(): any;
|
|
8
38
|
export declare function Configuration(settings: Partial<TsED.Configuration>): ClassDecorator;
|
|
@@ -1,37 +1,41 @@
|
|
|
1
1
|
export declare function bindConstant(target: any, propertyKey: string | symbol, expression: string, defaultValue?: any): void;
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Inject a frozen configuration value into a property.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* Retrieves a value from the injector configuration and freezes it (creates an immutable deep clone).
|
|
6
|
+
* The value is lazily loaded on first access and cached for subsequent accesses.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
* import {Env} from "@tsed/core/domain/Env.js";
|
|
9
|
-
* import {Constant, Value} from "@tsed/di";
|
|
8
|
+
* ### Usage
|
|
10
9
|
*
|
|
11
|
-
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import {Injectable, Constant} from "@tsed/di";
|
|
12
|
+
* import {Env} from "@tsed/core";
|
|
12
13
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
14
|
+
* @Injectable()
|
|
15
|
+
* export class MyService {
|
|
16
|
+
* @Constant("env")
|
|
17
|
+
* env: Env;
|
|
15
18
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
19
|
+
* @Constant("api.baseUrl")
|
|
20
|
+
* apiUrl: string;
|
|
18
21
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
22
|
+
* @Constant("api.timeout", 5000)
|
|
23
|
+
* timeout: number;
|
|
21
24
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
+
* constructor() {
|
|
26
|
+
* // Not available yet - undefined
|
|
27
|
+
* }
|
|
25
28
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
+
* $onInit() {
|
|
30
|
+
* console.log(this.env); // Value is available
|
|
31
|
+
* }
|
|
29
32
|
* }
|
|
30
33
|
* ```
|
|
31
34
|
*
|
|
32
|
-
* @
|
|
33
|
-
* @param
|
|
34
|
-
* @
|
|
35
|
-
* @decorator
|
|
35
|
+
* @typeParam Type The expected type of the constant value
|
|
36
|
+
* @param expression Dot-notation path to the configuration value
|
|
37
|
+
* @param defaultValue Optional default value if not found
|
|
38
|
+
* @returns Property decorator function
|
|
39
|
+
* @public
|
|
36
40
|
*/
|
|
37
41
|
export declare function Constant<Type = unknown>(expression: string, defaultValue?: Type): PropertyDecorator;
|
|
@@ -8,31 +8,51 @@ export interface ControllerOptions extends Partial<ProviderOpts<any>> {
|
|
|
8
8
|
middlewares?: Partial<ControllerMiddlewares>;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
|
-
* Declare a
|
|
12
|
-
* This routing listing will be built with the `express.Router` object.
|
|
11
|
+
* Declare a REST controller with a base path for routing.
|
|
13
12
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
13
|
+
* Registers a class as a controller provider that handles HTTP requests.
|
|
14
|
+
* Annotated methods define routes relative to the controller's base path.
|
|
15
|
+
*
|
|
16
|
+
* ### Usage
|
|
17
17
|
*
|
|
18
18
|
* ```typescript
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
19
|
+
* import {Controller, Get} from "@tsed/di";
|
|
20
|
+
*
|
|
21
|
+
* @Controller("/calendars")
|
|
22
|
+
* export class CalendarCtrl {
|
|
23
|
+
* @Get("/:id")
|
|
24
|
+
* get(@PathParams("id") id: string) {
|
|
25
|
+
* return {id};
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* // With middleware
|
|
30
|
+
* @Controller({
|
|
31
|
+
* path: "/users",
|
|
32
|
+
* middlewares: {
|
|
33
|
+
* useBefore: [AuthMiddleware]
|
|
34
|
+
* }
|
|
35
|
+
* })
|
|
36
|
+
* export class UserCtrl {}
|
|
37
|
+
*
|
|
38
|
+
* // Nested controllers
|
|
39
|
+
* @Controller({
|
|
40
|
+
* path: "/admin",
|
|
41
|
+
* children: [UserAdminCtrl, ProductAdminCtrl]
|
|
42
|
+
* })
|
|
43
|
+
* export class AdminCtrl {}
|
|
31
44
|
* ```
|
|
32
45
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
46
|
+
* ### Options
|
|
47
|
+
*
|
|
48
|
+
* - `path`: Base route path (string, RegExp, or array)
|
|
49
|
+
* - `children`: Child controller classes for nested routing
|
|
50
|
+
* - `middlewares`: Middleware configuration (useBefore, use, useAfter)
|
|
51
|
+
* - Additional `ProviderOpts` options
|
|
52
|
+
*
|
|
53
|
+
* @param options Controller path or configuration object
|
|
54
|
+
* @returns Class decorator function
|
|
55
|
+
* @public
|
|
35
56
|
* @decorator
|
|
36
|
-
* @classDecorator
|
|
37
57
|
*/
|
|
38
58
|
export declare function Controller(options: PathType | ControllerOptions): ClassDecorator;
|
|
@@ -11,22 +11,44 @@ export interface BindInjectablePropertyOpts<T = any> {
|
|
|
11
11
|
transform?: TransformInjectedProviderCB<T>;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
* Inject a provider
|
|
14
|
+
* Inject a provider into a constructor parameter or property.
|
|
15
15
|
*
|
|
16
|
-
*
|
|
16
|
+
* Explicitly declares a dependency injection for constructor parameters or class properties.
|
|
17
|
+
* Supports custom tokens, transformations, and injection options.
|
|
18
|
+
*
|
|
19
|
+
* ### Usage
|
|
17
20
|
*
|
|
18
21
|
* ```typescript
|
|
22
|
+
* import {Injectable, Inject} from "@tsed/di";
|
|
23
|
+
*
|
|
24
|
+
* const CONNECTION = Symbol.for("CONNECTION");
|
|
25
|
+
*
|
|
19
26
|
* @Injectable()
|
|
20
27
|
* export class MyService {
|
|
28
|
+
* // Property injection
|
|
21
29
|
* @Inject(CONNECTION)
|
|
22
|
-
* connection:
|
|
30
|
+
* connection: Database;
|
|
31
|
+
*
|
|
32
|
+
* // Constructor injection
|
|
33
|
+
* constructor(
|
|
34
|
+
* @Inject(CONNECTION) private db: Database,
|
|
35
|
+
* private otherService: OtherService // Auto-injected
|
|
36
|
+
* ) {}
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // With transformation
|
|
40
|
+
* @Injectable()
|
|
41
|
+
* export class ProcessorService {
|
|
42
|
+
* @Inject(DataService, (service) => service.getData())
|
|
43
|
+
* initialData: any[];
|
|
23
44
|
* }
|
|
24
45
|
* ```
|
|
25
46
|
*
|
|
26
|
-
* @
|
|
27
|
-
* @param
|
|
28
|
-
* @
|
|
29
|
-
* @
|
|
47
|
+
* @typeParam T The type of the injected provider
|
|
48
|
+
* @param token The provider token to inject (optional for constructor params with TypeScript metadata)
|
|
49
|
+
* @param transform Optional transformation function or injection options
|
|
50
|
+
* @returns Decorator function (property or parameter)
|
|
51
|
+
* @public
|
|
30
52
|
*/
|
|
31
53
|
export declare function Inject<T = any>(token?: TokenProvider<T> | (() => TokenProvider<T>), transform?: TransformInjectedProviderCB<T>): any;
|
|
32
54
|
export declare function Inject<T = any>(token?: TokenProvider<T> | (() => TokenProvider<T>), opts?: Partial<Omit<BindInjectablePropertyOpts<T>, "token">>): any;
|