@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/es/index.mjs CHANGED
@@ -451,6 +451,7 @@ function createType(debugName, provider, options) {
451
451
  type.__type = undefined;
452
452
  type.toString = ()=>name;
453
453
  if (provider) {
454
+ type.type = type;
454
455
  type.provider = provider;
455
456
  type.options = options;
456
457
  }
@@ -788,10 +789,10 @@ function isDisposable(value) {
788
789
  };
789
790
  // Register the class itself
790
791
  this.myTokenRegistry.put(Class, registration);
791
- // Register the additional tokens specified via class decorators.
792
+ // Register additional aliasing tokens specified via class decorators.
792
793
  // These tokens will point to the original Class token and will have the same scope.
793
- for (const token of metadata.tokenRef.getRefTokens()){
794
- this.myTokenRegistry.put(token, {
794
+ for (const aliasToken of metadata.tokenRef.getRefTokens()){
795
+ this.myTokenRegistry.put(aliasToken, {
795
796
  name: name,
796
797
  provider: {
797
798
  useExisting: [
@@ -825,6 +826,19 @@ function isDisposable(value) {
825
826
  dependencies: metadata.dependencies
826
827
  };
827
828
  this.myTokenRegistry.put(token, registration);
829
+ // Register additional aliasing tokens specified via class decorators.
830
+ // These tokens will point to the original token and will have the same scope.
831
+ for (const aliasToken of metadata.tokenRef.getRefTokens()){
832
+ this.myTokenRegistry.put(aliasToken, {
833
+ name: name,
834
+ provider: {
835
+ useExisting: [
836
+ token,
837
+ name
838
+ ]
839
+ }
840
+ });
841
+ }
828
842
  // Eager-instantiate only if the provided class is container-scoped.
829
843
  // Note that we are comparing the scope using the registration configured just above,
830
844
  // which takes into account both the metadata and the container option as a fallback.
@@ -1113,33 +1127,57 @@ function isDisposable(value) {
1113
1127
  return new ContainerImpl(undefined, options);
1114
1128
  }
1115
1129
 
1116
- // @__NO_SIDE_EFFECTS__
1130
+ /**
1131
+ * Class decorator that enables auto-registration of an unregistered class
1132
+ * when the class is first resolved from the container.
1133
+ *
1134
+ * Example:
1135
+ * ```ts
1136
+ * @AutoRegister
1137
+ * class Wizard {}
1138
+ *
1139
+ * const wizard = container.resolve(Wizard);
1140
+ * container.isRegistered(Wizard); // => true
1141
+ * ```
1142
+ */ // @__NO_SIDE_EFFECTS__
1117
1143
  function AutoRegister(target) {
1118
- const decorator = (Class)=>{
1119
- const metadata = getMetadata(Class);
1120
- metadata.autoRegister = true;
1121
- };
1122
- return target === undefined ? decorator : decorator(target);
1144
+ const metadata = getMetadata(target);
1145
+ metadata.autoRegister = true;
1123
1146
  }
1124
1147
 
1125
- // @__NO_SIDE_EFFECTS__
1148
+ /**
1149
+ * Class decorator that sets the class scope to **Container** and enables
1150
+ * eager instantiation when the class is registered in the container.
1151
+ *
1152
+ * This causes the container to create and cache the instance of the class
1153
+ * immediately, instead of deferring it until the first resolution.
1154
+ *
1155
+ * If the class cannot be resolved at registration time, registration fails.
1156
+ *
1157
+ * Example:
1158
+ * ```ts
1159
+ * @EagerInstantiate
1160
+ * class Wizard {}
1161
+ *
1162
+ * // Wizard is registered with Container scope, and an instance
1163
+ * // is created and cached immediately by the container
1164
+ * container.register(Wizard);
1165
+ * ```
1166
+ */ // @__NO_SIDE_EFFECTS__
1126
1167
  function EagerInstantiate(target) {
1127
- const decorator = (Class)=>{
1128
- const metadata = getMetadata(Class);
1129
- const currentScope = metadata.scope;
1130
- check(!currentScope || currentScope.value === "Container", ()=>{
1131
- const { value, appliedBy } = currentScope;
1132
- const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1133
- const className = getTokenName(Class);
1134
- 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.`;
1135
- });
1136
- metadata.eagerInstantiate = true;
1137
- metadata.scope = {
1138
- value: "Container",
1139
- appliedBy: "EagerInstantiate"
1140
- };
1168
+ const metadata = getMetadata(target);
1169
+ const currentScope = metadata.scope;
1170
+ check(!currentScope || currentScope.value === "Container", ()=>{
1171
+ const { value, appliedBy } = currentScope;
1172
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1173
+ const className = getTokenName(target);
1174
+ 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.`;
1175
+ });
1176
+ metadata.eagerInstantiate = true;
1177
+ metadata.scope = {
1178
+ value: "Container",
1179
+ appliedBy: "EagerInstantiate"
1141
1180
  };
1142
- return target === undefined ? decorator : decorator(target);
1143
1181
  }
1144
1182
 
1145
1183
  // @__NO_SIDE_EFFECTS__
@@ -1154,9 +1192,9 @@ function Inject(token) {
1154
1192
  }
1155
1193
 
1156
1194
  // @__NO_SIDE_EFFECTS__
1157
- function Injectable(...args) {
1158
- return (Class)=>{
1159
- const metadata = getMetadata(Class);
1195
+ function Alias(...args) {
1196
+ return (target)=>{
1197
+ const metadata = getMetadata(target);
1160
1198
  const arg0 = args[0];
1161
1199
  const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
1162
1200
  const currentRef = metadata.tokenRef;
@@ -1172,6 +1210,9 @@ function Injectable(...args) {
1172
1210
  };
1173
1211
  };
1174
1212
  }
1213
+ /**
1214
+ * @deprecated Use {@link Alias} instead.
1215
+ */ const Injectable = Alias;
1175
1216
 
1176
1217
  // @__NO_SIDE_EFFECTS__
1177
1218
  function InjectAll(token) {
@@ -1249,20 +1290,55 @@ function OptionalAll(token) {
1249
1290
  };
1250
1291
  }
1251
1292
 
1252
- // @__NO_SIDE_EFFECTS__
1293
+ /**
1294
+ * Class decorator that registers the decorated type with the **Container** scope.
1295
+ *
1296
+ * Use this scope when you want one cached instance per container,
1297
+ * with parent-container lookup fallback.
1298
+ *
1299
+ * Example:
1300
+ * ```ts
1301
+ * @ContainerScoped
1302
+ * class Wizard {
1303
+ * // ...
1304
+ * }
1305
+ * ```
1306
+ */ // @__NO_SIDE_EFFECTS__
1253
1307
  function ContainerScoped(target) {
1254
- const decorator = scoped("Container", "ContainerScoped");
1255
- return target === undefined ? decorator : decorator(target);
1308
+ scoped(target, "Container", "ContainerScoped");
1256
1309
  }
1257
- // @__NO_SIDE_EFFECTS__
1310
+ /**
1311
+ * Class decorator that registers the decorated type with the **Resolution** scope.
1312
+ *
1313
+ * Use this scope when you want one cached instance per resolution graph,
1314
+ * so repeated resolutions within the same request reuse the same value.
1315
+ *
1316
+ * Example:
1317
+ * ```ts
1318
+ * @ResolutionScoped
1319
+ * class Wand {
1320
+ * // ...
1321
+ * }
1322
+ * ```
1323
+ */ // @__NO_SIDE_EFFECTS__
1258
1324
  function ResolutionScoped(target) {
1259
- const decorator = scoped("Resolution", "ResolutionScoped");
1260
- return target === undefined ? decorator : decorator(target);
1325
+ scoped(target, "Resolution", "ResolutionScoped");
1261
1326
  }
1262
- // @__NO_SIDE_EFFECTS__
1327
+ /**
1328
+ * Class decorator that registers the decorated type with the **Transient** scope.
1329
+ *
1330
+ * Use this scope when you want a fresh instance every time the class is resolved.
1331
+ *
1332
+ * Example:
1333
+ * ```ts
1334
+ * @TransientScoped
1335
+ * class Wand {
1336
+ * // ...
1337
+ * }
1338
+ * ```
1339
+ */ // @__NO_SIDE_EFFECTS__
1263
1340
  function TransientScoped(target) {
1264
- const decorator = scoped("Transient", "TransientScoped");
1265
- return target === undefined ? decorator : decorator(target);
1341
+ scoped(target, "Transient", "TransientScoped");
1266
1342
  }
1267
1343
  /**
1268
1344
  * Class decorator for setting the scope of the decorated type when registering it.
@@ -1285,22 +1361,20 @@ function TransientScoped(target) {
1285
1361
  * ```
1286
1362
  */ // @__NO_SIDE_EFFECTS__
1287
1363
  function Scoped(scope) {
1288
- return scoped(scope, "Scoped");
1289
- }
1290
- function scoped(scope, decorator) {
1291
- return (Class)=>{
1292
- const metadata = getMetadata(Class);
1293
- const currentScope = metadata.scope;
1294
- check(!currentScope || currentScope.value === scope, ()=>{
1295
- const { value, appliedBy } = currentScope;
1296
- const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1297
- const className = getTokenName(Class);
1298
- 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.`;
1299
- });
1300
- metadata.scope = {
1301
- value: scope,
1302
- appliedBy: decorator
1303
- };
1364
+ return (target)=>scoped(target, scope, "Scoped");
1365
+ }
1366
+ function scoped(target, scope, decorator) {
1367
+ const metadata = getMetadata(target);
1368
+ const currentScope = metadata.scope;
1369
+ check(!currentScope || currentScope.value === scope, ()=>{
1370
+ const { value, appliedBy } = currentScope;
1371
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1372
+ const className = getTokenName(target);
1373
+ 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.`;
1374
+ });
1375
+ metadata.scope = {
1376
+ value: scope,
1377
+ appliedBy: decorator
1304
1378
  };
1305
1379
  }
1306
1380
 
@@ -1362,5 +1436,5 @@ const Scope = {
1362
1436
  */ Container: "Container"
1363
1437
  };
1364
1438
 
1365
- 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 };
1439
+ 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 };
1366
1440
  //# sourceMappingURL=index.mjs.map