@lppedd/di-wise-neo 0.24.0 → 0.26.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.
@@ -75,6 +75,10 @@ interface Type<T> extends ParameterDecorator {
75
75
  * An injectable type `T` with a default {@link Provider} and optional default registration options.
76
76
  */
77
77
  interface ProviderType<T> extends Type<T> {
78
+ /**
79
+ * The underlying `Type<T>`.
80
+ */
81
+ readonly type: Type<T>;
78
82
  /**
79
83
  * The type's default provider.
80
84
  */
@@ -399,7 +403,7 @@ interface Container {
399
403
  /**
400
404
  * Registers a {@link ClassProvider}, using the class itself as its token.
401
405
  *
402
- * Tokens provided via the {@link Injectable} decorator applied to the class
406
+ * Tokens provided via the {@link Alias} decorator applied to the class
403
407
  * are also registered as aliases.
404
408
  *
405
409
  * The scope is determined by the {@link Scoped} decorator - if present -
@@ -658,22 +662,6 @@ declare function createContainer(options?: ContainerOptions): Container;
658
662
  * ```
659
663
  */
660
664
  declare function AutoRegister<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
661
- /**
662
- * Class decorator that enables auto-registration of an unregistered class
663
- * when the class is first resolved from the container.
664
- *
665
- * Example:
666
- * ```ts
667
- * @AutoRegister()
668
- * class Wizard {}
669
- *
670
- * const wizard = container.resolve(Wizard);
671
- * container.isRegistered(Wizard); // => true
672
- * ```
673
- *
674
- * @deprecated Use `@AutoRegister` instead of `AutoRegister()`.
675
- */
676
- declare function AutoRegister<This extends object>(): ClassDecorator<This>;
677
665
 
678
666
  /**
679
667
  * Class decorator that sets the class scope to **Container** and enables
@@ -695,28 +683,6 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
695
683
  * ```
696
684
  */
697
685
  declare function EagerInstantiate<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
698
- /**
699
- * Class decorator that sets the class scope to **Container** and enables
700
- * eager instantiation when the class is registered in the container.
701
- *
702
- * This causes the container to create and cache the instance of the class
703
- * immediately, instead of deferring it until the first resolution.
704
- *
705
- * If the class cannot be resolved at registration time, registration fails.
706
- *
707
- * Example:
708
- * ```ts
709
- * @EagerInstantiate()
710
- * class Wizard {}
711
- *
712
- * // Wizard is registered with Container scope, and an instance
713
- * // is created and cached immediately by the container
714
- * container.register(Wizard);
715
- * ```
716
- *
717
- * @deprecated Use `@EagerInstantiate` instead of `@EagerInstantiate()`.
718
- */
719
- declare function EagerInstantiate<This extends object>(): ClassDecorator<This>;
720
686
 
721
687
  /**
722
688
  * Parameter decorator that injects the instance associated with the given class.
@@ -769,25 +735,25 @@ declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
769
735
  *
770
736
  * Example:
771
737
  * ```ts
772
- * @Injectable(Weapon)
738
+ * @Alias(Weapon)
773
739
  * class Rifle {}
774
740
  * ```
775
741
  *
776
- * Note that `@Injectable` decorators can be stacked to add multiple aliases.
742
+ * Note that `@Alias` decorators can be stacked to add multiple aliases.
777
743
  *
778
744
  * Example:
779
745
  * ```ts
780
- * @Injectable(Weapon)
781
- * @Injectable(Gun) // Or just @Injectable(Weapon, Gun)
746
+ * @Alias(Weapon)
747
+ * @Alias(Gun) // Or just @Alias(Weapon, Gun)
782
748
  * class Rifle {}
783
749
  * ```
784
750
  */
785
- declare function Injectable<Value, This extends Value & object>(token: Token<Value>): ClassDecorator<This>;
786
- declare function Injectable<VA, VB, This extends VA & VB & object>(tokenA: Token<VA>, tokenB: Token<VB>): ClassDecorator<This>;
787
- declare function Injectable<VA, VB, VC, This extends VA & VB & VC & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>): ClassDecorator<This>;
788
- declare function Injectable<VA, VB, VC, VD, This extends VA & VB & VC & VD & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>): ClassDecorator<This>;
789
- declare function Injectable<VA, VB, VC, VD, VE, This extends VA & VB & VC & VD & VE & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>): ClassDecorator<This>;
790
- declare function Injectable<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC & VD & VE & VF & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>, tokenF: Token<VF>): ClassDecorator<This>;
751
+ declare function Alias<Value, This extends Value & object>(token: Token<Value>): ClassDecorator<This>;
752
+ declare function Alias<VA, VB, This extends VA & VB & object>(tokenA: Token<VA>, tokenB: Token<VB>): ClassDecorator<This>;
753
+ declare function Alias<VA, VB, VC, This extends VA & VB & VC & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>): ClassDecorator<This>;
754
+ declare function Alias<VA, VB, VC, VD, This extends VA & VB & VC & VD & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>): ClassDecorator<This>;
755
+ declare function Alias<VA, VB, VC, VD, VE, This extends VA & VB & VC & VD & VE & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>): ClassDecorator<This>;
756
+ declare function Alias<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC & VD & VE & VF & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>, tokenF: Token<VF>): ClassDecorator<This>;
791
757
  /**
792
758
  * Class decorator that registers an additional aliasing token for the decorated type.
793
759
  *
@@ -799,22 +765,26 @@ declare function Injectable<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC &
799
765
  *
800
766
  * Example:
801
767
  * ```ts
802
- * @Injectable(tokenRef(() => Weapon)) // Weapon is declared after Rifle
768
+ * @Alias(tokenRef(() => Weapon)) // Weapon is declared after Rifle
803
769
  * class Rifle {}
804
770
  * // Other code...
805
771
  * const Weapon = createType("Weapon");
806
772
  * ```
807
773
  *
808
- * Note that `@Injectable` decorators can be stacked to add multiple aliases.
774
+ * Note that `@Alias` decorators can be stacked to add multiple aliases.
809
775
  *
810
776
  * Example:
811
777
  * ```ts
812
- * @Injectable(tokenRef(() => Weapon))
813
- * @Injectable(Gun)
778
+ * @Alias(tokenRef(() => Weapon))
779
+ * @Alias(Gun)
814
780
  * class Rifle {}
815
781
  * ```
816
782
  */
