@lppedd/di-wise-neo 0.3.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/LICENSE +22 -0
- package/README.md +488 -0
- package/dist/cjs/index.d.ts +772 -0
- package/dist/cjs/index.js +1055 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/es/index.d.mts +772 -0
- package/dist/es/index.mjs +1037 -0
- package/dist/es/index.mjs.map +1 -0
- package/package.json +77 -0
- package/src/container.ts +292 -0
- package/src/decorators/autoRegister.ts +24 -0
- package/src/decorators/decorators.ts +47 -0
- package/src/decorators/index.ts +7 -0
- package/src/decorators/inject.ts +42 -0
- package/src/decorators/injectAll.ts +45 -0
- package/src/decorators/injectable.ts +61 -0
- package/src/decorators/optional.ts +39 -0
- package/src/decorators/optionalAll.ts +42 -0
- package/src/decorators/scoped.ts +32 -0
- package/src/defaultContainer.ts +519 -0
- package/src/errors.ts +47 -0
- package/src/index.ts +16 -0
- package/src/inject.ts +88 -0
- package/src/injectAll.ts +21 -0
- package/src/injectionContext.ts +46 -0
- package/src/injector.ts +117 -0
- package/src/metadata.ts +41 -0
- package/src/middleware.ts +85 -0
- package/src/optional.ts +65 -0
- package/src/optionalAll.ts +19 -0
- package/src/provider.ts +61 -0
- package/src/scope.ts +8 -0
- package/src/token.ts +84 -0
- package/src/tokenRegistry.ts +165 -0
- package/src/tokensRef.ts +46 -0
- package/src/utils/context.ts +19 -0
- package/src/utils/disposable.ts +10 -0
- package/src/utils/invariant.ts +6 -0
- package/src/utils/keyedStack.ts +26 -0
- package/src/utils/typeName.ts +28 -0
- package/src/utils/weakRefMap.ts +28 -0
- package/src/valueRef.ts +3 -0
@@ -0,0 +1,772 @@
|
|
1
|
+
/**
|
2
|
+
* Constructor type.
|
3
|
+
*/
|
4
|
+
interface Constructor<Instance extends object> {
|
5
|
+
new (...args: any[]): Instance;
|
6
|
+
readonly name: string;
|
7
|
+
readonly length: number;
|
8
|
+
}
|
9
|
+
/**
|
10
|
+
* Token type.
|
11
|
+
*/
|
12
|
+
type Token<Value = any> = [Value] extends [object] ? Type<Value> | Constructor<Value> : Type<Value>;
|
13
|
+
/**
|
14
|
+
* Describes a {@link Token} array with at least one element.
|
15
|
+
*/
|
16
|
+
type Tokens<Value = any> = [Token<Value>, ...Token<Value>[]];
|
17
|
+
/**
|
18
|
+
* Type API.
|
19
|
+
*/
|
20
|
+
interface Type<A> {
|
21
|
+
/**
|
22
|
+
* Name of the type.
|
23
|
+
*/
|
24
|
+
readonly name: string;
|
25
|
+
/**
|
26
|
+
* Create an intersection type from another type.
|
27
|
+
*
|
28
|
+
* @example
|
29
|
+
* ```ts
|
30
|
+
* const A = Type<A>("A");
|
31
|
+
* const B = Type<B>("B");
|
32
|
+
*
|
33
|
+
* A.inter("I", B); // => Type<A & B>
|
34
|
+
* ```
|
35
|
+
*/
|
36
|
+
inter<B>(typeName: string, B: Type<B>): Type<A & B>;
|
37
|
+
/**
|
38
|
+
* Create a union type from another type.
|
39
|
+
*
|
40
|
+
* @example
|
41
|
+
* ```ts
|
42
|
+
* const A = Type<A>("A");
|
43
|
+
* const B = Type<B>("B");
|
44
|
+
*
|
45
|
+
* A.union("U", B); // => Type<A | B>
|
46
|
+
* ```
|
47
|
+
*/
|
48
|
+
union<B>(typeName: string, B: Type<B>): Type<A | B>;
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Create a type token.
|
52
|
+
*
|
53
|
+
* @example
|
54
|
+
* ```ts
|
55
|
+
* const Spell = Type<Spell>("Spell");
|
56
|
+
* ```
|
57
|
+
*
|
58
|
+
* @__NO_SIDE_EFFECTS__
|
59
|
+
*/
|
60
|
+
declare function Type<T>(typeName: string): Type<T>;
|
61
|
+
|
62
|
+
/**
|
63
|
+
* Provides a class instance for a token via a class constructor.
|
64
|
+
*/
|
65
|
+
interface ClassProvider<Instance extends object> {
|
66
|
+
readonly useClass: Constructor<Instance>;
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* Provides a value for a token via another existing token.
|
70
|
+
*/
|
71
|
+
interface ExistingProvider<Value> {
|
72
|
+
readonly useExisting: Token<Value>;
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* Provides a value for a token via a factory function.
|
76
|
+
*
|
77
|
+
* The factory function runs inside the injection context
|
78
|
+
* and can thus access dependencies via {@link inject}.
|
79
|
+
*/
|
80
|
+
interface FactoryProvider<Value> {
|
81
|
+
readonly useFactory: (...args: []) => Value;
|
82
|
+
}
|
83
|
+
/**
|
84
|
+
* Provides a direct - already constructed - value for a token.
|
85
|
+
*/
|
86
|
+
interface ValueProvider<T> {
|
87
|
+
readonly useValue: T;
|
88
|
+
}
|
89
|
+
/**
|
90
|
+
* A token provider.
|
91
|
+
*/
|
92
|
+
type Provider<Value = any> = ClassProvider<Value & object> | ExistingProvider<Value> | FactoryProvider<Value> | ValueProvider<Value>;
|
93
|
+
|
94
|
+
declare const Scope: {
|
95
|
+
readonly Inherited: "Inherited";
|
96
|
+
readonly Transient: "Transient";
|
97
|
+
readonly Resolution: "Resolution";
|
98
|
+
readonly Container: "Container";
|
99
|
+
};
|
100
|
+
type Scope = (typeof Scope)[keyof typeof Scope];
|
101
|
+
|
102
|
+
/**
|
103
|
+
* Token registration options.
|
104
|
+
*/
|
105
|
+
interface RegistrationOptions {
|
106
|
+
/**
|
107
|
+
* The scope of the registration.
|
108
|
+
*/
|
109
|
+
readonly scope?: Scope;
|
110
|
+
}
|
111
|
+
/**
|
112
|
+
* Create a one-off type token from a factory function.
|
113
|
+
*
|
114
|
+
* @example
|
115
|
+
* ```ts
|
116
|
+
* class Wizard {
|
117
|
+
* wand = inject(
|
118
|
+
* Build(() => {
|
119
|
+
* const wand = inject(Wand);
|
120
|
+
* wand.owner = this;
|
121
|
+
* // ...
|
122
|
+
* return wand;
|
123
|
+
* }),
|
124
|
+
* );
|
125
|
+
* }
|
126
|
+
* ```
|
127
|
+
*
|
128
|
+
* @__NO_SIDE_EFFECTS__
|
129
|
+
*/
|
130
|
+
declare function Build<Value>(factory: (...args: []) => Value): Type<Value>;
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Container creation options.
|
134
|
+
*/
|
135
|
+
interface ContainerOptions {
|
136
|
+
/**
|
137
|
+
* Whether to automatically register an unregistered class when resolving it as a token.
|
138
|
+
*
|
139
|
+
* @defaultValue false
|
140
|
+
*/
|
141
|
+
readonly autoRegister: boolean;
|
142
|
+
/**
|
143
|
+
* The default scope for registrations.
|
144
|
+
*
|
145
|
+
* @defaultValue Scope.Inherited
|
146
|
+
*/
|
147
|
+
readonly defaultScope: Scope;
|
148
|
+
}
|
149
|
+
/**
|
150
|
+
* Container API.
|
151
|
+
*/
|
152
|
+
interface Container {
|
153
|
+
/**
|
154
|
+
* The options used to create this container.
|
155
|
+
*/
|
156
|
+
readonly options: ContainerOptions;
|
157
|
+
/**
|
158
|
+
* The parent container, or `undefined` if this is the root container.
|
159
|
+
*/
|
160
|
+
readonly parent: Container | undefined;
|
161
|
+
/**
|
162
|
+
* Whether this container is disposed.
|
163
|
+
*/
|
164
|
+
readonly isDisposed: boolean;
|
165
|
+
/**
|
166
|
+
* Creates a new child container that inherits this container's options.
|
167
|
+
*
|
168
|
+
* You can pass specific options to override the inherited ones.
|
169
|
+
*/
|
170
|
+
createChild(options?: Partial<ContainerOptions>): Container;
|
171
|
+
/**
|
172
|
+
* Clears and returns all distinct cached values from this container's internal registry.
|
173
|
+
* Values from {@link ValueProvider} registrations are not included, as they are never cached.
|
174
|
+
*
|
175
|
+
* Note that only this container is affected. Parent containers, if any, remain unchanged.
|
176
|
+
*/
|
177
|
+
clearCache(): unknown[];
|
178
|
+
/**
|
179
|
+
* Returns the cached value from the most recent registration of the token,
|
180
|
+
* or `undefined` if no value has been cached yet (the token has not been resolved yet).
|
181
|
+
*
|
182
|
+
* If the token has at least one registration in this container,
|
183
|
+
* the cached value is taken from the most recent of those registrations.
|
184
|
+
* Otherwise, it may be retrieved from parent containers, if any.
|
185
|
+
*
|
186
|
+
* Values are never cached for tokens with _transient_ or _resolution_ scope,
|
187
|
+
* or for {@link ValueProvider} registrations.
|
188
|
+
*/
|
189
|
+
getCached<Value>(token: Token<Value>): Value | undefined;
|
190
|
+
/**
|
191
|
+
* Returns all cached values associated with registrations of the token,
|
192
|
+
* in the order they were registered, or an empty array if none have been cached.
|
193
|
+
*
|
194
|
+
* If the token has at least one registration in the current container,
|
195
|
+
* cached values are taken from those registrations.
|
196
|
+
* Otherwise, cached values may be retrieved from parent containers, if any.
|
197
|
+
*
|
198
|
+
* Values are never cached for tokens with _transient_ or _resolution_ scope,
|
199
|
+
* or for {@link ValueProvider} registrations.
|
200
|
+
*/
|
201
|
+
getAllCached<Value>(token: Token<Value>): Value[];
|
202
|
+
/**
|
203
|
+
* Removes all registrations from this container's internal registry.
|
204
|
+
*
|
205
|
+
* Returns an array of distinct cached values that were stored within the removed registrations.
|
206
|
+
* Values from {@link ValueProvider} registrations are not included, as they are not cached.
|
207
|
+
*
|
208
|
+
* Note that only this container is affected. Parent containers, if any, remain unchanged.
|
209
|
+
*/
|
210
|
+
resetRegistry(): unknown[];
|
211
|
+
/**
|
212
|
+
* Returns whether the token is registered in this container or in parent containers, if any.
|
213
|
+
*/
|
214
|
+
isRegistered(token: Token): boolean;
|
215
|
+
/**
|
216
|
+
* Registers a {@link ClassProvider}, using the class itself as its token.
|
217
|
+
*
|
218
|
+
* Tokens provided to the {@link Injectable} decorator applied to the class
|
219
|
+
* are also registered as aliases. The scope is determined by the {@link Scoped}
|
220
|
+
* decorator, if present.
|
221
|
+
*/
|
222
|
+
register<Instance extends object>(Class: Constructor<Instance>): this;
|
223
|
+
/**
|
224
|
+
* Registers a {@link ClassProvider} with a token.
|
225
|
+
*/
|
226
|
+
register<Instance extends object, ProviderInstance extends Instance>(token: Token<Instance>, provider: ClassProvider<ProviderInstance>, options?: RegistrationOptions): this;
|
227
|
+
/**
|
228
|
+
* Registers a {@link FactoryProvider} with a token.
|
229
|
+
*/
|
230
|
+
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: FactoryProvider<ProviderValue>, options?: RegistrationOptions): this;
|
231
|
+
/**
|
232
|
+
* Registers an {@link ExistingProvider} with a token.
|
233
|
+
*
|
234
|
+
* The token will alias the one set in `useExisting`.
|
235
|
+
*/
|
236
|
+
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ExistingProvider<ProviderValue>): this;
|
237
|
+
/**
|
238
|
+
* Registers a {@link ValueProvider} with a token.
|
239
|
+
*
|
240
|
+
* Values provided via `useValue` are never cached (scopes do not apply)
|
241
|
+
* and are simply returned as-is.
|
242
|
+
*/
|
243
|
+
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ValueProvider<ProviderValue>): this;
|
244
|
+
/**
|
245
|
+
* Removes all registrations for the given token from the container's internal registry.
|
246
|
+
*
|
247
|
+
* Returns an array of distinct cached values that were stored within the removed registrations.
|
248
|
+
* Values from {@link ValueProvider} registrations are not included, as they are not cached.
|
249
|
+
*
|
250
|
+
* Note that only this container is affected. Parent containers, if any, remain unchanged.
|
251
|
+
*/
|
252
|
+
unregister<Value>(token: Token<Value>): Value[];
|
253
|
+
/**
|
254
|
+
* Resolves the given class to the instance associated with it.
|
255
|
+
*
|
256
|
+
* If the class is registered in this container or any of its parent containers,
|
257
|
+
* an instance is created using the most recent registration.
|
258
|
+
*
|
259
|
+
* If the class is not registered, but it is decorated with {@link AutoRegister},
|
260
|
+
* or {@link ContainerOptions.autoRegister} is true, the class is registered automatically.
|
261
|
+
* Otherwise, resolution fails.
|
262
|
+
* The scope for the automatic registration is determined by either
|
263
|
+
* the {@link Scoped} decorator on the class, or {@link ContainerOptions.defaultScope}.
|
264
|
+
*
|
265
|
+
* If `optional` is false or is not passed and the class is not registered in the container
|
266
|
+
* (and could not be auto-registered), an error is thrown.
|
267
|
+
* Otherwise, if `optional` is true, `undefined` is returned.
|
268
|
+
*
|
269
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
270
|
+
* - For {@link ValueProvider}, the explicitly provided instance is returned.
|
271
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the instance.
|
272
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
273
|
+
* - For {@link ExistingProvider}, the instance is resolved by referring to another registered token.
|
274
|
+
*
|
275
|
+
* If the class is registered with _container_ scope, the resolved instance is cached
|
276
|
+
* in the container's internal registry.
|
277
|
+
*/
|
278
|
+
resolve<Instance extends object>(Class: Constructor<Instance>, optional?: false): Instance;
|
279
|
+
resolve<Instance extends object>(Class: Constructor<Instance>, optional: true): Instance | undefined;
|
280
|
+
resolve<Instance extends object>(Class: Constructor<Instance>, optional: boolean): Instance | undefined;
|
281
|
+
/**
|
282
|
+
* Resolves the given token to the value associated with it.
|
283
|
+
*
|
284
|
+
* If the token is registered in this container or any of its parent containers,
|
285
|
+
* its value is resolved using the most recent registration.
|
286
|
+
*
|
287
|
+
* If `optional` is false or not passed and the token is not registered in the container,
|
288
|
+
* an error is thrown. Otherwise, if `optional` is true, `undefined` is returned.
|
289
|
+
*
|
290
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
291
|
+
* - For {@link ValueProvider}, the explicitly provided value is returned.
|
292
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the value.
|
293
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
294
|
+
* - For {@link ExistingProvider}, the value is resolved by referring to another registered token.
|
295
|
+
*
|
296
|
+
* If the token is registered with _container_ scope, the resolved value is cached
|
297
|
+
* in the container's internal registry.
|
298
|
+
*/
|
299
|
+
resolve<Value>(token: Token<Value>, optional?: false): Value;
|
300
|
+
resolve<Value>(token: Token<Value>, optional: true): Value | undefined;
|
301
|
+
resolve<Value>(token: Token<Value>, optional: boolean): Value | undefined;
|
302
|
+
/**
|
303
|
+
* Resolves the given class to all instances provided by the registrations associated with it.
|
304
|
+
*
|
305
|
+
* If the class is not registered, but it is decorated with {@link AutoRegister},
|
306
|
+
* or {@link ContainerOptions.autoRegister} is true, the class is registered automatically.
|
307
|
+
* Otherwise, resolution fails.
|
308
|
+
* The scope for the automatic registration is determined by either
|
309
|
+
* the {@link Scoped} decorator on the class, or {@link ContainerOptions.defaultScope}.
|
310
|
+
*
|
311
|
+
* If `optional` is false or is not passed and the class is not registered in the container
|
312
|
+
* (and could not be auto-registered), an error is thrown.
|
313
|
+
* Otherwise, if `optional` is true, an empty array is returned.
|
314
|
+
*
|
315
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
316
|
+
* - For {@link ValueProvider}, the explicitly provided instance is returned.
|
317
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the instance.
|
318
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
319
|
+
* - For {@link ExistingProvider}, the instance is resolved by referring to another registered token.
|
320
|
+
*
|
321
|
+
* If the class is registered with _container_ scope, the resolved instances are cached
|
322
|
+
* in the container's internal registry.
|
323
|
+
*
|
324
|
+
* A separate instance of the class is created for each provider.
|
325
|
+
*
|
326
|
+
* @see The documentation for `resolve(Class: Constructor)`
|
327
|
+
*/
|
328
|
+
resolveAll<Instance extends object>(Class: Constructor<Instance>, optional?: false): Instance[];
|
329
|
+
resolveAll<Instance extends object>(Class: Constructor<Instance>, optional: true): Instance[];
|
330
|
+
resolveAll<Instance extends object>(Class: Constructor<Instance>, optional: boolean): Instance[];
|
331
|
+
/**
|
332
|
+
* Resolves the given token to all values provided by the registrations associated with it.
|
333
|
+
*
|
334
|
+
* If `optional` is false or not passed and the token is not registered in the container,
|
335
|
+
* an error is thrown. Otherwise, if `optional` is true, an empty array is returned.
|
336
|
+
*
|
337
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
338
|
+
* - For {@link ValueProvider}, the explicitly provided value is returned.
|
339
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the value.
|
340
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
341
|
+
* - For {@link ExistingProvider}, the value is resolved by referring to another registered token.
|
342
|
+
*
|
343
|
+
* If the token is registered with _container_ scope, the resolved values are cached
|
344
|
+
* in the container's internal registry.
|
345
|
+
*
|
346
|
+
* @see The documentation for `resolve(token: Token)`
|
347
|
+
*/
|
348
|
+
resolveAll<Value>(token: Token<Value>, optional?: false): NonNullable<Value>[];
|
349
|
+
resolveAll<Value>(token: Token<Value>, optional: true): NonNullable<Value>[];
|
350
|
+
resolveAll<Value>(token: Token<Value>, optional: boolean): NonNullable<Value>[];
|
351
|
+
/**
|
352
|
+
* Disposes this container and all its cached values.
|
353
|
+
*
|
354
|
+
* Token values implementing a `Disposable` interface (e.g., objects with a `dispose()` function)
|
355
|
+
* are automatically disposed during this process.
|
356
|
+
*
|
357
|
+
* Note that children containers are disposed first, in creation order.
|
358
|
+
*/
|
359
|
+
dispose(): void;
|
360
|
+
}
|
361
|
+
/**
|
362
|
+
* Creates a new container.
|
363
|
+
*/
|
364
|
+
declare function createContainer(options?: Partial<ContainerOptions>): Container;
|
365
|
+
|
366
|
+
/**
|
367
|
+
* Class decorator for enabling auto-registration of an unregistered class
|
368
|
+
* when first resolving it from the container.
|
369
|
+
*
|
370
|
+
* @example
|
371
|
+
* ```ts
|
372
|
+
* @AutoRegister()
|
373
|
+
* class Wizard {}
|
374
|
+
*
|
375
|
+
* const wizard = container.resolve(Wizard);
|
376
|
+
* container.isRegistered(Wizard); // => true
|
377
|
+
* ```
|
378
|
+
*
|
379
|
+
* @__NO_SIDE_EFFECTS__
|
380
|
+
*/
|
381
|
+
declare function AutoRegister(enable?: boolean): ClassDecorator;
|
382
|
+
|
383
|
+
interface TokensRef<Value = any> {
|
384
|
+
readonly getRefTokens: () => Set<Token<Value>>;
|
385
|
+
}
|
386
|
+
interface TokenRef<Value = any> {
|
387
|
+
readonly getRefToken: () => Token<Value>;
|
388
|
+
}
|
389
|
+
/**
|
390
|
+
* Allows referencing a token that is declared later in the file by wrapping it in a function.
|
391
|
+
*/
|
392
|
+
declare function forwardRef<Value>(token: () => Tokens<Value>): TokensRef<Value>;
|
393
|
+
declare function forwardRef<Value>(token: () => Token<Value>): TokenRef<Value>;
|
394
|
+
|
395
|
+
/**
|
396
|
+
* Parameter decorator that injects the instance associated with the given class.
|
397
|
+
*
|
398
|
+
* Throws an error if the class is not registered in the container.
|
399
|
+
*/
|
400
|
+
declare function Inject<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
|
401
|
+
/**
|
402
|
+
* Parameter decorator that injects the value associated with the given token.
|
403
|
+
*
|
404
|
+
* Throws an error if the token is not registered in the container.
|
405
|
+
*/
|
406
|
+
declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
|
407
|
+
/**
|
408
|
+
* Parameter decorator that injects the value associated with the given token.
|
409
|
+
*
|
410
|
+
* Allows referencing a token that is declared later in the file by using
|
411
|
+
* the {@link forwardRef} helper function.
|
412
|
+
*
|
413
|
+
* Throws an error if the token is not registered in the container.
|
414
|
+
*
|
415
|
+
* @example
|
416
|
+
* ```ts
|
417
|
+
* class Wizard {
|
418
|
+
* constructor(@Inject(forwardRef(() => Wand)) readonly wand: Wand) {}
|
419
|
+
* }
|
420
|
+
* // Other code...
|
421
|
+
* class Wand {}
|
422
|
+
* ```
|
423
|
+
*/
|
424
|
+
declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
425
|
+
|
426
|
+
/**
|
427
|
+
* Class decorator for registering additional aliasing tokens for the decorated type
|
428
|
+
* when registering it.
|
429
|
+
*
|
430
|
+
* The container uses {@link ExistingProvider} under the hood.
|
431
|
+
*
|
432
|
+
* @example
|
433
|
+
* ```ts
|
434
|
+
* @Injectable(Weapon)
|
435
|
+
* class Wand {}
|
436
|
+
* ```
|
437
|
+
*/
|
438
|
+
declare function Injectable<This extends object, Value extends This>(...tokens: Tokens<Value>): ClassDecorator;
|
439
|
+
/**
|
440
|
+
* Class decorator for registering additional aliasing tokens for the decorated type
|
441
|
+
* when registering it.
|
442
|
+
*
|
443
|
+
* The container uses {@link ExistingProvider} under the hood.
|
444
|
+
*
|
445
|
+
* Allows referencing tokens that are declared later in the file by using
|
446
|
+
* the {@link forwardRef} helper function.
|
447
|
+
*
|
448
|
+
* @example
|
449
|
+
* ```ts
|
450
|
+
* @Injectable(forwardRef() => Weapon) // Weapon is declared after Wand
|
451
|
+
* class Wizard {}
|
452
|
+
* // Other code...
|
453
|
+
* class Weapon {}
|
454
|
+
* ```
|
455
|
+
*/
|
456
|
+
declare function Injectable<This extends object, Value extends This>(tokens: TokenRef<Value> | TokensRef<Value>): ClassDecorator;
|
457
|
+
|
458
|
+
/**
|
459
|
+
* Parameter decorator that injects all instances provided by the registrations
|
460
|
+
* associated with the given class.
|
461
|
+
*
|
462
|
+
* Throws an error if the class is not registered in the container.
|
463
|
+
*/
|
464
|
+
declare function InjectAll<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
|
465
|
+
/**
|
466
|
+
* Parameter decorator that injects all values provided by the registrations
|
467
|
+
* associated with the given token.
|
468
|
+
*
|
469
|
+
* Throws an error if the token is not registered in the container.
|
470
|
+
*/
|
471
|
+
declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
|
472
|
+
/**
|
473
|
+
* Parameter decorator that injects all values provided by the registrations
|
474
|
+
* associated with the given token.
|
475
|
+
*
|
476
|
+
* Allows referencing a token that is declared later in the file by using
|
477
|
+
* the {@link forwardRef} helper function.
|
478
|
+
*
|
479
|
+
* Throws an error if the token is not registered in the container.
|
480
|
+
*
|
481
|
+
* @example
|
482
|
+
* ```ts
|
483
|
+
* class Wizard {
|
484
|
+
* constructor(@InjectAll(forwardRef(() => Wand)) readonly wands: Wand[]) {}
|
485
|
+
* }
|
486
|
+
* // Other code...
|
487
|
+
* class Wand {}
|
488
|
+
* ```
|
489
|
+
*/
|
490
|
+
declare function InjectAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
491
|
+
|
492
|
+
/**
|
493
|
+
* Parameter decorator that injects the instance associated with the given class,
|
494
|
+
* or `undefined` if the class is not registered in the container.
|
495
|
+
*/
|
496
|
+
declare function Optional<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
|
497
|
+
/**
|
498
|
+
* Parameter decorator that injects the value associated with the given token,
|
499
|
+
* or `undefined` if the token is not registered in the container.
|
500
|
+
*/
|
501
|
+
declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
|
502
|
+
/**
|
503
|
+
* Parameter decorator that injects the value associated with the given token,
|
504
|
+
* or `undefined` if the token is not registered in the container.
|
505
|
+
*
|
506
|
+
* Allows referencing a token that is declared later in the file by using
|
507
|
+
* the {@link forwardRef} helper function.
|
508
|
+
*
|
509
|
+
* @example
|
510
|
+
* ```ts
|
511
|
+
* class Wizard {
|
512
|
+
* constructor(@Optional(forwardRef(() => Wand)) readonly wand: Wand | undefined) {}
|
513
|
+
* }
|
514
|
+
* // Other code...
|
515
|
+
* class Wand {}
|
516
|
+
* ```
|
517
|
+
*/
|
518
|
+
declare function Optional<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
519
|
+
|
520
|
+
/**
|
521
|
+
* Parameter decorator that injects all instances provided by the registrations
|
522
|
+
* associated with the given class, or an empty array if the class is not registered
|
523
|
+
* in the container.
|
524
|
+
*/
|
525
|
+
declare function OptionalAll<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
|
526
|
+
/**
|
527
|
+
* Parameter decorator that injects all values provided by the registrations
|
528
|
+
* associated with the given token, or an empty array if the token is not registered
|
529
|
+
* in the container.
|
530
|
+
*/
|
531
|
+
declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
|
532
|
+
/**
|
533
|
+
* Parameter decorator that injects all values provided by the registrations
|
534
|
+
* associated with the given token, or an empty array if the token is not registered
|
535
|
+
* in the container.
|
536
|
+
*
|
537
|
+
* Allows referencing a token that is declared later in the file by using
|
538
|
+
* the {@link forwardRef} helper function.
|
539
|
+
*
|
540
|
+
* @example
|
541
|
+
* ```ts
|
542
|
+
* class Wizard {
|
543
|
+
* constructor(@OptionalAll(forwardRef(() => Wand)) readonly wands: Wand[]) {}
|
544
|
+
* }
|
545
|
+
* // Other code...
|
546
|
+
* class Wand {}
|
547
|
+
* ```
|
548
|
+
*/
|
549
|
+
declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
550
|
+
|
551
|
+
/**
|
552
|
+
* Class decorator for setting the scope of the decorated type when registering it.
|
553
|
+
*
|
554
|
+
* The scope set by this decorator can be overridden by explicit registration options, if provided.
|
555
|
+
*
|
556
|
+
* @example
|
557
|
+
* ```ts
|
558
|
+
* @Scoped(Scope.Container)
|
559
|
+
* class Wizard {}
|
560
|
+
*
|
561
|
+
* container.register(Wizard);
|
562
|
+
*
|
563
|
+
* // Under the hood
|
564
|
+
* container.register(
|
565
|
+
* Wizard,
|
566
|
+
* { useClass: Wizard },
|
567
|
+
* { scope: Scope.Container },
|
568
|
+
* );
|
569
|
+
* ```
|
570
|
+
*
|
571
|
+
* @__NO_SIDE_EFFECTS__
|
572
|
+
*/
|
573
|
+
declare function Scoped(scope: Scope): ClassDecorator;
|
574
|
+
|
575
|
+
/**
|
576
|
+
* Injects the instance associated with the given class.
|
577
|
+
*
|
578
|
+
* Throws an error if the class is not registered in the container.
|
579
|
+
*/
|
580
|
+
declare function inject<Instance extends object>(Class: Constructor<Instance>): Instance;
|
581
|
+
/**
|
582
|
+
* Injects the value associated with the given token.
|
583
|
+
*
|
584
|
+
* Throws an error if the token is not registered in the container.
|
585
|
+
*/
|
586
|
+
declare function inject<Value>(token: Token<Value>): Value;
|
587
|
+
/**
|
588
|
+
* Injects the instance associated with the given class.
|
589
|
+
*
|
590
|
+
* Throws an error if the class is not registered in the container.
|
591
|
+
*
|
592
|
+
* Compared to {@link inject}, `injectBy` accepts a `thisArg` argument
|
593
|
+
* (the containing class) which is used to resolve circular dependencies.
|
594
|
+
*
|
595
|
+
* @example
|
596
|
+
* ```ts
|
597
|
+
* class Wand {
|
598
|
+
* owner = inject(Wizard);
|
599
|
+
* }
|
600
|
+
*
|
601
|
+
* class Wizard {
|
602
|
+
* wand = injectBy(this, Wand);
|
603
|
+
* }
|
604
|
+
* ```
|
605
|
+
*
|
606
|
+
* @param thisArg - The containing instance, used to help resolve circular dependencies.
|
607
|
+
* @param Class - The class to resolve.
|
608
|
+
*/
|
609
|
+
declare function injectBy<Instance extends object>(thisArg: any, Class: Constructor<Instance>): Instance;
|
610
|
+
/**
|
611
|
+
* Injects the value associated with the given token.
|
612
|
+
*
|
613
|
+
* Throws an error if the token is not registered in the container.
|
614
|
+
*
|
615
|
+
* Compared to {@link inject}, `injectBy` accepts a `thisArg` argument
|
616
|
+
* (the containing class) which is used to resolve circular dependencies.
|
617
|
+
*
|
618
|
+
* @example
|
619
|
+
* ```ts
|
620
|
+
* class Wand {
|
621
|
+
* owner = inject(Wizard);
|
622
|
+
* }
|
623
|
+
*
|
624
|
+
* class Wizard {
|
625
|
+
* wand = injectBy(this, Wand);
|
626
|
+
* }
|
627
|
+
* ```
|
628
|
+
*
|
629
|
+
* @param thisArg - The containing instance, used to help resolve circular dependencies.
|
630
|
+
* @param token - The token to resolve.
|
631
|
+
*/
|
632
|
+
declare function injectBy<Value>(thisArg: any, token: Token<Value>): Value;
|
633
|
+
|
634
|
+
/**
|
635
|
+
* Injects all instances provided by the registrations associated with the given class.
|
636
|
+
*
|
637
|
+
* Throws an error if the class is not registered in the container.
|
638
|
+
*/
|
639
|
+
declare function injectAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
640
|
+
/**
|
641
|
+
* Injects all values provided by the registrations associated with the given token.
|
642
|
+
*
|
643
|
+
* Throws an error if the token is not registered in the container.
|
644
|
+
*/
|
645
|
+
declare function injectAll<Value>(token: Token<Value>): NonNullable<Value>[];
|
646
|
+
|
647
|
+
/**
|
648
|
+
* Injector API.
|
649
|
+
*/
|
650
|
+
interface Injector {
|
651
|
+
/**
|
652
|
+
* Injects the instance associated with the given class.
|
653
|
+
*
|
654
|
+
* Throws an error if the class is not registered in the container.
|
655
|
+
*/
|
656
|
+
inject<Instance extends object>(Class: Constructor<Instance>): Instance;
|
657
|
+
/**
|
658
|
+
* Injects the value associated with the given token.
|
659
|
+
*
|
660
|
+
* Throws an error if the token is not registered in the container.
|
661
|
+
*/
|
662
|
+
inject<Value>(token: Token<Value>): Value;
|
663
|
+
/**
|
664
|
+
* Injects all instances provided by the registrations associated with the given class.
|
665
|
+
*
|
666
|
+
* Throws an error if the class is not registered in the container.
|
667
|
+
*/
|
668
|
+
injectAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
669
|
+
/**
|
670
|
+
* Injects all values provided by the registrations associated with the given token.
|
671
|
+
*
|
672
|
+
* Throws an error if the token is not registered in the container.
|
673
|
+
*/
|
674
|
+
injectAll<Value>(token: Token<Value>): NonNullable<Value>[];
|
675
|
+
/**
|
676
|
+
* Injects the instance associated with the given class,
|
677
|
+
* or `undefined` if the class is not registered in the container.
|
678
|
+
*/
|
679
|
+
optional<Instance extends object>(Class: Constructor<Instance>): Instance | undefined;
|
680
|
+
/**
|
681
|
+
* Injects the value associated with the given token,
|
682
|
+
* or `undefined` if the token is not registered in the container.
|
683
|
+
*/
|
684
|
+
optional<Value>(token: Token<Value>): Value | undefined;
|
685
|
+
/**
|
686
|
+
* Injects all instances provided by the registrations associated with the given class,
|
687
|
+
* or an empty array if the class is not registered in the container.
|
688
|
+
*/
|
689
|
+
optionalAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
690
|
+
/**
|
691
|
+
* Injects all values provided by the registrations associated with the given token,
|
692
|
+
* or an empty array if the token is not registered in the container.
|
693
|
+
*/
|
694
|
+
optionalAll<Value>(token: Token<Value>): NonNullable<Value>[];
|
695
|
+
}
|
696
|
+
/**
|
697
|
+
* Injector token for dynamic injections.
|
698
|
+
*
|
699
|
+
* @example
|
700
|
+
* ```ts
|
701
|
+
* class Wizard {
|
702
|
+
* private injector = inject(Injector);
|
703
|
+
* private wand?: Wand;
|
704
|
+
*
|
705
|
+
* getWand(): Wand {
|
706
|
+
* return (this.wand ??= this.injector.inject(Wand));
|
707
|
+
* }
|
708
|
+
* }
|
709
|
+
*
|
710
|
+
* const wizard = container.resolve(Wizard);
|
711
|
+
* wizard.getWand(); // => Wand
|
712
|
+
* ```
|
713
|
+
*/
|
714
|
+
declare const Injector: Type<Injector>;
|
715
|
+
|
716
|
+
/**
|
717
|
+
* Composer API for middleware functions.
|
718
|
+
*/
|
719
|
+
interface MiddlewareComposer {
|
720
|
+
/**
|
721
|
+
* Add a middleware function to the composer.
|
722
|
+
*/
|
723
|
+
use<MethodKey extends keyof Container>(key: MethodKey, wrap: Container[MethodKey] extends Function ? (next: Container[MethodKey]) => Container[MethodKey] : never): MiddlewareComposer;
|
724
|
+
}
|
725
|
+
/**
|
726
|
+
* Middleware function that can be used to extend the container.
|
727
|
+
*
|
728
|
+
* @example
|
729
|
+
* ```ts
|
730
|
+
* const logger: Middleware = (composer, _api) => {
|
731
|
+
* composer
|
732
|
+
* .use("resolve", (next) => (...args) => {
|
733
|
+
* console.log("resolve", args);
|
734
|
+
* return next(...args);
|
735
|
+
* })
|
736
|
+
* .use("resolveAll", (next) => (...args) => {
|
737
|
+
* console.log("resolveAll", args);
|
738
|
+
* return next(...args);
|
739
|
+
* });
|
740
|
+
* };
|
741
|
+
* ```
|
742
|
+
*/
|
743
|
+
interface Middleware {
|
744
|
+
(composer: MiddlewareComposer, api: Readonly<Container>): void;
|
745
|
+
}
|
746
|
+
/**
|
747
|
+
* Apply middleware functions to a container.
|
748
|
+
*
|
749
|
+
* Middlewares are applied in array order, but execute in reverse order.
|
750
|
+
*
|
751
|
+
* @example
|
752
|
+
* ```ts
|
753
|
+
* const container = applyMiddleware(
|
754
|
+
* createContainer(),
|
755
|
+
* [A, B],
|
756
|
+
* );
|
757
|
+
* ```
|
758
|
+
*
|
759
|
+
* The execution order will be:
|
760
|
+
*
|
761
|
+
* 1. B before
|
762
|
+
* 2. A before
|
763
|
+
* 3. original function
|
764
|
+
* 4. A after
|
765
|
+
* 5. B after
|
766
|
+
*
|
767
|
+
* This allows outer middlewares to wrap and control the behavior of inner middlewares.
|
768
|
+
*/
|
769
|
+
declare function applyMiddleware(container: Container, middlewares: Middleware[]): Container;
|
770
|
+
|
771
|
+
export { AutoRegister, Build, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, Type, applyMiddleware, createContainer, forwardRef, inject, injectAll, injectBy };
|
772
|
+
export type { ClassProvider, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, ValueProvider };
|