@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.
@@ -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/es/index.mjs CHANGED
@@ -52,14 +52,12 @@ function getTokenPath(tokens) {
52
52
  function getTokenName(token) {
53
53
  return token.name || "<unnamed>";
54
54
  }
55
+ // @internal
55
56
  function getFullTokenName([token, name]) {
56
57
  const tokenName = token ? token.name || "<unnamed>" : "<undefined token>";
57
- return name !== undefined ? `${tokenName}["${name}"]` : tokenName;
58
+ return name !== undefined ? `${tokenName}['${name}']` : tokenName;
58
59
  }
59
60
  function getCause(error) {
60
- if (!error) {
61
- return "";
62
- }
63
61
  const msg = isError(error) ? error.message : String(error);
64
62
  return `\n [cause] ${untag(msg)}`;
65
63
  }
@@ -437,24 +435,25 @@ function describeParam(target, methodKey, parameterIndex) {
437
435
  }
438
436
 
439
437
  // @__NO_SIDE_EFFECTS__
440
- function createType(typeName, provider, options) {
441
- const name = `Type<${typeName}>`;
438
+ function createType(debugName, provider, options) {
442
439
  const type = (target, propertyKey, parameterIndex)=>{
443
- updateParameterMetadata(name, target, propertyKey, parameterIndex, (dependency)=>{
440
+ updateParameterMetadata(debugName, target, propertyKey, parameterIndex, (dependency)=>{
444
441
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
445
442
  dependency.appliedBy = "Inject";
446
443
  dependency.tokenRef = tokenRef(()=>type);
447
444
  });
448
445
  };
446
+ const name = `Type<${debugName}>`;
449
447
  Object.defineProperty(type, "name", {
450
448
  value: name
451
449
  });
450
+ type.debugName = debugName;
451
+ type.__type = undefined;
452
+ type.toString = ()=>name;
452
453
  if (provider) {
453
454
  type.provider = provider;
454
455
  type.options = options;
455
456
  }
456
- type.__type = undefined;
457
- type.toString = ()=>name;
458
457
  return type;
459
458
  }
460
459
  // @internal
@@ -511,7 +510,10 @@ class TokenRegistry {
511
510
  const name = registration.name;
512
511
  if (name !== undefined) {
513
512
  const existing = registrations.filter((r)=>r.name === name);
514
- check(existing.length === 0, `token ${getTokenName(token)} with name '${name}' is already registered`);
513
+ check(existing.length === 0, `token ${getFullTokenName([
514
+ token,
515
+ name
516
+ ])} is already registered`);
515
517
  }
516
518
  } else {
517
519
  this.myRegistrations.set(token, registrations = []);
@@ -579,8 +581,8 @@ function isBuilder(provider) {
579
581
  return builders.has(provider);
580
582
  }
581
583
  // @__NO_SIDE_EFFECTS__
582
- function build(factory, name) {
583
- const token = createType(name ?? `Build<${getTypeName(factory)}>`);
584
+ function build(factory, debugName) {
585
+ const token = createType(debugName ?? `Build<${getTypeName(factory)}>`);
584
586
  const provider = {
585
587
  useFactory: factory
586
588
  };
@@ -1111,47 +1113,18 @@ function isDisposable(value) {
1111
1113
  return new ContainerImpl(undefined, options);
1112
1114
  }
1113
1115
 
1114
- /**
1115
- * Class decorator that enables auto-registration of an unregistered class
1116
- * when the class is first resolved from the container.
1117
- *
1118
- * Example:
1119
- * ```ts
1120
- * @AutoRegister()
1121
- * class Wizard {}
1122
- *
1123
- * const wizard = container.resolve(Wizard);
1124
- * container.isRegistered(Wizard); // => true
1125
- * ```
1126
- */ // @__NO_SIDE_EFFECTS__
1127
- function AutoRegister() {
1128
- return (Class)=>{
1116
+ // @__NO_SIDE_EFFECTS__
1117
+ function AutoRegister(target) {
1118
+ const decorator = (Class)=>{
1129
1119
  const metadata = getMetadata(Class);
1130
1120
  metadata.autoRegister = true;
1131
1121
  };
1122
+ return target === undefined ? decorator : decorator(target);
1132
1123
  }
1133
1124
 
1134
- /**
1135
- * Class decorator that sets the class scope to **Container** and enables
1136
- * eager instantiation when the class is registered in the container.
1137
- *
1138
- * This causes the container to create and cache the instance of the class
1139
- * immediately, instead of deferring it until the first resolution.
1140
- *
1141
- * If the class cannot be resolved at registration time, registration fails.
1142
- *
1143
- * Example:
1144
- * ```ts
1145
- * @EagerInstantiate()
1146
- * class Wizard {}
1147
- *
1148
- * // Wizard is registered with Container scope, and an instance
1149
- * // is created and cached immediately by the container
1150
- * container.register(Wizard);
1151
- * ```
1152
- */ // @__NO_SIDE_EFFECTS__
1153
- function EagerInstantiate() {
1154
- return (Class)=>{
1125
+ // @__NO_SIDE_EFFECTS__
1126
+ function EagerInstantiate(target) {
1127
+ const decorator = (Class)=>{
1155
1128
  const metadata = getMetadata(Class);
1156
1129
  const currentScope = metadata.scope;
1157
1130
  check(!currentScope || currentScope.value === "Container", ()=>{
@@ -1166,6 +1139,7 @@ function EagerInstantiate() {
1166
1139
  appliedBy: "EagerInstantiate"
1167
1140
  };
1168
1141
  };
1142
+ return target === undefined ? decorator : decorator(target);
1169
1143
  }
1170
1144
 
1171
1145
  // @__NO_SIDE_EFFECTS__
@@ -1275,55 +1249,20 @@ function OptionalAll(token) {
1275
1249
  };
1276
1250
  }
1277
1251
 
1278
- /**
1279
- * Class decorator that registers the decorated type with the **Container** scope.
1280
- *
1281
- * Use this scope when you want one cached instance per container,
1282
- * with parent-container lookup fallback.
1283
- *
1284
- * Example:
1285
- * ```ts
1286
- * @ContainerScoped()
1287
- * class Wizard {
1288
- * // ...
1289
- * }
1290
- * ```
1291
- */ // @__NO_SIDE_EFFECTS__
1292
- function ContainerScoped() {
1293
- return scoped("Container", "ContainerScoped");
1252
+ // @__NO_SIDE_EFFECTS__
1253
+ function ContainerScoped(target) {
1254
+ const decorator = scoped("Container", "ContainerScoped");
1255
+ return target === undefined ? decorator : decorator(target);
1294
1256
  }
1295
- /**
1296
- * Class decorator that registers the decorated type with the **Resolution** scope.
1297
- *
1298
- * Use this scope when you want one cached instance per resolution graph,
1299
- * so repeated resolutions within the same request reuse the same value.
1300
- *
1301
- * Example:
1302
- * ```ts
1303
- * @ResolutionScoped()
1304
- * class Wand {
1305
- * // ...
1306
- * }
1307
- * ```
1308
- */ // @__NO_SIDE_EFFECTS__
1309
- function ResolutionScoped() {
1310
- return scoped("Resolution", "ResolutionScoped");
1257
+ // @__NO_SIDE_EFFECTS__
1258
+ function ResolutionScoped(target) {
1259
+ const decorator = scoped("Resolution", "ResolutionScoped");
1260
+ return target === undefined ? decorator : decorator(target);
1311
1261
  }
1312
- /**
1313
- * Class decorator that registers the decorated type with the **Transient** scope.
1314
- *
1315
- * Use this scope when you want a fresh instance every time the class is resolved.
1316
- *
1317
- * Example:
1318
- * ```ts
1319
- * @TransientScoped()
1320
- * class Wand {
1321
- * // ...
1322
- * }
1323
- * ```
1324
- */ // @__NO_SIDE_EFFECTS__
1325
- function TransientScoped() {
1326
- return scoped("Transient", "TransientScoped");
1262
+ // @__NO_SIDE_EFFECTS__
1263
+ function TransientScoped(target) {
1264
+ const decorator = scoped("Transient", "TransientScoped");
1265
+ return target === undefined ? decorator : decorator(target);
1327
1266
  }
1328
1267
  /**
1329
1268
  * Class decorator for setting the scope of the decorated type when registering it.