817
- declare function Injectable<Value, This extends Value & object>(tokens: TokenRef<Value>): ClassDecorator<This>;
783
+ declare function Alias<Value, This extends Value & object>(tokens: TokenRef<Value>): ClassDecorator<This>;
784
+ /**
785
+ * @deprecated Use {@link Alias} instead.
786
+ */
787
+ declare const Injectable: typeof Alias;
818
788
 
819
789
  /**
820
790
  * Parameter decorator that injects all instances provided by the registrations
@@ -964,23 +934,6 @@ declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator
964
934
  * ```
965
935
  */
966
936
  declare function ContainerScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
967
- /**
968
- * Class decorator that registers the decorated type with the **Container** scope.
969
- *
970
- * Use this scope when you want one cached instance per container,
971
- * with parent-container lookup fallback.
972
- *
973
- * Example:
974
- * ```ts
975
- * @ContainerScoped()
976
- * class Wizard {
977
- * // ...
978
- * }
979
- * ```
980
- *
981
- * @deprecated Use `@ContainerScoped` instead of `@ContainerScoped()`.
982
- */
983
- declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
984
937
  /**
985
938
  * Class decorator that registers the decorated type with the **Resolution** scope.
986
939
  *
@@ -996,23 +949,6 @@ declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
996
949
  * ```
997
950
  */
998
951
  declare function ResolutionScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
