@lppedd/di-wise-neo 0.23.1 → 0.25.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
  *
@@ -669,14 +650,14 @@ declare function createContainer(options?: ContainerOptions): Container;
669
650
  *
670
651
  * Example:
671
652
  * ```ts
672
- * @AutoRegister()
653
+ * @AutoRegister
673
654
  * class Wizard {}
674
655
  *
675
656
  * const wizard = container.resolve(Wizard);
676
657
  * container.isRegistered(Wizard); // => true
677
658
  * ```
678
659
  */
679
- declare function AutoRegister<This extends object>(): ClassDecorator<This>;
660
+ declare function AutoRegister<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
680
661
 
681
662
  /**
682
663
  * Class decorator that sets the class scope to **Container** and enables
@@ -689,7 +670,7 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
689
670
  *
690
671
  * Example:
691
672
  * ```ts
692
- * @EagerInstantiate()
673
+ * @EagerInstantiate
693
674
  * class Wizard {}
694
675
  *
695
676
  * // Wizard is registered with Container scope, and an instance
@@ -697,7 +678,7 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
697
678
  * container.register(Wizard);
698
679
  * ```
699
680
  */
700
- declare function EagerInstantiate<This extends object>(): ClassDecorator<This>;
681
+ declare function EagerInstantiate<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
701
682
 
702
683
  /**
703
684
  * Parameter decorator that injects the instance associated with the given class.
@@ -938,13 +919,13 @@ declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator
938
919
  *
939
920
  * Example:
940
921
  * ```ts
941
- * @ContainerScoped()
922
+ * @ContainerScoped
942
923
  * class Wizard {
943
924
  * // ...
944
925
  * }
945
926
  * ```
946
927
  */
947
- declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
928
+ declare function ContainerScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
948
929
  /**
949
930
  * Class decorator that registers the decorated type with the **Resolution** scope.
950
931
  *
@@ -953,13 +934,13 @@ declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
953
934
  *
954
935
  * Example:
955
936
  * ```ts
956
- * @ResolutionScoped()
937
+ * @ResolutionScoped
957
938
  * class Wand {
958
939
  * // ...
959
940
  * }
960
941
  * ```
961
942
  */
962
- declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
943
+ declare function ResolutionScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
963
944
  /**
964
945
  * Class decorator that registers the decorated type with the **Transient** scope.
965
946
  *
@@ -967,13 +948,13 @@ declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
967
948
  *
968
949
  * Example:
969
950
  * ```ts
970
- * @TransientScoped()
951
+ * @TransientScoped
971
952
  * class Wand {
972
953
  * // ...
973
954
  * }
974
955
  * ```
975
956
  */
976
- declare function TransientScoped<This extends object>(): ClassDecorator<This>;
957
+ declare function TransientScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
977
958
  /**
978
959
  * Class decorator for setting the scope of the decorated type when registering it.
979
960
  *
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
  };
@@ -1117,18 +1119,16 @@ function isDisposable(value) {
1117
1119
  *
1118
1120
  * Example:
1119
1121
  * ```ts
1120
- * @AutoRegister()
1122
+ * @AutoRegister
1121
1123
  * class Wizard {}
1122
1124
  *
1123
1125
  * const wizard = container.resolve(Wizard);
1124
1126
  * container.isRegistered(Wizard); // => true
1125
1127
  * ```
1126
1128
  */ // @__NO_SIDE_EFFECTS__
