@lppedd/di-wise-neo 0.3.2 → 0.4.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 +23 -18
- package/dist/cjs/index.d.ts +91 -33
- package/dist/cjs/index.js +46 -17
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.mts +91 -33
- package/dist/es/index.mjs +46 -17
- package/dist/es/index.mjs.map +1 -1
- package/package.json +14 -18
package/dist/es/index.d.mts
CHANGED
@@ -3,11 +3,11 @@
|
|
3
3
|
*/
|
4
4
|
interface Type<A> {
|
5
5
|
/**
|
6
|
-
*
|
6
|
+
* The name of the type.
|
7
7
|
*/
|
8
8
|
readonly name: string;
|
9
9
|
/**
|
10
|
-
*
|
10
|
+
* Creates an intersection type from another type.
|
11
11
|
*
|
12
12
|
* @example
|
13
13
|
* ```ts
|
@@ -19,7 +19,7 @@ interface Type<A> {
|
|
19
19
|
*/
|
20
20
|
inter<B>(typeName: string, B: Type<B>): Type<A & B>;
|
21
21
|
/**
|
22
|
-
*
|
22
|
+
* Creates a union type from another type.
|
23
23
|
*
|
24
24
|
* @example
|
25
25
|
* ```ts
|
@@ -37,7 +37,6 @@ interface Type<A> {
|
|
37
37
|
interface Constructor<Instance extends object> {
|
38
38
|
new (...args: any[]): Instance;
|
39
39
|
readonly name: string;
|
40
|
-
readonly length: number;
|
41
40
|
}
|
42
41
|
/**
|
43
42
|
* Token type.
|
@@ -48,11 +47,11 @@ type Token<Value = any> = [Value] extends [object] ? Type<Value> | Constructor<V
|
|
48
47
|
*/
|
49
48
|
type Tokens<Value = any> = [Token<Value>, ...Token<Value>[]];
|
50
49
|
/**
|
51
|
-
*
|
50
|
+
* Creates a type token.
|
52
51
|
*
|
53
52
|
* @example
|
54
53
|
* ```ts
|
55
|
-
* const
|
54
|
+
* const ISpell = createType<Spell>("Spell");
|
56
55
|
* ```
|
57
56
|
*
|
58
57
|
* @__NO_SIDE_EFFECTS__
|
@@ -65,31 +64,33 @@ declare function createType<T>(typeName: string): Type<T>;
|
|
65
64
|
interface ClassProvider<Instance extends object> {
|
66
65
|
readonly useClass: Constructor<Instance>;
|
67
66
|
}
|
68
|
-
/**
|
69
|
-
* Provides a value for a token via another existing token.
|
70
|
-
*/
|
71
|
-
interface ExistingProvider<Value> {
|
72
|
-
readonly useExisting: Token<Value>;
|
73
|
-
}
|
74
67
|
/**
|
75
68
|
* Provides a value for a token via a factory function.
|
76
69
|
*
|
77
|
-
* The factory function runs inside the injection context
|
78
|
-
*
|
70
|
+
* The factory function runs inside the injection context and can
|
71
|
+
* thus access dependencies via {@link inject}-like functions.
|
79
72
|
*/
|
80
73
|
interface FactoryProvider<Value> {
|
81
74
|
readonly useFactory: (...args: []) => Value;
|
82
75
|
}
|
83
76
|
/**
|
84
|
-
* Provides a
|
77
|
+
* Provides a static - already constructed - value for a token.
|
85
78
|
*/
|
86
79
|
interface ValueProvider<T> {
|
87
80
|
readonly useValue: T;
|
88
81
|
}
|
82
|
+
/**
|
83
|
+
* Aliases another registered token.
|
84
|
+
*
|
85
|
+
* Resolving this token will return the value of the aliased one.
|
86
|
+
*/
|
87
|
+
interface ExistingProvider<Value> {
|
88
|
+
readonly useExisting: Token<Value>;
|
89
|
+
}
|
89
90
|
/**
|
90
91
|
* A token provider.
|
91
92
|
*/
|
92
|
-
type Provider<Value = any> = ClassProvider<Value & object> |
|
93
|
+
type Provider<Value = any> = ClassProvider<Value & object> | FactoryProvider<Value> | ValueProvider<Value> | ExistingProvider<Value>;
|
93
94
|
|
94
95
|
declare const Scope: {
|
95
96
|
readonly Inherited: "Inherited";
|
@@ -212,35 +213,87 @@ interface Container {
|
|
212
213
|
* Returns whether the token is registered in this container or in parent containers, if any.
|
213
214
|
*/
|
214
215
|
isRegistered(token: Token): boolean;
|
216
|
+
/**
|
217
|
+
* Registers a concrete class, where the class acts as its own token.
|
218
|
+
*
|
219
|
+
* Tokens provided via the {@link Injectable} decorator applied to the class
|
220
|
+
* are also registered as aliases.
|
221
|
+
*
|
222
|
+
* The default registration scope is determined by the {@link Scoped} decorator,
|
223
|
+
* if present.
|
224
|
+
*/
|
225
|
+
registerClass<Instance extends object>(Class: Constructor<Instance>): void;
|
226
|
+
/**
|
227
|
+
* Registers a concrete class with a token.
|
228
|
+
*
|
229
|
+
* The default registration scope is determined by the {@link Scoped} decorator
|
230
|
+
* applied to the class, if present, but it can be overridden by passing explicit
|
231
|
+
* registration options.
|
232
|
+
*/
|
233
|
+
registerClass<Instance extends object, ProvidedInstance extends Instance>(token: Token<Instance>, Class: Constructor<ProvidedInstance>, options?: RegistrationOptions): void;
|
234
|
+
/**
|
235
|
+
* Registers a token whose value is produced by a factory function.
|
236
|
+
*
|
237
|
+
* The factory function runs inside the injection context and can
|
238
|
+
* thus access dependencies via {@link inject}-like functions.
|
239
|
+
*/
|
240
|
+
registerFactory<Value, ProvidedValue extends Value>(token: Token<Value>, factory: (...args: []) => ProvidedValue, options?: RegistrationOptions): void;
|
241
|
+
/**
|
242
|
+
* Registers a token with a fixed value.
|
243
|
+
*
|
244
|
+
* The provided value is returned as-is when the token is resolved (scopes do not apply).
|
245
|
+
*/
|
246
|
+
registerValue<Value, ProvidedValue extends Value>(token: Token<Value>, value: ProvidedValue): void;
|
247
|
+
/**
|
248
|
+
* Registers one or more tokens as aliases for a target token.
|
249
|
+
*
|
250
|
+
* When an alias is resolved, the target token is resolved instead.
|
251
|
+
*/
|
252
|
+
registerAlias<Value, ProvidedValue extends Value>(targetToken: Token<ProvidedValue>, aliasTokens: Tokens<Value>): void;
|
215
253
|
/**
|
216
254
|
* Registers a {@link ClassProvider}, using the class itself as its token.
|
217
255
|
*
|
218
|
-
* Tokens provided
|
219
|
-
* are also registered as aliases.
|
220
|
-
*
|
256
|
+
* Tokens provided via the {@link Injectable} decorator applied to the class
|
257
|
+
* are also registered as aliases.
|
258
|
+
*
|
259
|
+
* The scope is determined by the {@link Scoped} decorator, if present.
|
260
|
+
*
|
261
|
+
* @see registerClass
|
221
262
|
*/
|
222
|
-
register<Instance extends object>(Class: Constructor<Instance>):
|
263
|
+
register<Instance extends object>(Class: Constructor<Instance>): Container;
|
223
264
|
/**
|
224
265
|
* Registers a {@link ClassProvider} with a token.
|
266
|
+
*
|
267
|
+
* The default registration scope is determined by the {@link Scoped} decorator
|
268
|
+
* applied to the provided class, if present, but it can be overridden by
|
269
|
+
* passing explicit registration options.
|
270
|
+
*
|
271
|
+
* @see registerClass
|
225
272
|
*/
|
226
|
-
register<Instance extends object, ProviderInstance extends Instance>(token: Token<Instance>, provider: ClassProvider<ProviderInstance>, options?: RegistrationOptions):
|
273
|
+
register<Instance extends object, ProviderInstance extends Instance>(token: Token<Instance>, provider: ClassProvider<ProviderInstance>, options?: RegistrationOptions): Container;
|
227
274
|
/**
|
228
275
|
* Registers a {@link FactoryProvider} with a token.
|
276
|
+
*
|
277
|
+
* @see registerFactory
|
229
278
|
*/
|
230
|
-
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: FactoryProvider<ProviderValue>, options?: RegistrationOptions):
|
279
|
+
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: FactoryProvider<ProviderValue>, options?: RegistrationOptions): Container;
|
231
280
|
/**
|
232
281
|
* Registers an {@link ExistingProvider} with a token.
|
233
282
|
*
|
234
283
|
* The token will alias the one set in `useExisting`.
|
284
|
+
*
|
285
|
+
* @see registerAlias
|
235
286
|
*/
|
236
|
-
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ExistingProvider<ProviderValue>):
|
287
|
+
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ExistingProvider<ProviderValue>): Container;
|
237
288
|
/**
|
238
289
|
* Registers a {@link ValueProvider} with a token.
|
239
290
|
*
|
240
291
|
* Values provided via `useValue` are never cached (scopes do not apply)
|
241
292
|
* and are simply returned as-is.
|
293
|
+
*
|
294
|
+
* @see registerValue
|
242
295
|
*/
|
243
|
-
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ValueProvider<ProviderValue>):
|
296
|
+
register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ValueProvider<ProviderValue>): Container;
|
244
297
|
/**
|
245
298
|
* Removes all registrations for the given token from the container's internal registry.
|
246
299
|
*
|
@@ -364,8 +417,8 @@ interface Container {
|
|
364
417
|
declare function createContainer(options?: Partial<ContainerOptions>): Container;
|
365
418
|
|
366
419
|
/**
|
367
|
-
* Class decorator
|
368
|
-
* when first
|
420
|
+
* Class decorator that enables auto-registration of an unregistered class,
|
421
|
+
* when the class is first resolved from the container.
|
369
422
|
*
|
370
423
|
* @example
|
371
424
|
* ```ts
|
@@ -387,9 +440,14 @@ interface TokenRef<Value = any> {
|
|
387
440
|
readonly getRefToken: () => Token<Value>;
|
388
441
|
}
|
389
442
|
/**
|
390
|
-
* Allows referencing
|
443
|
+
* Allows referencing tokens that are declared later in the file by wrapping them
|
444
|
+
* in a lazily evaluated function.
|
391
445
|
*/
|
392
446
|
declare function forwardRef<Value>(token: () => Tokens<Value>): TokensRef<Value>;
|
447
|
+
/**
|
448
|
+
* Allows referencing a token that is declared later in the file by wrapping it
|
449
|
+
* in a lazily evaluated function.
|
450
|
+
*/
|
393
451
|
declare function forwardRef<Value>(token: () => Token<Value>): TokenRef<Value>;
|
394
452
|
|
395
453
|
/**
|
@@ -424,8 +482,8 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
|
|
424
482
|
declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
425
483
|
|
426
484
|
/**
|
427
|
-
* Class decorator
|
428
|
-
* when
|
485
|
+
* Class decorator that registers additional aliasing tokens for the decorated type,
|
486
|
+
* when the type is first registered in the container.
|
429
487
|
*
|
430
488
|
* The container uses {@link ExistingProvider} under the hood.
|
431
489
|
*
|
@@ -437,8 +495,8 @@ declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
|
437
495
|
*/
|
438
496
|
declare function Injectable<This extends object, Value extends This>(...tokens: Tokens<Value>): ClassDecorator;
|
439
497
|
/**
|
440
|
-
* Class decorator
|
441
|
-
* when
|
498
|
+
* Class decorator that registers additional aliasing tokens for the decorated type,
|
499
|
+
* when the type is first registered in the container.
|
442
500
|
*
|
443
501
|
* The container uses {@link ExistingProvider} under the hood.
|
444
502
|
*
|
@@ -718,7 +776,7 @@ declare const Injector: Type<Injector>;
|
|
718
776
|
*/
|
719
777
|
interface MiddlewareComposer {
|
720
778
|
/**
|
721
|
-
*
|
779
|
+
* Adds a middleware function to the composer.
|
722
780
|
*/
|
723
781
|
use<MethodKey extends keyof Container>(key: MethodKey, wrap: Container[MethodKey] extends Function ? (next: Container[MethodKey]) => Container[MethodKey] : never): MiddlewareComposer;
|
724
782
|
}
|
@@ -744,7 +802,7 @@ interface Middleware {
|
|
744
802
|
(composer: MiddlewareComposer, api: Readonly<Container>): void;
|
745
803
|
}
|
746
804
|
/**
|
747
|
-
*
|
805
|
+
* Applies middleware functions to a container.
|
748
806
|
*
|
749
807
|
* Middlewares are applied in array order, but execute in reverse order.
|
750
808
|
*
|
package/dist/es/index.mjs
CHANGED
@@ -207,10 +207,6 @@ function isClassProvider(provider) {
|
|
207
207
|
return "useClass" in provider;
|
208
208
|
}
|
209
209
|
// @internal
|
210
|
-
function isExistingProvider(provider) {
|
211
|
-
return "useExisting" in provider;
|
212
|
-
}
|
213
|
-
// @internal
|
214
210
|
function isFactoryProvider(provider) {
|
215
211
|
return "useFactory" in provider;
|
216
212
|
}
|
@@ -218,6 +214,10 @@ function isFactoryProvider(provider) {
|
|
218
214
|
function isValueProvider(provider) {
|
219
215
|
return "useValue" in provider;
|
220
216
|
}
|
217
|
+
// @internal
|
218
|
+
function isExistingProvider(provider) {
|
219
|
+
return "useExisting" in provider;
|
220
|
+
}
|
221
221
|
|
222
222
|
const Scope = {
|
223
223
|
Inherited: "Inherited",
|
@@ -229,11 +229,11 @@ const Scope = {
|
|
229
229
|
/**
|
230
230
|
* Type API.
|
231
231
|
*/ /**
|
232
|
-
*
|
232
|
+
* Creates a type token.
|
233
233
|
*
|
234
234
|
* @example
|
235
235
|
* ```ts
|
236
|
-
* const
|
236
|
+
* const ISpell = createType<Spell>("Spell");
|
237
237
|
* ```
|
238
238
|
*
|
239
239
|
* @__NO_SIDE_EFFECTS__
|
@@ -279,8 +279,8 @@ function getTypeName(value) {
|
|
279
279
|
// @internal
|
280
280
|
class TokenRegistry {
|
281
281
|
constructor(parent){
|
282
|
-
this.parent = parent;
|
283
282
|
this.myMap = new Map();
|
283
|
+
this.myParent = parent;
|
284
284
|
}
|
285
285
|
get(token) {
|
286
286
|
// To clarify, at(-1) means we take the last added registration for this token
|
@@ -333,7 +333,7 @@ class TokenRegistry {
|
|
333
333
|
}
|
334
334
|
getAllFromParent(token) {
|
335
335
|
const registrations = this.myMap.get(token);
|
336
|
-
return registrations || this.
|
336
|
+
return registrations || this.myParent?.getAllFromParent(token);
|
337
337
|
}
|
338
338
|
}
|
339
339
|
// @internal
|
@@ -384,12 +384,11 @@ function isDisposable(value) {
|
|
384
384
|
|
385
385
|
/**
|
386
386
|
* The default implementation of a di-wise-neo {@link Container}.
|
387
|
-
*/ class
|
388
|
-
constructor(
|
389
|
-
this.myParent = myParent;
|
390
|
-
// eslint-disable-next-line no-use-before-define
|
387
|
+
*/ class ContainerImpl {
|
388
|
+
constructor(parent, options){
|
391
389
|
this.myChildren = new Set();
|
392
390
|
this.myDisposed = false;
|
391
|
+
this.myParent = parent;
|
393
392
|
this.myOptions = {
|
394
393
|
autoRegister: false,
|
395
394
|
defaultScope: Scope.Inherited,
|
@@ -413,7 +412,7 @@ function isDisposable(value) {
|
|
413
412
|
}
|
414
413
|
createChild(options) {
|
415
414
|
this.checkDisposed();
|
416
|
-
const container = new
|
415
|
+
const container = new ContainerImpl(this, {
|
417
416
|
...this.myOptions,
|
418
417
|
...options
|
419
418
|
});
|
@@ -460,6 +459,36 @@ function isDisposable(value) {
|
|
460
459
|
this.checkDisposed();
|
461
460
|
return this.myTokenRegistry.get(token) !== undefined;
|
462
461
|
}
|
462
|
+
registerClass(token, Class, options) {
|
463
|
+
// This mess will go away once/if we remove the register method
|
464
|
+
// in favor of the multiple specialized ones
|
465
|
+
if (Class) {
|
466
|
+
const ctor = Class ?? token;
|
467
|
+
this.register(token, {
|
468
|
+
useClass: ctor
|
469
|
+
}, options);
|
470
|
+
} else {
|
471
|
+
this.register(token);
|
472
|
+
}
|
473
|
+
}
|
474
|
+
registerFactory(token, factory, options) {
|
475
|
+
this.register(token, {
|
476
|
+
useFactory: factory
|
477
|
+
}, options);
|
478
|
+
}
|
479
|
+
registerValue(token, value) {
|
480
|
+
this.register(token, {
|
481
|
+
useValue: value
|
482
|
+
});
|
483
|
+
}
|
484
|
+
registerAlias(targetToken, aliasTokens) {
|
485
|
+
// De-duplicate tokens
|
486
|
+
for (const alias of new Set(aliasTokens)){
|
487
|
+
this.register(alias, {
|
488
|
+
useExisting: targetToken
|
489
|
+
});
|
490
|
+
}
|
491
|
+
}
|
463
492
|
register(...args) {
|
464
493
|
this.checkDisposed();
|
465
494
|
if (args.length == 1) {
|
@@ -785,12 +814,12 @@ function isDisposable(value) {
|
|
785
814
|
autoRegister: false,
|
786
815
|
defaultScope: Scope.Inherited
|
787
816
|
}) {
|
788
|
-
return new
|
817
|
+
return new ContainerImpl(undefined, options);
|
789
818
|
}
|
790
819
|
|
791
820
|
/**
|
792
|
-
* Class decorator
|
793
|
-
* when first
|
821
|
+
* Class decorator that enables auto-registration of an unregistered class,
|
822
|
+
* when the class is first resolved from the container.
|
794
823
|
*
|
795
824
|
* @example
|
796
825
|
* ```ts
|
@@ -990,7 +1019,7 @@ function OptionalAll(token) {
|
|
990
1019
|
});
|
991
1020
|
|
992
1021
|
/**
|
993
|
-
*
|
1022
|
+
* Applies middleware functions to a container.
|
994
1023
|
*
|
995
1024
|
* Middlewares are applied in array order, but execute in reverse order.
|
996
1025
|
*
|