999
- /**
1000
- * Class decorator that registers the decorated type with the **Resolution** scope.
1001
- *
1002
- * Use this scope when you want one cached instance per resolution graph,
1003
- * so repeated resolutions within the same request reuse the same value.
1004
- *
1005
- * Example:
1006
- * ```ts
1007
- * @ResolutionScoped()
1008
- * class Wand {
1009
- * // ...
1010
- * }
1011
- * ```
1012
- *
1013
- * @deprecated Use `@ResolutionScoped` instead of `@ResolutionScoped()`.
1014
- */
1015
- declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
1016
952
  /**
1017
953
  * Class decorator that registers the decorated type with the **Transient** scope.
1018
954
  *
@@ -1027,22 +963,6 @@ declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
1027
963
  * ```
1028
964
  */
1029
965
  declare function TransientScoped<This extends object, Ctor extends Constructor<This>>(target: Ctor): void;
1030
- /**
1031
- * Class decorator that registers the decorated type with the **Transient** scope.
1032
- *
1033
- * Use this scope when you want a fresh instance every time the class is resolved.
1034
- *
1035
- * Example:
1036
- * ```ts
1037
- * @TransientScoped()
1038
- * class Wand {
1039
- * // ...
1040
- * }
1041
- * ```
1042
- *
1043
- * @deprecated Use `@TransientScoped` instead of `@TransientScoped()`.
1044
- */
1045
- declare function TransientScoped<This extends object>(): ClassDecorator<This>;
1046
966
  /**
1047
967
  * Class decorator for setting the scope of the decorated type when registering it.
1048
968
  *
@@ -1333,5 +1253,5 @@ declare function optionalBy<Instance extends object>(thisArg: object, Class: Con
1333
1253
  */
1334
1254
  declare function optionalBy<Value>(thisArg: object, token: Token<Value>, name?: string): Value | undefined;
1335
1255
 
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 };
1256
+ export { Alias, 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 };
1337
1257
  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