1127
- function AutoRegister() {
1128
- return (Class)=>{
1129
- const metadata = getMetadata(Class);
1130
- metadata.autoRegister = true;
1131
- };
1129
+ function AutoRegister(target) {
1130
+ const metadata = getMetadata(target);
1131
+ metadata.autoRegister = true;
1132
1132
  }
1133
1133
 
1134
1134
  /**
@@ -1142,7 +1142,7 @@ function AutoRegister() {
1142
1142
  *
1143
1143
  * Example:
1144
1144
  * ```ts
1145
- * @EagerInstantiate()
1145
+ * @EagerInstantiate
1146
1146
  * class Wizard {}
1147
1147
  *
1148
1148
  * // Wizard is registered with Container scope, and an instance
@@ -1150,21 +1150,19 @@ function AutoRegister() {
1150
1150
  * container.register(Wizard);
1151
1151
  * ```
1152
1152
  */ // @__NO_SIDE_EFFECTS__
1153
- function EagerInstantiate() {
1154
- return (Class)=>{
1155
- const metadata = getMetadata(Class);
1156
- const currentScope = metadata.scope;
1157
- check(!currentScope || currentScope.value === "Container", ()=>{
1158
- const { value, appliedBy } = currentScope;
1159
- const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1160
- const className = getTokenName(Class);
1161
- return `class ${className}: scope ${value} was already set by @${by},\n ` + `but @EagerInstantiate is trying to set a conflicting scope Container.\n ` + `Only one decorator should set the class scope, or all must use the same value.`;
1162
- });
1163
- metadata.eagerInstantiate = true;
1164
- metadata.scope = {
1165
- value: "Container",
1166
- appliedBy: "EagerInstantiate"
1167
- };
1153
+ function EagerInstantiate(target) {
1154
+ const metadata = getMetadata(target);
1155
+ const currentScope = metadata.scope;
1156
+ check(!currentScope || currentScope.value === "Container", ()=>{
1157
+ const { value, appliedBy } = currentScope;
1158
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1159
+ const className = getTokenName(target);
1160
+ return `class ${className}: scope ${value} was already set by @${by},\n ` + `but @EagerInstantiate is trying to set a conflicting scope Container.\n ` + `Only one decorator should set the class scope, or all must use the same value.`;
1161
+ });
1162
+ metadata.eagerInstantiate = true;
1163
+ metadata.scope = {
1164
+ value: "Container",
1165
+ appliedBy: "EagerInstantiate"
1168
1166
  };
1169
1167
  }
1170
1168
 
@@ -1181,8 +1179,8 @@ function Inject(token) {
1181
1179
 
1182
1180
  // @__NO_SIDE_EFFECTS__
1183
1181
  function Injectable(...args) {
1184
- return (Class)=>{
1185
- const metadata = getMetadata(Class);
1182
+ return (target)=>{
1183
+ const metadata = getMetadata(target);
1186
1184
  const arg0 = args[0];
1187
1185
  const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
1188
1186
  const currentRef = metadata.tokenRef;
@@ -1283,14 +1281,14 @@ function OptionalAll(token) {
1283
1281
  *
1284
1282
  * Example:
1285
1283
  * ```ts
1286
- * @ContainerScoped()
1284
+ * @ContainerScoped
1287
1285
  * class Wizard {
1288
1286
  * // ...
1289
1287
  * }
1290
1288
  * ```
1291
1289
  */ // @__NO_SIDE_EFFECTS__
1292
- function ContainerScoped() {
1293
- return scoped("Container", "ContainerScoped");
1290
+ function ContainerScoped(target) {
1291
+ scoped(target, "Container", "ContainerScoped");
1294
1292
  }
1295
1293
  /**
1296
1294
  * Class decorator that registers the decorated type with the **Resolution** scope.
@@ -1300,14 +1298,14 @@ function ContainerScoped() {
1300
1298
  *
1301
1299
  * Example:
1302
1300
  * ```ts
1303
- * @ResolutionScoped()
1301
+ * @ResolutionScoped
1304
1302
  * class Wand {
1305
1303
  * // ...
1306
1304
  * }
1307
1305
  * ```
1308
1306
  */ // @__NO_SIDE_EFFECTS__
1309
- function ResolutionScoped() {
1310
- return scoped("Resolution", "ResolutionScoped");
1307
+ function ResolutionScoped(target) {
1308
+ scoped(target, "Resolution", "ResolutionScoped");
1311
1309
  }
1312
1310
  /**
1313
1311
  * Class decorator that registers the decorated type with the **Transient** scope.
@@ -1316,14 +1314,14 @@ function ResolutionScoped() {
1316
1314
  *
1317
1315
  * Example:
1318
1316
  * ```ts
1319
- * @TransientScoped()
1317
+ * @TransientScoped
1320
1318
  * class Wand {
1321
1319
  * // ...
1322
1320
  * }
1323
1321
  * ```
1324
1322
  */ // @__NO_SIDE_EFFECTS__
1325
- function TransientScoped() {
1326
- return scoped("Transient", "TransientScoped");
1323
+ function TransientScoped(target) {
1324
+ scoped(target, "Transient", "TransientScoped");
1327
1325
  }
1328
1326
  /**
1329
1327
  * Class decorator for setting the scope of the decorated type when registering it.
@@ -1346,22 +1344,20 @@ function TransientScoped() {
1346
1344
  * ```
1347
1345
  */ // @__NO_SIDE_EFFECTS__
1348
1346
  function Scoped(scope) {
1349
- return scoped(scope, "Scoped");
1350
- }
1351
- function scoped(scope, decorator) {
1352
- return (Class)=>{
1353
- const metadata = getMetadata(Class);
1354
- const currentScope = metadata.scope;
1355
- check(!currentScope || currentScope.value === scope, ()=>{
1356
- const { value, appliedBy } = currentScope;
1357
- const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1358
- const className = getTokenName(Class);
1359
- return `class ${className}: scope ${value} was already set by @${by},\n ` + `but @${decorator} is trying to set a conflicting scope ${scope}.\n ` + `Only one decorator should set the class scope, or all must use the same value.`;
1360
- });
1361
- metadata.scope = {
1362
- value: scope,
1363
- appliedBy: decorator
1364
- };
1347
+ return (target)=>scoped(target, scope, "Scoped");
1348
+ }
1349
+ function scoped(target, scope, decorator) {
1350
+ const metadata = getMetadata(target);
1351
+ const currentScope = metadata.scope;
1352
+ check(!currentScope || currentScope.value === scope, ()=>{
1353
+ const { value, appliedBy } = currentScope;
1354
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1355
+ const className = getTokenName(target);
1356
+ return `class ${className}: scope ${value} was already set by @${by},\n ` + `but @${decorator} is trying to set a conflicting scope ${scope}.\n ` + `Only one decorator should set the class scope, or all must use the same value.`;
1357
+ });
1358
+ metadata.scope = {
1359
+ value: scope,
1360
+ appliedBy: decorator
1365
1361
  };
1366
1362
  }
1367
1363