@lppedd/di-wise-neo 0.23.0 → 0.24.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 CHANGED
@@ -512,7 +512,7 @@ Enables automatic registration of the decorated class when it is resolved if it
512
512
  been registered beforehand.
513
513
 
514
514
  ```ts
515
- @AutoRegister()
515
+ @AutoRegister
516
516
  export class ExtensionContext {
517
517
  /* ... */
518
518
  }
@@ -529,7 +529,7 @@ This causes the container to immediately create and cache the instance of the cl
529
529
  at registration time, instead of deferring instantiation until the first resolution.
530
530
 
531
531
  ```ts
532
- @EagerInstantiate()
532
+ @EagerInstantiate
533
533
  export class ExtensionContext {
534
534
  /* ... */
535
535
  }
@@ -55,9 +55,13 @@ declare function build<Value>(factory: (...args: []) => Value): Type<Value>;
55
55
  */
56
56
  interface Type<T> extends ParameterDecorator {
57
57
  /**
58
- * The name of the type.
58
+ * The type's presentable name, in the `Type<debugName>` format.
59
59
  */
60
60
  readonly name: string;
61
+ /**
62
+ * The type's debug name, as passed to {@link createType}.
63
+ */
64
+ readonly debugName: string;
61
65
  /**
62
66
  * Ensures that different `Type<T>` types are not structurally compatible.
63
67
  *
@@ -66,10 +70,6 @@ interface Type<T> extends ParameterDecorator {
66
70
  * @private
67
71
  */
68
72
  readonly __type: T | undefined;
69
- /**
70
- * Returns the type's {@link Type.name|name}.
71
- */
72
- readonly toString: () => string;
73
73
  }
74
74
  /**
75
75
  * An injectable type `T` with a default {@link Provider} and optional default registration options.
@@ -110,7 +110,7 @@ type Tokens<Value> = [Token<Value>, ...Token<Value>[]];
110
110
  * });
111
111
  * ```
112
112
  */
113
- declare function createType<T>(typeName: string): Type<T>;
113
+ declare function createType<T>(debugName: string): Type<T>;
114
114
  /**
115
115
  * Creates a type token with a default {@link Provider} and optional default registration options.
116
116
  *
@@ -124,7 +124,7 @@ declare function createType<T>(typeName: string): Type<T>;
124
124
  * container.register(ISpell);
125
125
  * ```
126
126
  */
127
- declare function createType<T>(typeName: string, provider: Provider<T>, options?: RegistrationOptions): ProviderType<T>;
127
+ declare function createType<T>(debugName: string, provider: Provider<T>, options?: RegistrationOptions): ProviderType<T>;
128
128
 
