@lppedd/di-wise-neo 0.11.1 → 0.12.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/README.md +42 -39
- package/dist/cjs/index.d.ts +138 -44
- package/dist/cjs/index.js +66 -50
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.mts +138 -44
- package/dist/es/index.mjs +66 -50
- package/dist/es/index.mjs.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
@@ -3,17 +3,17 @@
|
|
3
3
|
<p align="center">Lightweight, type-safe, flexible dependency injection library for TypeScript and JavaScript</p>
|
4
4
|
<div align="center">
|
5
5
|
|
6
|
+
[](https://www.npmjs.com/package/@lppedd/di-wise-neo)
|
7
|
+
[](https://en.wikipedia.org/wiki/ECMAScript_version_history#13th_edition_%E2%80%93_ECMAScript_2022)
|
8
|
+
[](https://github.com/lppedd/di-wise-neo/blob/main/CHANGELOG.md#0100)
|
6
9
|
[](https://github.com/lppedd/di-wise-neo/actions/workflows/test.yml)
|
7
10
|
[](https://app.codecov.io/gh/lppedd/di-wise-neo/tree/main/src)
|
8
|
-
[](https://www.npmjs.com/package/@lppedd/di-wise-neo)
|
9
|
-
[](https://bundlejs.com/?q=@lppedd/di-wise-neo)
|
10
11
|
[](https://github.com/lppedd/di-wise-neo/blob/main/LICENSE)
|
11
12
|
|
12
13
|
</div>
|
13
14
|
<img align="center" src="./.github/images/neo-wall.jpg" alt="di-wise-neo" style="border: 3px solid black; border-radius: 15px;" />
|
14
15
|
|
15
16
|
> [!NOTE]
|
16
|
-
>
|
17
17
|
> **di-wise-neo** is a fork of [di-wise][di-wise], aiming to provide a simpler yet more powerful API,
|
18
18
|
> in part thanks to TypeScript's experimental decorators. Shout out to [@exuanbo](https://github.com/exuanbo)
|
19
19
|
> for the strong foundations!
|
@@ -42,7 +42,7 @@ Part of the problem is the crazy amount of parameter passing, and the many expor
|
|
42
42
|
global values floating around waiting to be imported and to generate yet another
|
43
43
|
coupling point.
|
44
44
|
|
45
|
-
My background with Java is full of such cases
|
45
|
+
My background with Java is full of such cases that have been (partially) mitigated
|
46
46
|
by introducing dependency-injection libraries based on Java's powerful Contexts and
|
47
47
|
Dependency Injection (see [Weld][cdi-weld], the reference implementation).
|
48
48
|
|
@@ -53,24 +53,23 @@ I've also explored on my own and discovered [redi][redi] and [di-wise][di-wise].
|
|
53
53
|
|
54
54
|
What I was looking for is a lightweight solution that offers:
|
55
55
|
|
56
|
-
-
|
57
|
-
-
|
58
|
-
-
|
59
|
-
Yes I know, forget type-safety with decorators, but they are extremely
|
60
|
-
|
61
|
-
|
62
|
-
and ESBuild [does not][esbuild-issue] support `emitDecoratorMetadata`
|
56
|
+
- Full type safety.
|
57
|
+
- Scoped resolution of dependencies.
|
58
|
+
- Optional decorator support for constructor and method injection.
|
59
|
+
Yes, I know, forget type-safety with decorators, but they are extremely intuitive to pick up for Java devs.
|
60
|
+
- No dependency on [reflect-metadata][reflect-metadata], as I'm an ESBuild user and ESBuild
|
61
|
+
[does not][esbuild-issue] support `emitDecoratorMetadata`.
|
63
62
|
|
64
|
-
Unfortunately both [tsyringe][tsyringe] and [InversifyJS][InversifyJS] require
|
63
|
+
Unfortunately, both [tsyringe][tsyringe] and [InversifyJS][InversifyJS] require
|
65
64
|
[reflect-metadata][reflect-metadata] to run correctly. [Awilix][Awilix] looks good,
|
66
65
|
but it's probably too much for what I need to do, and it does not support decorators.
|
67
66
|
Plus, the API just didn't click for me.
|
68
67
|
|
69
68
|
[redi][redi] focuses _only_ on constructor injection via decorators, which is nice.
|
70
69
|
However, it falls short when it comes to type safety and resolution scopes:
|
71
|
-
it only supports singletons
|
70
|
+
it only supports singletons with a decorator-based trick to create fresh instances.
|
72
71
|
|
73
|
-
And lastly, [di-wise][di-wise]. This small library was quite
|
72
|
+
And lastly, [di-wise][di-wise]. This small library was quite a surprise! Easy to pick up,
|
74
73
|
no scope creep, injection context support, and full type safety via Angular-like
|
75
74
|
`inject<T>()` functions (that's more like a service locator, but whatever).
|
76
75
|
The only problems are the slightly overcomplicated API - especially regarding typings - and
|
@@ -95,18 +94,21 @@ yarn add @lppedd/di-wise-neo
|
|
95
94
|
|
96
95
|
### API reference
|
97
96
|
|
98
|
-
You can find the complete API reference at [lppedd.github.io/di-wise-neo]
|
97
|
+
You can find the complete API reference at [lppedd.github.io/di-wise-neo][di-wise-neo].
|
99
98
|
|
100
99
|
### Ergonomics
|
101
100
|
|
102
|
-
- Does **not** depend on other libraries
|
103
|
-
- Does **not** use [reflect-metadata]
|
104
|
-
- **Can** be used from JavaScript with function-based injection
|
101
|
+
- Does **not** depend on other libraries.
|
102
|
+
- Does **not** use [reflect-metadata][reflect-metadata] to drive decorators.
|
103
|
+
- **Can** be used from JavaScript with function-based injection.
|
105
104
|
|
106
105
|
### Requirements
|
107
106
|
|
108
|
-
-
|
109
|
-
|
107
|
+
- The JS environment must support features such as `Array.at`, `Array.flat`, `WeakRef`, `WeakMap`, `Set`, `Map`.
|
108
|
+
|
109
|
+
#### Decorator-based injection
|
110
|
+
|
111
|
+
- When using decorator-based injection, `experimentalDecorators` must be enabled in your `tsconfig.json` file.
|
110
112
|
|
111
113
|
## Quickstart
|
112
114
|
|
@@ -175,9 +177,9 @@ values are cached and reused.
|
|
175
177
|
|
176
178
|
Creates a new value every time the dependency is resolved, which means values are never cached.
|
177
179
|
|
178
|
-
-
|
179
|
-
-
|
180
|
-
-
|
180
|
+
- A class registered via `ClassProvider` is instantiated on each resolution
|
181
|
+
- A factory function registered via `FactoryProvider` is invoked on each resolution
|
182
|
+
- A value registered via `ValueProvider` is always returned as-is
|
181
183
|
|
182
184
|
> [!NOTE]
|
183
185
|
> When a **Transient** or **Resolution**-scoped value is injected into a **Container**-scoped
|
@@ -187,8 +189,7 @@ Creates a new value every time the dependency is resolved, which means values ar
|
|
187
189
|
### Resolution
|
188
190
|
|
189
191
|
Creates and caches a single value per resolution graph.
|
190
|
-
The same value is reused during a single resolution request, but a new one is created
|
191
|
-
for each separate request.
|
192
|
+
The same value is reused during a single resolution request, but a new one is created for each request.
|
192
193
|
|
193
194
|
### Container
|
194
195
|
|
@@ -196,7 +197,7 @@ Creates and caches a single value per container.
|
|
196
197
|
If the value is not found in the current container, it is looked up in the parent container,
|
197
198
|
and so on.
|
198
199
|
|
199
|
-
It effectively behaves like a **singleton** scope
|
200
|
+
It effectively behaves like a **singleton** scope but allows container-specific overrides.
|
200
201
|
|
201
202
|
## Token registration
|
202
203
|
|
@@ -291,9 +292,10 @@ All injection functions must be invoked inside an _injection context_, which sto
|
|
291
292
|
the currently active container.
|
292
293
|
The _injection context_ is available in these situations:
|
293
294
|
|
294
|
-
-
|
295
|
-
-
|
296
|
-
-
|
295
|
+
- Inside the `constructor` of a class instantiated by the DI container.
|
296
|
+
- In property initializers of such classes.
|
297
|
+
- Within factory functions used by `FactoryProvider`.
|
298
|
+
- Within functions passed to `Injector.runInContext`.
|
297
299
|
|
298
300
|
### `inject<T>(Token): T`
|
299
301
|
|
@@ -342,7 +344,7 @@ export class ProcessManager {
|
|
342
344
|
|
343
345
|
### `optionalAll<T>(Token): T[]`
|
344
346
|
|
345
|
-
Injects all values associated with a token
|
347
|
+
Injects all values associated with a token or an **empty array** if the token
|
346
348
|
has never been registered in the container.
|
347
349
|
|
348
350
|
```ts
|
@@ -356,10 +358,11 @@ export class ExtensionContext {
|
|
356
358
|
|
357
359
|
## Decorator-based injection
|
358
360
|
|
359
|
-
You can also perform dependency injection using TypeScript's experimental decorators.
|
361
|
+
You can also perform dependency injection using TypeScript's experimental decorators.
|
362
|
+
**di-wise-neo** supports decorating constructor and instance method parameters.
|
360
363
|
|
361
|
-
|
362
|
-
|
364
|
+
> [!NOTE]
|
365
|
+
> Property injection is **not** supported by design.
|
363
366
|
|
364
367
|
### `@Inject(Token)`
|
365
368
|
|
@@ -413,7 +416,7 @@ export class ProcessManager {
|
|
413
416
|
|
414
417
|
### `@OptionalAll(Token)`
|
415
418
|
|
416
|
-
Injects all values associated with a token
|
419
|
+
Injects all values associated with a token or an **empty array** if the token
|
417
420
|
has never been registered in the container.
|
418
421
|
|
419
422
|
```ts
|
@@ -427,7 +430,7 @@ export class ExtensionContext {
|
|
427
430
|
|
428
431
|
### Forward references
|
429
432
|
|
430
|
-
Sometimes you may need to reference a token or class
|
433
|
+
Sometimes you may need to reference a token or class declared later in the file.
|
431
434
|
Normally, attempting to do that would result in a `ReferenceError`:
|
432
435
|
|
433
436
|
> ReferenceError: Cannot access 'IStore' before initialization
|
@@ -501,8 +504,8 @@ The container will throw an error at registration time if the name is already ta
|
|
501
504
|
|
502
505
|
### `@AutoRegister`
|
503
506
|
|
504
|
-
Enables automatic registration of the decorated class when it is resolved
|
505
|
-
|
507
|
+
Enables automatic registration of the decorated class when it is resolved if it has not
|
508
|
+
been registered beforehand.
|
506
509
|
|
507
510
|
```ts
|
508
511
|
@AutoRegister()
|
@@ -516,8 +519,7 @@ container.resolve(ExtensionContext);
|
|
516
519
|
|
517
520
|
### `@EagerInstantiate`
|
518
521
|
|
519
|
-
Sets the default class scope to **Container** and marks the class for eager instantiation
|
520
|
-
upon registration.
|
522
|
+
Sets the default class scope to **Container** and marks the class for eager instantiation upon registration.
|
521
523
|
|
522
524
|
This causes the container to immediately create and cache the instance of the class
|
523
525
|
at registration time, instead of deferring instantiation until the first resolution.
|
@@ -596,6 +598,7 @@ All credits to the original author for focusing on a clean architecture and on c
|
|
596
598
|
[InversifyJS]: https://github.com/inversify/InversifyJS
|
597
599
|
[redi]: https://github.com/wzhudev/redi
|
598
600
|
[di-wise]: https://github.com/exuanbo/di-wise
|
601
|
+
[di-wise-neo]: https://lppedd.github.io/di-wise-neo
|
599
602
|
[reflect-metadata]: https://github.com/microsoft/reflect-metadata
|
600
603
|
[esbuild-issue]: https://github.com/evanw/esbuild/issues/257
|
601
604
|
[source-container]: https://github.com/lppedd/di-wise-neo/blob/main/src/container.ts#L29
|
package/dist/cjs/index.d.ts
CHANGED
@@ -145,7 +145,19 @@ interface ValueProvider<Value> {
|
|
145
145
|
*/
|
146
146
|
interface ExistingProvider<Value> {
|
147
147
|
/**
|
148
|
-
* The existing token to alias.
|
148
|
+
* The existing token to alias, with an optional name qualifier.
|
149
|
+
*
|
150
|
+
* @example
|
151
|
+
* ```ts
|
152
|
+
* container.register(ISecretStorage, {
|
153
|
+
* useExisting: PersistentSecretStorage,
|
154
|
+
* });
|
155
|
+
*
|
156
|
+
* // Or in case we need to alias a name-qualified token
|
157
|
+
* container.register(ISecretStorage, {
|
158
|
+
* useExisting: [PersistentSecretStorage, "fileSystem"],
|
159
|
+
* });
|
160
|
+
* ```
|
149
161
|
*/
|
150
162
|
readonly useExisting: Token<Value> | [Token<Value>, string?];
|
151
163
|
/**
|
@@ -171,8 +183,22 @@ interface ExistingProvider<Value> {
|
|
171
183
|
type Provider<Value = any> = ClassProvider<Value & object> | FactoryProvider<Value> | ValueProvider<Value> | ExistingProvider<Value>;
|
172
184
|
|
173
185
|
declare const Scope: {
|
186
|
+
/**
|
187
|
+
* Creates a new value every time the token is resolved.
|
188
|
+
*/
|
174
189
|
readonly Transient: "Transient";
|
190
|
+
/**
|
191
|
+
* Creates and caches a single value per token resolution graph.
|
192
|
+
*
|
193
|
+
* The same value is reused during a single resolution request and is subsequently discarded.
|
194
|
+
*/
|
175
195
|
readonly Resolution: "Resolution";
|
196
|
+
/**
|
197
|
+
* Creates and caches a single value per container.
|
198
|
+
*
|
199
|
+
* If the value is not found in the current container, it is looked up in the parent container,
|
200
|
+
* and so on. It effectively behaves like a _singleton_ scope but allows container-specific overrides.
|
201
|
+
*/
|
176
202
|
readonly Container: "Container";
|
177
203
|
};
|
178
204
|
type Scope = (typeof Scope)[keyof typeof Scope];
|
@@ -345,9 +371,8 @@ interface Container {
|
|
345
371
|
* The scope for the automatic registration is determined by either
|
346
372
|
* the {@link Scoped} decorator on the class, or {@link ContainerOptions.defaultScope}.
|
347
373
|
*
|
348
|
-
* If
|
349
|
-
*
|
350
|
-
* Otherwise, if `optional` is true, `undefined` is returned.
|
374
|
+
* If the class is not registered in this container or any of its parent containers
|
375
|
+
* and could not be auto-registered, an error is thrown.
|
351
376
|
*
|
352
377
|
* The resolution behavior depends on the {@link Provider} used during registration:
|
353
378
|
* - For {@link ValueProvider}, the explicitly provided instance is returned.
|
@@ -359,17 +384,13 @@ interface Container {
|
|
359
384
|
* in the container's internal registry.
|
360
385
|
*/
|
361
386
|
resolve<Instance extends object>(Class: Constructor<Instance>, name?: string): Instance;
|
362
|
-
resolve<Instance extends object>(Class: Constructor<Instance>, optional?: false, name?: string): Instance;
|
363
|
-
resolve<Instance extends object>(Class: Constructor<Instance>, optional: true, name?: string): Instance | undefined;
|
364
|
-
resolve<Instance extends object>(Class: Constructor<Instance>, optional?: boolean, name?: string): Instance | undefined;
|
365
387
|
/**
|
366
388
|
* Resolves the given token to the value associated with it.
|
367
389
|
*
|
368
390
|
* If the token is registered in this container or any of its parent containers,
|
369
391
|
* its value is resolved using the most recent registration.
|
370
392
|
*
|
371
|
-
* If
|
372
|
-
* an error is thrown. Otherwise, if `optional` is true, `undefined` is returned.
|
393
|
+
* If the token is not registered in this container or any of its parent containers, an error is thrown.
|
373
394
|
*
|
374
395
|
* The resolution behavior depends on the {@link Provider} used during registration:
|
375
396
|
* - For {@link ValueProvider}, the explicitly provided value is returned.
|
@@ -381,9 +402,51 @@ interface Container {
|
|
381
402
|
* in the container's internal registry.
|
382
403
|
*/
|
383
404
|
resolve<Value>(token: Token<Value>, name?: string): Value;
|
384
|
-
|
385
|
-
|
386
|
-
|
405
|
+
/**
|
406
|
+
* Resolves the given class to the instance associated with it.
|
407
|
+
*
|
408
|
+
* If the class is registered in this container or any of its parent containers,
|
409
|
+
* an instance is created using the most recent registration.
|
410
|
+
*
|
411
|
+
* If the class is not registered, but it is decorated with {@link AutoRegister},
|
412
|
+
* or {@link ContainerOptions.autoRegister} is true, the class is registered automatically.
|
413
|
+
* Otherwise, the resolution fails.
|
414
|
+
*
|
415
|
+
* The scope for the automatic registration is determined by either
|
416
|
+
* the {@link Scoped} decorator on the class, or {@link ContainerOptions.defaultScope}.
|
417
|
+
*
|
418
|
+
* If the class is not registered in this container or any of its parent containers
|
419
|
+
* and could not be auto-registered, `undefined` is returned instead.
|
420
|
+
*
|
421
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
422
|
+
* - For {@link ValueProvider}, the explicitly provided instance is returned.
|
423
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the instance.
|
424
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
425
|
+
* - For {@link ExistingProvider}, the instance is resolved by referring to another registered token.
|
426
|
+
*
|
427
|
+
* If the class is registered with _container_ scope, the resolved instance is cached
|
428
|
+
* in the container's internal registry.
|
429
|
+
*/
|
430
|
+
tryResolve<Instance extends object>(Class: Constructor<Instance>, name?: string): Instance | undefined;
|
431
|
+
/**
|
432
|
+
* Tries to resolve the given token to the value associated with it.
|
433
|
+
*
|
434
|
+
* If the token is registered in this container or any of its parent containers,
|
435
|
+
* its value is resolved using the most recent registration.
|
436
|
+
*
|
437
|
+
* If the token is not registered in this container or any of its parent containers,
|
438
|
+
* `undefined` is returned instead.
|
439
|
+
*
|
440
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
441
|
+
* - For {@link ValueProvider}, the explicitly provided value is returned.
|
442
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the value.
|
443
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
444
|
+
* - For {@link ExistingProvider}, the value is resolved by referring to another registered token.
|
445
|
+
*
|
446
|
+
* If the token is registered with _container_ scope, the resolved value is cached
|
447
|
+
* in the container's internal registry.
|
448
|
+
*/
|
449
|
+
tryResolve<Value>(token: Token<Value>, name?: string): Value | undefined;
|
387
450
|
/**
|
388
451
|
* Resolves the given class to all instances provided by the registrations associated with it.
|
389
452
|
*
|
@@ -394,9 +457,8 @@ interface Container {
|
|
394
457
|
* The scope for the automatic registration is determined by either
|
395
458
|
* the {@link Scoped} decorator on the class, or {@link ContainerOptions.defaultScope}.
|
396
459
|
*
|
397
|
-
* If
|
398
|
-
*
|
399
|
-
* Otherwise, if `optional` is true, an empty array is returned.
|
460
|
+
* If the class is not registered in this container or any of its parent containers
|
461
|
+
* and could not be auto-registered, an error is thrown.
|
400
462
|
*
|
401
463
|
* The resolution behavior depends on the {@link Provider} used during registration:
|
402
464
|
* - For {@link ValueProvider}, the explicitly provided instance is returned.
|
@@ -408,17 +470,12 @@ interface Container {
|
|
408
470
|
* in the container's internal registry.
|
409
471
|
*
|
410
472
|
* A separate instance of the class is created for each provider.
|
411
|
-
*
|
412
|
-
* @see The documentation for `resolve(Class: Constructor)`
|
413
473
|
*/
|
414
|
-
resolveAll<Instance extends object>(Class: Constructor<Instance
|
415
|
-
resolveAll<Instance extends object>(Class: Constructor<Instance>, optional: true): Instance[];
|
416
|
-
resolveAll<Instance extends object>(Class: Constructor<Instance>, optional?: boolean): Instance[];
|
474
|
+
resolveAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
417
475
|
/**
|
418
476
|
* Resolves the given token to all values provided by the registrations associated with it.
|
419
477
|
*
|
420
|
-
* If
|
421
|
-
* an error is thrown. Otherwise, if `optional` is true, an empty array is returned.
|
478
|
+
* If the token is not registered in this container or any of its parent containers, an error is thrown.
|
422
479
|
*
|
423
480
|
* The resolution behavior depends on the {@link Provider} used during registration:
|
424
481
|
* - For {@link ValueProvider}, the explicitly provided value is returned.
|
@@ -428,12 +485,49 @@ interface Container {
|
|
428
485
|
*
|
429
486
|
* If the token is registered with _container_ scope, the resolved values are cached
|
430
487
|
* in the container's internal registry.
|
488
|
+
*/
|
489
|
+
resolveAll<Value>(token: Token<Value>): Value[];
|
490
|
+
/**
|
491
|
+
* Resolves the given class to all instances provided by the registrations associated with it.
|
492
|
+
*
|
493
|
+
* If the class is not registered, but it is decorated with {@link AutoRegister},
|
494
|
+
* or {@link ContainerOptions.autoRegister} is true, the class is registered automatically.
|
495
|
+
* Otherwise, the resolution fails.
|
431
496
|
*
|
432
|
-
*
|
497
|
+
* The scope for the automatic registration is determined by either
|
498
|
+
* the {@link Scoped} decorator on the class, or {@link ContainerOptions.defaultScope}.
|
499
|
+
*
|
500
|
+
* If the class is not registered in this container or any of its parent containers
|
501
|
+
* and could not be auto-registered, an empty array is returned instead.
|
502
|
+
*
|
503
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
504
|
+
* - For {@link ValueProvider}, the explicitly provided instance is returned.
|
505
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the instance.
|
506
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
507
|
+
* - For {@link ExistingProvider}, the instance is resolved by referring to another registered token.
|
508
|
+
*
|
509
|
+
* If the class is registered with _container_ scope, the resolved instances are cached
|
510
|
+
* in the container's internal registry.
|
511
|
+
*
|
512
|
+
* A separate instance of the class is created for each provider.
|
513
|
+
*/
|
514
|
+
tryResolveAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
515
|
+
/**
|
516
|
+
* Resolves the given token to all values provided by the registrations associated with it.
|
517
|
+
*
|
518
|
+
* If the token is not registered in this container or any of its parent containers,
|
519
|
+
* an empty array is returned instead.
|
520
|
+
*
|
521
|
+
* The resolution behavior depends on the {@link Provider} used during registration:
|
522
|
+
* - For {@link ValueProvider}, the explicitly provided value is returned.
|
523
|
+
* - For {@link FactoryProvider}, the factory function is invoked to create the value.
|
524
|
+
* - For {@link ClassProvider}, a new instance of the class is created according to its scope.
|
525
|
+
* - For {@link ExistingProvider}, the value is resolved by referring to another registered token.
|
526
|
+
*
|
527
|
+
* If the token is registered with _container_ scope, the resolved values are cached
|
528
|
+
* in the container's internal registry.
|
433
529
|
*/
|
434
|
-
|
435
|
-
resolveAll<Value>(token: Token<Value>, optional: true): NonNullable<Value>[];
|
436
|
-
resolveAll<Value>(token: Token<Value>, optional?: boolean): NonNullable<Value>[];
|
530
|
+
tryResolveAll<Value>(token: Token<Value>): Value[];
|
437
531
|
/**
|
438
532
|
* Disposes this container and all its cached values.
|
439
533
|
*
|
@@ -450,7 +544,7 @@ interface Container {
|
|
450
544
|
declare function createContainer(options?: Partial<ContainerOptions>): Container;
|
451
545
|
|
452
546
|
/**
|
453
|
-
* Class decorator that enables auto-registration of an unregistered class
|
547
|
+
* Class decorator that enables auto-registration of an unregistered class
|
454
548
|
* when the class is first resolved from the container.
|
455
549
|
*
|
456
550
|
* @example
|
@@ -536,7 +630,7 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
|
|
536
630
|
declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
537
631
|
|
538
632
|
/**
|
539
|
-
* Class decorator that registers additional aliasing tokens for the decorated type
|
633
|
+
* Class decorator that registers additional aliasing tokens for the decorated type
|
540
634
|
* when the type is first registered in the container.
|
541
635
|
*
|
542
636
|
* The container uses {@link ExistingProvider} under the hood.
|
@@ -549,7 +643,7 @@ declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
|
549
643
|
*/
|
550
644
|
declare function Injectable<This extends object, Value extends This>(...tokens: Tokens<Value>): ClassDecorator;
|
551
645
|
/**
|
552
|
-
* Class decorator that registers additional aliasing tokens for the decorated type
|
646
|
+
* Class decorator that registers additional aliasing tokens for the decorated type
|
553
647
|
* when the type is first registered in the container.
|
554
648
|
*
|
555
649
|
* The container uses {@link ExistingProvider} under the hood.
|
@@ -651,19 +745,19 @@ declare function Optional<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
|
651
745
|
|
652
746
|
/**
|
653
747
|
* Parameter decorator that injects all instances provided by the registrations
|
654
|
-
* associated with the given class
|
748
|
+
* associated with the given class or an empty array if the class is not registered
|
655
749
|
* in the container.
|
656
750
|
*/
|
657
751
|
declare function OptionalAll<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
|
658
752
|
/**
|
659
753
|
* Parameter decorator that injects all values provided by the registrations
|
660
|
-
* associated with the given token
|
754
|
+
* associated with the given token or an empty array if the token is not registered
|
661
755
|
* in the container.
|
662
756
|
*/
|
663
757
|
declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
|
664
758
|
/**
|
665
759
|
* Parameter decorator that injects all values provided by the registrations
|
666
|
-
* associated with the given token
|
760
|
+
* associated with the given token or an empty array if the token is not registered
|
667
761
|
* in the container.
|
668
762
|
*
|
669
763
|
* Allows referencing a token declared later in the file by using the
|
@@ -776,7 +870,7 @@ declare function injectAll<Instance extends object>(Class: Constructor<Instance>
|
|
776
870
|
*
|
777
871
|
* Throws an error if the token is not registered in the container.
|
778
872
|
*/
|
779
|
-
declare function injectAll<Value>(token: Token<Value>):
|
873
|
+
declare function injectAll<Value>(token: Token<Value>): Value[];
|
780
874
|
|
781
875
|
/**
|
782
876
|
* Injector API.
|
@@ -805,7 +899,7 @@ interface Injector {
|
|
805
899
|
*
|
806
900
|
* Throws an error if the token is not registered in the container.
|
807
901
|
*/
|
808
|
-
injectAll<Value>(token: Token<Value>):
|
902
|
+
injectAll<Value>(token: Token<Value>): Value[];
|
809
903
|
/**
|
810
904
|
* Injects the instance associated with the given class,
|
811
905
|
* or `undefined` if the class is not registered in the container.
|
@@ -817,15 +911,15 @@ interface Injector {
|
|
817
911
|
*/
|
818
912
|
optional<Value>(token: Token<Value>, name?: string): Value | undefined;
|
819
913
|
/**
|
820
|
-
* Injects all instances provided by the registrations associated with the given class
|
914
|
+
* Injects all instances provided by the registrations associated with the given class
|
821
915
|
* or an empty array if the class is not registered in the container.
|
822
916
|
*/
|
823
917
|
optionalAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
824
918
|
/**
|
825
|
-
* Injects all values provided by the registrations associated with the given token
|
919
|
+
* Injects all values provided by the registrations associated with the given token
|
826
920
|
* or an empty array if the token is not registered in the container.
|
827
921
|
*/
|
828
|
-
optionalAll<Value>(token: Token<Value>):
|
922
|
+
optionalAll<Value>(token: Token<Value>): Value[];
|
829
923
|
/**
|
830
924
|
* Runs a function inside the injection context of this injector.
|
831
925
|
*
|
@@ -833,7 +927,7 @@ interface Injector {
|
|
833
927
|
* are only usable synchronously: they cannot be called from asynchronous callbacks
|
834
928
|
* or after any `await` points.
|
835
929
|
*
|
836
|
-
* @param fn The function to be run in the context of this injector.
|
930
|
+
* @param fn - The function to be run in the context of this injector.
|
837
931
|
* @returns The return value of the function, if any.
|
838
932
|
*/
|
839
933
|
runInContext<ReturnType>(fn: () => ReturnType): ReturnType;
|
@@ -865,8 +959,8 @@ declare const Injector: Type<Injector>;
|
|
865
959
|
* This allows libraries or consumers that manipulate constructors, such as through
|
866
960
|
* class decorators, to inform the DI system about the real "identity" of a class.
|
867
961
|
*
|
868
|
-
* @param transformedClass The constructor function returned by a class decorator or factory.
|
869
|
-
* @param originalClass The original constructor function.
|
962
|
+
* @param transformedClass - The constructor function returned by a class decorator or factory.
|
963
|
+
* @param originalClass - The original constructor function.
|
870
964
|
*
|
871
965
|
* @remarks
|
872
966
|
* This API affects the core class identity resolution mechanism of the DI system.
|
@@ -909,7 +1003,7 @@ interface Middleware {
|
|
909
1003
|
/**
|
910
1004
|
* Applies middleware functions to a container.
|
911
1005
|
*
|
912
|
-
* Middlewares are applied in array order
|
1006
|
+
* Middlewares are applied in array order but execute in reverse order.
|
913
1007
|
*
|
914
1008
|
* @example
|
915
1009
|
* ```ts
|
@@ -967,15 +1061,15 @@ declare function optionalBy<Instance extends object>(thisArg: any, Class: Constr
|
|
967
1061
|
declare function optionalBy<Value>(thisArg: any, token: Token<Value>, name?: string): Value | undefined;
|
968
1062
|
|
969
1063
|
/**
|
970
|
-
* Injects all instances provided by the registrations associated with the given class
|
1064
|
+
* Injects all instances provided by the registrations associated with the given class
|
971
1065
|
* or an empty array if the class is not registered in the container.
|
972
1066
|
*/
|
973
1067
|
declare function optionalAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
|
974
1068
|
/**
|
975
|
-
* Injects all values provided by the registrations associated with the given token
|
1069
|
+
* Injects all values provided by the registrations associated with the given token
|
976
1070
|
* or an empty array if the token is not registered in the container.
|
977
1071
|
*/
|
978
|
-
declare function optionalAll<Value>(token: Token<Value>):
|
1072
|
+
declare function optionalAll<Value>(token: Token<Value>): Value[];
|
979
1073
|
|
980
1074
|
export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping };
|
981
1075
|
export type { ClassProvider, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, Type, ValueProvider };
|