@@ -453,6 +453,7 @@ function createType(debugName, provider, options) {
453
453
  type.__type = undefined;
454
454
  type.toString = ()=>name;
455
455
  if (provider) {
456
+ type.type = type;
456
457
  type.provider = provider;
457
458
  type.options = options;
458
459
  }
@@ -790,10 +791,10 @@ function isDisposable(value) {
790
791
  };
791
792
  // Register the class itself
792
793
  this.myTokenRegistry.put(Class, registration);
793
- // Register the additional tokens specified via class decorators.
794
+ // Register additional aliasing tokens specified via class decorators.
794
795
  // These tokens will point to the original Class token and will have the same scope.
795
- for (const token of metadata.tokenRef.getRefTokens()){
796
- this.myTokenRegistry.put(token, {
796
+ for (const aliasToken of metadata.tokenRef.getRefTokens()){
797
+ this.myTokenRegistry.put(aliasToken, {
797
798
  name: name,
798
799
  provider: {
799
800
  useExisting: [
@@ -827,6 +828,19 @@ function isDisposable(value) {
827
828
  dependencies: metadata.dependencies
828
829
  };
829
830
  this.myTokenRegistry.put(token, registration);
831
+ // Register additional aliasing tokens specified via class decorators.
832
+ // These tokens will point to the original token and will have the same scope.
833
+ for (const aliasToken of metadata.tokenRef.getRefTokens()){
834
+ this.myTokenRegistry.put(aliasToken, {
835
+ name: name,
836
+ provider: {
837
+ useExisting: [
838
+ token,
839
+ name
840
+ ]
841
+ }
842
+ });
843
+ }
830
844
  // Eager-instantiate only if the provided class is container-scoped.
831
845
  // Note that we are comparing the scope using the registration configured just above,
832
846
  // which takes into account both the metadata and the container option as a fallback.
@@ -1115,33 +1129,57 @@ function isDisposable(value) {
1115
1129
  return new ContainerImpl(undefined, options);
1116
1130
  }
1117
1131
 
1118
- // @__NO_SIDE_EFFECTS__
1132
+ /**
1133
+ * Class decorator that enables auto-registration of an unregistered class
1134
+ * when the class is first resolved from the container.
1135
+ *
1136
+ * Example:
1137
+ * ```ts
1138
+ * @AutoRegister
1139
+ * class Wizard {}
1140
+ *
1141
+ * const wizard = container.resolve(Wizard);
1142
+ * container.isRegistered(Wizard); // => true
1143
+ * ```
1144
+ */ // @__NO_SIDE_EFFECTS__
1119
1145
  function AutoRegister(target) {
1120
- const decorator = (Class)=>{
1121
- const metadata = getMetadata(Class);
1122
- metadata.autoRegister = true;
1123
- };
1124
- return target === undefined ? decorator : decorator(target);
1146
+ const metadata = getMetadata(target);
1147
+ metadata.autoRegister = true;
1125
1148
  }
1126
1149
 
1127
- // @__NO_SIDE_EFFECTS__
1150
+ /**
1151
+ * Class decorator that sets the class scope to **Container** and enables
1152
+ * eager instantiation when the class is registered in the container.
1153
+ *
1154
+ * This causes the container to create and cache the instance of the class
1155
+ * immediately, instead of deferring it until the first resolution.
1156
+ *
1157
+ * If the class cannot be resolved at registration time, registration fails.
1158
+ *
1159
+ * Example:
1160
+ * ```ts
1161
+ * @EagerInstantiate
1162
+ * class Wizard {}
1163
+ *
1164
+ * // Wizard is registered with Container scope, and an instance
1165
+ * // is created and cached immediately by the container
1166
+ * container.register(Wizard);
1167
+ * ```
1168
+ */ // @__NO_SIDE_EFFECTS__
1128
1169
  function EagerInstantiate(target) {
1129
- const decorator = (Class)=>{
1130
- const metadata = getMetadata(Class);
1131
- const currentScope = metadata.scope;
1132
- check(!currentScope || currentScope.value === "Container", ()=>{
1133
- const { value, appliedBy } = currentScope;
1134
- const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1135
- const className = getTokenName(Class);
1136
- 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.`;
1137
- });
1138
- metadata.eagerInstantiate = true;
1139
- metadata.scope = {
1140
- value: "Container",
1141
- appliedBy: "EagerInstantiate"
1142
- };
1170
+ const metadata = getMetadata(target);
1171
+ const currentScope = metadata.scope;
1172
+ check(!currentScope || currentScope.value === "Container", ()=>{
1173
+ const { value, appliedBy } = currentScope;
1174
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1175
+ const className = getTokenName(target);
1176
+ 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.`;
1177
+ });
1178
+ metadata.eagerInstantiate = true;
1179
+ metadata.scope = {
1180
+ value: "Container",
1181
+ appliedBy: "EagerInstantiate"
1143
1182
  };
1144
- return target === undefined ? decorator : decorator(target);
1145
1183
  }
1146
1184
 
1147
1185
  // @__NO_SIDE_EFFECTS__
@@ -1156,9 +1194,9 @@ function Inject(token) {
1156
1194
  }
1157
1195
 
1158
1196
  // @__NO_SIDE_EFFECTS__
1159
- function Injectable(...args) {
1160
- return (Class)=>{
1161
- const metadata = getMetadata(Class);
1197
+ function Alias(...args) {
1198
+ return (target)=>{
1199
+ const metadata = getMetadata(target);
1162
1200
  const arg0 = args[0];
1163
1201
  const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
1164
1202
  const currentRef = metadata.tokenRef;
@@ -1174,6 +1212,9 @@ function Injectable(...args) {
1174
1212
  };
1175
1213
  };
1176
1214
  }
1215
+ /**
1216
+ * @deprecated Use {@link Alias} instead.
1217
+ */ const Injectable = Alias;
1177
1218
 
1178
1219
  // @__NO_SIDE_EFFECTS__
1179
1220
  function InjectAll(token) {
@@ -1251,20 +1292,55 @@ function OptionalAll(token) {
1251
1292
  };
1252
1293
  }
1253
1294
 
1254
- // @__NO_SIDE_EFFECTS__
1295
+ /**
1296
+ * Class decorator that registers the decorated type with the **Container** scope.
1297
+ *
1298
+ * Use this scope when you want one cached instance per container,
1299
+ * with parent-container lookup fallback.
1300
+ *
1301
+ * Example:
1302
+ * ```ts
1303
+ * @ContainerScoped
1304
+ * class Wizard {
1305
+ * // ...
1306
+ * }
1307
+ * ```
1308
+ */ // @__NO_SIDE_EFFECTS__
1255
1309
  function ContainerScoped(target) {
1256
- const decorator = scoped("Container", "ContainerScoped");
1257
- return target === undefined ? decorator : decorator(target);
1310
+ scoped(target, "Container", "ContainerScoped");
1258
1311
  }
1259
- // @__NO_SIDE_EFFECTS__
1312
+ /**
1313
+ * Class decorator that registers the decorated type with the **Resolution** scope.
1314
+ *
1315
+ * Use this scope when you want one cached instance per resolution graph,
1316
+ * so repeated resolutions within the same request reuse the same value.
1317
+ *
1318
+ * Example:
1319
+ * ```ts
1320
+ * @ResolutionScoped
1321
+ * class Wand {
1322
+ * // ...
1323
+ * }
1324
+ * ```
1325
+ */ // @__NO_SIDE_EFFECTS__
1260
1326
  function ResolutionScoped(target) {
1261
- const decorator = scoped("Resolution", "ResolutionScoped");
1262
- return target === undefined ? decorator : decorator(target);
1327
+ scoped(target, "Resolution", "ResolutionScoped");
1263
1328
  }
1264
- // @__NO_SIDE_EFFECTS__
1329
+ /**
1330
+ * Class decorator that registers the decorated type with the **Transient** scope.
1331
+ *
1332
+ * Use this scope when you want a fresh instance every time the class is resolved.
1333
+ *
1334
+ * Example:
1335
+ * ```ts
1336
+ * @TransientScoped
1337
+ * class Wand {
1338
+ * // ...
1339
+ * }
1340
+ * ```
1341
+ */ // @__NO_SIDE_EFFECTS__
1265
1342
  function TransientScoped(target) {
1266
- const decorator = scoped("Transient", "TransientScoped");
1267
- return target === undefined ? decorator : decorator(target);
1343
+ scoped(target, "Transient", "TransientScoped");
1268
1344
  }
1269
1345
  /**
1270
1346
  * Class decorator for setting the scope of the decorated type when registering it.
@@ -1287,22 +1363,20 @@ function TransientScoped(target) {
1287
1363
  * ```
1288
1364
  */ // @__NO_SIDE_EFFECTS__
1289
1365
  function Scoped(scope) {
1290
- return scoped(scope, "Scoped");
1291
- }
1292
- function scoped(scope, decorator) {
1293
- return (Class)=>{
1294
- const metadata = getMetadata(Class);
1295
- const currentScope = metadata.scope;
1296
- check(!currentScope || currentScope.value === scope, ()=>{
1297
- const { value, appliedBy } = currentScope;
1298
- const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1299
- const className = getTokenName(Class);
1300
- 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.`;
1301
- });
1302
- metadata.scope = {
1303
- value: scope,
1304
- appliedBy: decorator
1305
- };
1366
+ return (target)=>scoped(target, scope, "Scoped");
1367
+ }
1368
+ function scoped(target, scope, decorator) {
1369
+ const metadata = getMetadata(target);
1370
+ const currentScope = metadata.scope;
1371
+ check(!currentScope || currentScope.value === scope, ()=>{
1372
+ const { value, appliedBy } = currentScope;
1373
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1374
+ const className = getTokenName(target);
1375
+ 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.`;
1376
+ });
1377
+ metadata.scope = {
1378
+ value: scope,
1379
+ appliedBy: decorator
1306
1380
  };
1307
1381
  }
1308
1382
 
@@ -1364,6 +1438,7 @@ const Scope = {
1364
1438
  */ Container: "Container"
1365
1439
  };
1366
1440
 
1441
+ exports.Alias = Alias;
1367
1442
  exports.AutoRegister = AutoRegister;
1368
1443
  exports.ContainerScoped = ContainerScoped;
1369
1444
  exports.EagerInstantiate = EagerInstantiate;