129
129
  interface ClassRef<Instance extends object> {
130
130
  readonly getRefClass: () => Constructor<Instance>;
@@ -271,8 +271,6 @@ type RequiredNonNullable<T> = {
271
271
  [P in keyof T]-?: NonNullable<T[P]>;
272
272
  };
273
273
 
274
- type ProviderFor<V> = V extends object ? Provider<V> : Exclude<Provider<V>, ClassProvider<any>>;
275
- type RegistrationOptionsFor<P> = P extends ValueProvider<any> ? never : RegistrationOptions;
276
274
  /**
277
275
  * Container creation options.
278
276
  */
@@ -412,23 +410,6 @@ interface Container {
412
410
  * Registers a token type with a default {@link Provider} and optional default registration options.
413
411
  */
414
412
  register<Value>(token: ProviderType<Value>): Container;
415
- /**
416
- * Registers a {@link Provider} with a token or class.
417
- *
418
- * The provider must be one of:
419
- * - {@link ClassProvider} via `useClass`
420
- * - {@link FactoryProvider} via `useFactory`
421
- * - {@link ValueProvider} via `useValue`
422
- * - {@link ExistingProvider} via `useExisting`
423
- *
424
- * For {@link ClassProvider} registrations, the default scope is determined by the {@link Scoped}
425
- * decorator applied to the provided class - if present - or by the {@link ContainerOptions.defaultScope}
426
- * value, but it can be overridden by passing explicit registration options.
427
- *
428
- * For {@link ValueProvider} registrations, the provided value is returned as-is and never cached,
429
- * and registration options do not apply.
430
- */
431
- register<Value, ProviderValue extends Value, Provider extends ProviderFor<ProviderValue>>(token: Token<Value>, provider: ProviderFor<ProviderValue> & Provider, options?: RegistrationOptionsFor<Provider>): Container;
432
413
  /**
433
414
  * Registers a {@link ClassProvider} with a token.
434
415
  *
@@ -663,6 +644,20 @@ interface Container {
663
644
  */
664
645
  declare function createContainer(options?: ContainerOptions): Container;
665
646
 
647
+ /**
648
+ * Class decorator that enables auto-registration of an unregistered class
649
+ * when the class is first resolved from the container.
650
+ *
651
+ * Example:
652
+ * ```ts
653
+ * @AutoRegister
654
+ * class Wizard {}
655
+ *
656
+ * const wizard = container.resolve(Wizard);
657
+ * container.isRegistered(Wizard); // => true
658
+ * ```
659
+ */
660
+ declare function AutoRegister<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
666
661
  /**
667
662
  * Class decorator that enables auto-registration of an unregistered class
668
663
  * when the class is first resolved from the container.
@@ -675,9 +670,31 @@ declare function createContainer(options?: ContainerOptions): Container;
675
670
  * const wizard = container.resolve(Wizard);
676
671
  * container.isRegistered(Wizard); // => true
677
672
  * ```
673
+ *
674
+ * @deprecated Use `@AutoRegister` instead of `AutoRegister()`.
678
675
  */
679
676
  declare function AutoRegister<This extends object>(): ClassDecorator<This>;
680
677
 
678
+ /**
679
+ * Class decorator that sets the class scope to **Container** and enables
680
+ * eager instantiation when the class is registered in the container.
681
+ *
682
+ * This causes the container to create and cache the instance of the class
683
+ * immediately, instead of deferring it until the first resolution.
684
+ *
685
+ * If the class cannot be resolved at registration time, registration fails.
686
+ *
687
+ * Example:
688
+ * ```ts
689
+ * @EagerInstantiate
690
+ * class Wizard {}
691
+ *
692
+ * // Wizard is registered with Container scope, and an instance
693
+ * // is created and cached immediately by the container
694
+ * container.register(Wizard);
695
+ * ```
696
+ */
697
+ declare function EagerInstantiate<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
681
698
  /**
682
699
  * Class decorator that sets the class scope to **Container** and enables
683
700
  * eager instantiation when the class is registered in the container.
@@ -696,6 +713,8 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
696
713
  * // is created and cached immediately by the container
697
714
  * container.register(Wizard);
698
715
  * ```
716
+ *
717
+ * @deprecated Use `@EagerInstantiate` instead of `@EagerInstantiate()`.
699
718
  */
700
719
  declare function EagerInstantiate<This extends object>(): ClassDecorator<This>;
701
720
 
@@ -930,6 +949,21 @@ declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
930
949
  */
931
950
  declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
932
951
 
952
+ /**
953
+ * Class decorator that registers the decorated type with the **Container** scope.
954
+ *
955
+ * Use this scope when you want one cached instance per container,
956
+ * with parent-container lookup fallback.
957
+ *
958
+ * Example:
959
+ * ```ts
960
+ * @ContainerScoped
961
+ * class Wizard {
962
+ * // ...
963
+ * }
964
+ * ```
965
+ */
966
+ declare function ContainerScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
933
967
  /**
934
968
  * Class decorator that registers the decorated type with the **Container** scope.
935
969
  *
@@ -943,8 +977,25 @@ declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator
943
977
  * // ...
944
978
  * }
945
979
  * ```
980
+ *
981
+ * @deprecated Use `@ContainerScoped` instead of `@ContainerScoped()`.
946
982
  */
947
983
  declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
984
+ /**
985
+ * Class decorator that registers the decorated type with the **Resolution** scope.
986
+ *
987
+ * Use this scope when you want one cached instance per resolution graph,
988
+ * so repeated resolutions within the same request reuse the same value.
989
+ *
990
+ * Example:
991
+ * ```ts
992
+ * @ResolutionScoped
993
+ * class Wand {
994
+ * // ...
995
+ * }
996
+ * ```
997
+ */
998
+ declare function ResolutionScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
948
999
  /**
949
1000
  * Class decorator that registers the decorated type with the **Resolution** scope.
950
1001
  *
@@ -958,8 +1009,24 @@ declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
958
1009
  * // ...
959
1010
  * }
960
1011
  * ```
1012
+ *
1013
+ * @deprecated Use `@ResolutionScoped` instead of `@ResolutionScoped()`.
961
1014
  */
962
1015
  declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
1016
+ /**
1017
+ * Class decorator that registers the decorated type with the **Transient** scope.
1018
+ *
1019
+ * Use this scope when you want a fresh instance every time the class is resolved.
1020
+ *
1021
+ * Example:
1022
+ * ```ts
1023
+ * @TransientScoped
1024
+ * class Wand {
1025
+ * // ...
1026
+ * }
1027
+ * ```
1028
+ */
1029
+ declare function TransientScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
963
1030
  /**
964
1031
  * Class decorator that registers the decorated type with the **Transient** scope.
965
1032
  *
@@ -972,6 +1039,8 @@ declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
972
1039
  * // ...
973
1040
  * }
974
1041
  * ```
1042
+ *
1043
+ * @deprecated Use `@TransientScoped` instead of `@TransientScoped()`.
975
1044
  */
976
1045
  declare function TransientScoped<This extends object>(): ClassDecorator<This>;
977
1046
  /**
@@ -1055,7 +1124,7 @@ declare function injectAll<Value>(token: Token<Value>): Value[];
1055
1124
  * @param Class - The class to resolve.
1056
1125
  * @param name - The name qualifier of the class to resolve.
1057
1126
  */
1058
- declare function injectBy<Instance extends object>(thisArg: any, Class: Constructor<Instance>, name?: string): Instance;
1127
+ declare function injectBy<Instance extends object>(thisArg: object, Class: Constructor<Instance>, name?: string): Instance;
1059
1128
  /**
1060
1129
  * Injects the value associated with the given token.
1061
1130
  *
@@ -1079,7 +1148,7 @@ declare function injectBy<Instance extends object>(thisArg: any, Class: Construc
1079
1148
  * @param token - The token to resolve.
1080
1149
  * @param name - The name qualifier of the token to resolve.
1081
1150
  */
1082
- declare function injectBy<Value>(thisArg: any, token: Token<Value>, name?: string): Value;
1151
+ declare function injectBy<Value>(thisArg: object, token: Token<Value>, name?: string): Value;
1083
1152
 
1084
1153
  /**
1085
1154
  * Asserts that the current stack frame is within an injection context,
@@ -1250,7 +1319,7 @@ declare function optionalAll<Value>(token: Token<Value>): Value[];
1250
1319
  * @param Class - The class to resolve.
1251
1320
  * @param name - The name qualifier of the class to resolve.
1252
1321
  */
1253
- declare function optionalBy<Instance extends object>(thisArg: any, Class: Constructor<Instance>, name?: string): Instance | undefined;
1322
+ declare function optionalBy<Instance extends object>(thisArg: object, Class: Constructor<Instance>, name?: string): Instance | undefined;
1254
1323
  /**
1255
1324
  * Injects the value associated with the given token,
1256
1325
  * or `undefined` if the token is not registered in the container.
@@ -1262,7 +1331,7 @@ declare function optionalBy<Instance extends object>(thisArg: any, Class: Constr
1262
1331
  * @param token - The token to resolve.
1263
1332
  * @param name - The name qualifier of the token to resolve.
1264
1333
  */
1265
- declare function optionalBy<Value>(thisArg: any, token: Token<Value>, name?: string): Value | undefined;
1334
+ declare function optionalBy<Value>(thisArg: object, token: Token<Value>, name?: string): Value | undefined;
1266
1335
 
1267
1336
  export { AutoRegister, ContainerScoped, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, ResolutionScoped, Scope, Scoped, TransientScoped, assertInjectionContext, build, classRef, createContainer, createType, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping, tokenRef };
1268
1337
  export type { ChildContainerOptions, ClassProvider, ClassRef, Constructor, Container, ContainerHook, ContainerOptions, ExistingProvider, FactoryProvider, Provider, ProviderType, RegistrationOptions, Token, TokenRef, Tokens, Type, ValueProvider };
package/dist/cjs/index.js CHANGED
@@ -54,14 +54,12 @@ function getTokenPath(tokens) {
54
54
  function getTokenName(token) {
55
55
  return token.name || "<unnamed>";
56
56
  }
57
+ // @internal
57
58
  function getFullTokenName([token, name]) {
58
59
  const tokenName = token ? token.name || "<unnamed>" : "<undefined token>";
59
- return name !== undefined ? `${tokenName}["${name}"]` : tokenName;
60
+ return name !== undefined ? `${tokenName}['${name}']` : tokenName;
60
61
  }
61
62
  function getCause(error) {
62
- if (!error) {
63
- return "";
64
- }
65
63
  const msg = isError(error) ? error.message : String(error);
66
64
  return `\n [cause] ${untag(msg)}`;
67
65
  }
@@ -439,24 +437,25 @@ function describeParam(target, methodKey, parameterIndex) {
439
437
  }
440
438
 
441
439
  // @__NO_SIDE_EFFECTS__
442
- function createType(typeName, provider, options) {
443
- const name = `Type<${typeName}>`;
440
+ function createType(debugName, provider, options) {
444
441
  const type = (target, propertyKey, parameterIndex)=>{
445
- updateParameterMetadata(name, target, propertyKey, parameterIndex, (dependency)=>{
442
+ updateParameterMetadata(debugName, target, propertyKey, parameterIndex, (dependency)=>{
446
443
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
447
444
  dependency.appliedBy = "Inject";
448
445
  dependency.tokenRef = tokenRef(()=>type);
449
446
  });
450
447
  };
448
+ const name = `Type<${debugName}>`;
451
449
  Object.defineProperty(type, "name", {
452
450
  value: name
453
451
  });
452
+ type.debugName = debugName;
453
+ type.__type = undefined;
454
+ type.toString = ()=>name;
454
455
  if (provider) {
455
456
  type.provider = provider;
456
457
  type.options = options;
457
458
  }
458
- type.__type = undefined;
459
- type.toString = ()=>name;
460
459
  return type;
461
460
  }
462
461
  // @internal
@@ -513,7 +512,10 @@ class TokenRegistry {
513
512
  const name = registration.name;
514
513
  if (name !== undefined) {
515
514
  const existing = registrations.filter((r)=>r.name === name);
516
- check(existing.length === 0, `token ${getTokenName(token)} with name '${name}' is already registered`);
515
+ check(existing.length === 0, `token ${getFullTokenName([
516
+ token,
517
+ name
518
+ ])} is already registered`);
517
519
  }
518
520
  } else {
519
521
  this.myRegistrations.set(token, registrations = []);
@@ -581,8 +583,8 @@ function isBuilder(provider) {
581
583
  return builders.has(provider);
582
584
  }
583
585
  // @__NO_SIDE_EFFECTS__
584
- function build(factory, name) {
585
- const token = createType(name ?? `Build<${getTypeName(factory)}>`);
586
+ function build(factory, debugName) {
587
+ const token = createType(debugName ?? `Build<${getTypeName(factory)}>`);
586
588
  const provider = {
587
589
  useFactory: factory
588
590
  };
@@ -1113,47 +1115,18 @@ function isDisposable(value) {
1113
1115
  return new ContainerImpl(undefined, options);
1114
1116
  }
1115
1117
 
1116
- /**
1117
- * Class decorator that enables auto-registration of an unregistered class
1118
- * when the class is first resolved from the container.
1119
- *
1120
- * Example:
1121
- * ```ts
1122
- * @AutoRegister()
1123
- * class Wizard {}
1124
- *
1125
- * const wizard = container.resolve(Wizard);
1126
- * container.isRegistered(Wizard); // => true
1127
- * ```
1128
- */ // @__NO_SIDE_EFFECTS__
1129
- function AutoRegister() {
1130
- return (Class)=>{
1118
+ // @__NO_SIDE_EFFECTS__
1119
+ function AutoRegister(target) {
1120
+ const decorator = (Class)=>{
1131
1121
  const metadata = getMetadata(Class);
1132
1122
  metadata.autoRegister = true;
1133
1123
  };
1124
+ return target === undefined ? decorator : decorator(target);
1134
1125
  }
1135
1126
 
1136
- /**
1137
- * Class decorator that sets the class scope to **Container** and enables
1138
- * eager instantiation when the class is registered in the container.
1139
- *
1140
- * This causes the container to create and cache the instance of the class
1141
- * immediately, instead of deferring it until the first resolution.
1142
- *
1143
- * If the class cannot be resolved at registration time, registration fails.
1144
- *
1145
- * Example:
1146
- * ```ts
1147
- * @EagerInstantiate()
1148
- * class Wizard {}
1149
- *
1150
- * // Wizard is registered with Container scope, and an instance
1151
- * // is created and cached immediately by the container
1152
- * container.register(Wizard);
1153
- * ```
1154
- */ // @__NO_SIDE_EFFECTS__
1155
- function EagerInstantiate() {
1156
- return (Class)=>{
1127
+ // @__NO_SIDE_EFFECTS__
1128
+ function EagerInstantiate(target) {
1129
+ const decorator = (Class)=>{
1157
1130
  const metadata = getMetadata(Class);
1158
1131
  const currentScope = metadata.scope;
1159
1132
  check(!currentScope || currentScope.value === "Container", ()=>{
@@ -1168,6 +1141,7 @@ function EagerInstantiate() {
1168
1141
  appliedBy: "EagerInstantiate"
1169
1142
  };
1170
1143
  };
1144
+ return target === undefined ? decorator : decorator(target);
1171
1145
  }
1172
1146
 
1173
1147
  // @__NO_SIDE_EFFECTS__
@@ -1277,55 +1251,20 @@ function OptionalAll(token) {
1277
1251
  };
1278
1252
  }
1279
1253
 
1280
- /**
1281
- * Class decorator that registers the decorated type with the **Container** scope.
1282
- *
1283
- * Use this scope when you want one cached instance per container,
1284
- * with parent-container lookup fallback.
1285
- *
1286
- * Example:
1287
- * ```ts
1288
- * @ContainerScoped()
1289
- * class Wizard {
1290
- * // ...
1291
- * }
1292
- * ```
1293
- */ // @__NO_SIDE_EFFECTS__
1294
- function ContainerScoped() {
1295
- return scoped("Container", "ContainerScoped");
1254
+ // @__NO_SIDE_EFFECTS__
1255
+ function ContainerScoped(target) {
1256
+ const decorator = scoped("Container", "ContainerScoped");
1257
+ return target === undefined ? decorator : decorator(target);
1296
1258
  }
1297
- /**
1298
- * Class decorator that registers the decorated type with the **Resolution** scope.
1299
- *
1300
- * Use this scope when you want one cached instance per resolution graph,
1301
- * so repeated resolutions within the same request reuse the same value.
1302
- *
1303
- * Example:
1304
- * ```ts
1305
- * @ResolutionScoped()
1306
- * class Wand {
1307
- * // ...
1308
- * }
1309
- * ```
1310
- */ // @__NO_SIDE_EFFECTS__
1311
- function ResolutionScoped() {
1312
- return scoped("Resolution", "ResolutionScoped");
1259
+ // @__NO_SIDE_EFFECTS__
1260
+ function ResolutionScoped(target) {
1261
+ const decorator = scoped("Resolution", "ResolutionScoped");
1262
+ return target === undefined ? decorator : decorator(target);
1313
1263
  }
1314
- /**
1315
- * Class decorator that registers the decorated type with the **Transient** scope.
1316
- *
1317
- * Use this scope when you want a fresh instance every time the class is resolved.
1318
- *
1319
- * Example:
1320
- * ```ts
1321
- * @TransientScoped()
1322
- * class Wand {
1323
- * // ...
1324
- * }
1325
- * ```
1326
- */ // @__NO_SIDE_EFFECTS__
1327
- function TransientScoped() {
1328
- return scoped("Transient", "TransientScoped");
1264
+ // @__NO_SIDE_EFFECTS__
1265
+ function TransientScoped(target) {
1266
+ const decorator = scoped("Transient", "TransientScoped");
1267
+ return target === undefined ? decorator : decorator(target);
1329
1268
  }
1330
1269
  /**
1331
1270
  * Class decorator for setting the scope of the decorated type when registering it.