@lppedd/di-wise-neo 0.22.0 → 0.23.1

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.
@@ -682,8 +682,10 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
682
682
  * Class decorator that sets the class scope to **Container** and enables
683
683
  * eager instantiation when the class is registered in the container.
684
684
  *
685
- * This causes the container to immediately create and cache the instance of the class,
686
- * instead of deferring instantiation until the first resolution.
685
+ * This causes the container to create and cache the instance of the class
686
+ * immediately, instead of deferring it until the first resolution.
687
+ *
688
+ * If the class cannot be resolved at registration time, registration fails.
687
689
  *
688
690
  * Example:
689
691
  * ```ts
@@ -691,7 +693,7 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
691
693
  * class Wizard {}
692
694
  *
693
695
  * // Wizard is registered with Container scope, and an instance
694
- * // is immediately created and cached by the container
696
+ * // is created and cached immediately by the container
695
697
  * container.register(Wizard);
696
698
  * ```
697
699
  */
@@ -702,8 +704,9 @@ declare function EagerInstantiate<This extends object>(): ClassDecorator<This>;
702
704
  *
703
705
  * Throws an error if:
704
706
  * - The class is not registered in the container.
705
- * - A circular dependency is detected. Use function injection with {@link injectBy}
706
- * if resolving circular dependencies is necessary.
707
+ * - A circular dependency is detected.
708
+ *
709
+ * Use {@link injectBy} when you need to resolve circular dependencies.
707
710
  */
708
711
  declare function Inject<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
709
712
  /**
@@ -711,8 +714,9 @@ declare function Inject<Instance extends object>(Class: Constructor<Instance>):
711
714
  *
712
715
  * Throws an error if:
713
716
  * - The token is not registered in the container.
714
- * - A circular dependency is detected. Use function injection with {@link injectBy}
715
- * if resolving circular dependencies is necessary.
717
+ * - A circular dependency is detected.
718
+ *
719
+ * Use {@link injectBy} when you need to resolve circular dependencies.
716
720
  */
717
721
  declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
718
722
  /**
@@ -723,8 +727,9 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
723
727
  *
724
728
  * Throws an error if:
725
729
  * - The token is not registered in the container.
726
- * - A circular dependency is detected. Use function injection with {@link injectBy}
727
- * if resolving circular dependencies is necessary.
730
+ * - A circular dependency is detected.
731
+ *
732
+ * Use function injection with {@link injectBy} when you need to resolve circular dependencies.
728
733
  *
729
734
  * Example:
730
735
  * ```ts
@@ -833,17 +838,18 @@ declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
833
838
  declare function InjectAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
834
839
 
835
840
  /**
836
- * Qualifies a class or an injected parameter with a unique name.
841
+ * Qualifies a class or an injected parameter with a name.
837
842
  *
838
- * This allows the container to distinguish between multiple implementations
839
- * of the same interface or type during registration and injection.
843
+ * On a class, the name is used when registering that class with the container.
844
+ * On a parameter, the name selects a named registration or alias during injection.
845
+ *
846
+ * This is useful when multiple registrations exist for the same token.
840
847
  *
841
848
  * Example:
842
849
  * ```ts
843
850
  * @Named("dumbledore")
844
851
  * class Dumbledore implements Wizard {}
845
852
  *
846
- * // Register Dumbledore with Type<Wizard>
847
853
  * container.register(IWizard, { useClass: Dumbledore });
848
854
  * const dumbledore = container.resolve(IWizard, "dumbledore");
849
855
  * ```
@@ -855,7 +861,7 @@ declare function Named<This extends object>(name: string): ClassDecorator<This>
855
861
  * or `undefined` if the class is not registered in the container.
856
862
  *
857
863
  * Throws an error if a circular dependency is detected. Use function injection
858
- * with {@link optionalBy} if resolving circular dependencies is necessary.
864
+ * with {@link optionalBy} when you need to resolve circular dependencies.
859
865
  */
860
866
  declare function Optional<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
861
867
  /**
@@ -863,7 +869,7 @@ declare function Optional<Instance extends object>(Class: Constructor<Instance>)
863
869
  * or `undefined` if the token is not registered in the container.
864
870
  *
865
871
  * Throws an error if a circular dependency is detected. Use function injection
866
- * with {@link optionalBy} if resolving circular dependencies is necessary.
872
+ * with {@link optionalBy} when you need to resolve circular dependencies.
867
873
  */
868
874
  declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
869
875
  /**
@@ -874,7 +880,7 @@ declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
874
880
  * the {@link tokenRef} helper function.
875
881
  *
876
882
  * Throws an error if a circular dependency is detected. Use function injection
877
- * with {@link optionalBy} if resolving circular dependencies is necessary.
883
+ * with {@link optionalBy} when you need to resolve circular dependencies.
878
884
  *
879
885
  * Example:
880
886
  * ```ts
@@ -924,6 +930,50 @@ declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
924
930
  */
925
931
  declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
926
932
 
933
+ /**
934
+ * Class decorator that registers the decorated type with the **Container** scope.
935
+ *
936
+ * Use this scope when you want one cached instance per container,
937
+ * with parent-container lookup fallback.
938
+ *
939
+ * Example:
940
+ * ```ts
941
+ * @ContainerScoped()
942
+ * class Wizard {
943
+ * // ...
944
+ * }
945
+ * ```
946
+ */
947
+ declare function ContainerScoped<This extends object>(): ClassDecorator<This>;
948
+ /**
949
+ * Class decorator that registers the decorated type with the **Resolution** scope.
950
+ *
951
+ * Use this scope when you want one cached instance per resolution graph,
952
+ * so repeated resolutions within the same request reuse the same value.
953
+ *
954
+ * Example:
955
+ * ```ts
956
+ * @ResolutionScoped()
957
+ * class Wand {
958
+ * // ...
959
+ * }
960
+ * ```
961
+ */
962
+ declare function ResolutionScoped<This extends object>(): ClassDecorator<This>;
963
+ /**
964
+ * Class decorator that registers the decorated type with the **Transient** scope.
965
+ *
966
+ * Use this scope when you want a fresh instance every time the class is resolved.
967
+ *
968
+ * Example:
969
+ * ```ts
970
+ * @TransientScoped()
971
+ * class Wand {
972
+ * // ...
973
+ * }
974
+ * ```
975
+ */
976
+ declare function TransientScoped<This extends object>(): ClassDecorator<This>;
927
977
  /**
928
978
  * Class decorator for setting the scope of the decorated type when registering it.
929
979
  *
@@ -1005,7 +1055,7 @@ declare function injectAll<Value>(token: Token<Value>): Value[];
1005
1055
  * @param Class - The class to resolve.
1006
1056
  * @param name - The name qualifier of the class to resolve.
1007
1057
  */
1008
- declare function injectBy<Instance extends object>(thisArg: any, Class: Constructor<Instance>, name?: string): Instance;
1058
+ declare function injectBy<Instance extends object>(thisArg: object, Class: Constructor<Instance>, name?: string): Instance;
1009
1059
  /**
1010
1060
  * Injects the value associated with the given token.
1011
1061
  *
@@ -1029,7 +1079,7 @@ declare function injectBy<Instance extends object>(thisArg: any, Class: Construc
1029
1079
  * @param token - The token to resolve.
1030
1080
  * @param name - The name qualifier of the token to resolve.
1031
1081
  */
1032
- declare function injectBy<Value>(thisArg: any, token: Token<Value>, name?: string): Value;
1082
+ declare function injectBy<Value>(thisArg: object, token: Token<Value>, name?: string): Value;
1033
1083
 
1034
1084
  /**
1035
1085
  * Asserts that the current stack frame is within an injection context,
@@ -1162,7 +1212,7 @@ declare function setClassIdentityMapping<T extends object>(transformedClass: Con
1162
1212
  * or `undefined` if the class is not registered in the container.
1163
1213
  *
1164
1214
  * Throws an error if a circular dependency is detected.
1165
- * Use {@link optionalBy} if resolving circular dependencies is necessary.
1215
+ * Use {@link optionalBy} when you need to resolve circular dependencies.
1166
1216
  */
1167
1217
  declare function optional<Instance extends object>(Class: Constructor<Instance>, name?: string): Instance | undefined;
1168
1218
  /**
@@ -1170,7 +1220,7 @@ declare function optional<Instance extends object>(Class: Constructor<Instance>,
1170
1220
  * or `undefined` if the token is not registered in the container.
1171
1221
  *
1172
1222
  * Throws an error if a circular dependency is detected.
1173
- * Use {@link optionalBy} if resolving circular dependencies is necessary.
1223
+ * Use {@link optionalBy} when you need to resolve circular dependencies.
1174
1224
  */
1175
1225
  declare function optional<Value>(token: Token<Value>, name?: string): Value | undefined;
1176
1226
 
@@ -1200,7 +1250,7 @@ declare function optionalAll<Value>(token: Token<Value>): Value[];
1200
1250
  * @param Class - The class to resolve.
1201
1251
  * @param name - The name qualifier of the class to resolve.
1202
1252
  */
1203
- declare function optionalBy<Instance extends object>(thisArg: any, Class: Constructor<Instance>, name?: string): Instance | undefined;
1253
+ declare function optionalBy<Instance extends object>(thisArg: object, Class: Constructor<Instance>, name?: string): Instance | undefined;
1204
1254
  /**
1205
1255
  * Injects the value associated with the given token,
1206
1256
  * or `undefined` if the token is not registered in the container.
@@ -1212,7 +1262,7 @@ declare function optionalBy<Instance extends object>(thisArg: any, Class: Constr
1212
1262
  * @param token - The token to resolve.
1213
1263
  * @param name - The name qualifier of the token to resolve.
1214
1264
  */
1215
- declare function optionalBy<Value>(thisArg: any, token: Token<Value>, name?: string): Value | undefined;
1265
+ declare function optionalBy<Value>(thisArg: object, token: Token<Value>, name?: string): Value | undefined;
1216
1266
 
1217
- export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, Scope, Scoped, assertInjectionContext, build, classRef, createContainer, createType, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping, tokenRef };
1267
+ 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 };
1218
1268
  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
@@ -441,14 +441,13 @@ function describeParam(target, methodKey, parameterIndex) {
441
441
  // @__NO_SIDE_EFFECTS__
442
442
  function createType(typeName, provider, options) {
443
443
  const name = `Type<${typeName}>`;
444
- const decorator = (target, propertyKey, parameterIndex)=>{
444
+ const type = (target, propertyKey, parameterIndex)=>{
445
445
  updateParameterMetadata(name, target, propertyKey, parameterIndex, (dependency)=>{
446
446
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
447
447
  dependency.appliedBy = "Inject";
448
- dependency.tokenRef = tokenRef(()=>decorator);
448
+ dependency.tokenRef = tokenRef(()=>type);
449
449
  });
450
450
  };
451
- const type = decorator;
452
451
  Object.defineProperty(type, "name", {
453
452
  value: name
454
453
  });
@@ -1128,7 +1127,7 @@ function isDisposable(value) {
1128
1127
  * ```
1129
1128
  */ // @__NO_SIDE_EFFECTS__
1130
1129
  function AutoRegister() {
1131
- return function(Class) {
1130
+ return (Class)=>{
1132
1131
  const metadata = getMetadata(Class);
1133
1132
  metadata.autoRegister = true;
1134
1133
  };
@@ -1138,8 +1137,10 @@ function AutoRegister() {
1138
1137
  * Class decorator that sets the class scope to **Container** and enables
1139
1138
  * eager instantiation when the class is registered in the container.
1140
1139
  *
1141
- * This causes the container to immediately create and cache the instance of the class,
1142
- * instead of deferring instantiation until the first resolution.
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.
1143
1144
  *
1144
1145
  * Example:
1145
1146
  * ```ts
@@ -1147,18 +1148,19 @@ function AutoRegister() {
1147
1148
  * class Wizard {}
1148
1149
  *
1149
1150
  * // Wizard is registered with Container scope, and an instance
1150
- * // is immediately created and cached by the container
1151
+ * // is created and cached immediately by the container
1151
1152
  * container.register(Wizard);
1152
1153
  * ```
1153
1154
  */ // @__NO_SIDE_EFFECTS__
1154
1155
  function EagerInstantiate() {
1155
- return function(Class) {
1156
+ return (Class)=>{
1156
1157
  const metadata = getMetadata(Class);
1157
1158
  const currentScope = metadata.scope;
1158
1159
  check(!currentScope || currentScope.value === "Container", ()=>{
1159
1160
  const { value, appliedBy } = currentScope;
1161
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1160
1162
  const className = getTokenName(Class);
1161
- return `class ${className}: scope ${value} was already set by @${appliedBy},\n ` + `but @EagerInstantiate is trying to set a conflicting scope Container.\n ` + `Only one decorator should set the class scope, or all must agree on the same value.`;
1163
+ 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
1164
  });
1163
1165
  metadata.eagerInstantiate = true;
1164
1166
  metadata.scope = {
@@ -1170,7 +1172,7 @@ function EagerInstantiate() {
1170
1172
 
1171
1173
  // @__NO_SIDE_EFFECTS__
1172
1174
  function Inject(token) {
1173
- return function(target, propertyKey, parameterIndex) {
1175
+ return (target, propertyKey, parameterIndex)=>{
1174
1176
  updateParameterMetadata("Inject", target, propertyKey, parameterIndex, (dependency)=>{
1175
1177
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1176
1178
  dependency.appliedBy = "Inject";
@@ -1181,7 +1183,7 @@ function Inject(token) {
1181
1183
 
1182
1184
  // @__NO_SIDE_EFFECTS__
1183
1185
  function Injectable(...args) {
1184
- return function(Class) {
1186
+ return (Class)=>{
1185
1187
  const metadata = getMetadata(Class);
1186
1188
  const arg0 = args[0];
1187
1189
  const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
@@ -1201,7 +1203,7 @@ function Injectable(...args) {
1201
1203
 
1202
1204
  // @__NO_SIDE_EFFECTS__
1203
1205
  function InjectAll(token) {
1204
- return function(target, propertyKey, parameterIndex) {
1206
+ return (target, propertyKey, parameterIndex)=>{
1205
1207
  updateParameterMetadata("InjectAll", target, propertyKey, parameterIndex, (dependency)=>{
1206
1208
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1207
1209
  dependency.appliedBy = "InjectAll";
@@ -1212,24 +1214,25 @@ function InjectAll(token) {
1212
1214
  }
1213
1215
 
1214
1216
  /**
1215
- * Qualifies a class or an injected parameter with a unique name.
1217
+ * Qualifies a class or an injected parameter with a name.
1218
+ *
1219
+ * On a class, the name is used when registering that class with the container.
1220
+ * On a parameter, the name selects a named registration or alias during injection.
1216
1221
  *
1217
- * This allows the container to distinguish between multiple implementations
1218
- * of the same interface or type during registration and injection.
1222
+ * This is useful when multiple registrations exist for the same token.
1219
1223
  *
1220
1224
  * Example:
1221
1225
  * ```ts
1222
1226
  * @Named("dumbledore")
1223
1227
  * class Dumbledore implements Wizard {}
1224
1228
  *
1225
- * // Register Dumbledore with Type<Wizard>
1226
1229
  * container.register(IWizard, { useClass: Dumbledore });
1227
1230
  * const dumbledore = container.resolve(IWizard, "dumbledore");
1228
1231
  * ```
1229
1232
  */ // @__NO_SIDE_EFFECTS__
1230
1233
  function Named(name) {
1231
1234
  check(name.trim(), "@Named qualifier must not be empty");
1232
- return function(target, propertyKey, parameterIndex) {
1235
+ return (target, propertyKey, parameterIndex)=>{
1233
1236
  if (parameterIndex === undefined) {
1234
1237
  // The decorator has been applied to the class
1235
1238
  const Class = target;
@@ -1253,7 +1256,7 @@ function Named(name) {
1253
1256
 
1254
1257
  // @__NO_SIDE_EFFECTS__
1255
1258
  function Optional(token) {
1256
- return function(target, propertyKey, parameterIndex) {
1259
+ return (target, propertyKey, parameterIndex)=>{
1257
1260
  updateParameterMetadata("Optional", target, propertyKey, parameterIndex, (dependency)=>{
1258
1261
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1259
1262
  dependency.appliedBy = "Optional";
@@ -1264,7 +1267,7 @@ function Optional(token) {
1264
1267
 
1265
1268
  // @__NO_SIDE_EFFECTS__
1266
1269
  function OptionalAll(token) {
1267
- return function(target, propertyKey, parameterIndex) {
1270
+ return (target, propertyKey, parameterIndex)=>{
1268
1271
  updateParameterMetadata("OptionalAll", target, propertyKey, parameterIndex, (dependency)=>{
1269
1272
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1270
1273
  dependency.appliedBy = "OptionalAll";
@@ -1274,6 +1277,56 @@ function OptionalAll(token) {
1274
1277
  };
1275
1278
  }
1276
1279
 
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");
1296
+ }
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");
1313
+ }
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");
1329
+ }
1277
1330
  /**
1278
1331
  * Class decorator for setting the scope of the decorated type when registering it.
1279
1332
  *
@@ -1295,18 +1348,21 @@ function OptionalAll(token) {
1295
1348
  * ```
1296
1349
  */ // @__NO_SIDE_EFFECTS__
1297
1350
  function Scoped(scope) {
1298
- return function(Class) {
1351
+ return scoped(scope, "Scoped");
1352
+ }
1353
+ function scoped(scope, decorator) {
1354
+ return (Class)=>{
1299
1355
  const metadata = getMetadata(Class);
1300
1356
  const currentScope = metadata.scope;
1301
1357
  check(!currentScope || currentScope.value === scope, ()=>{
1302
1358
  const { value, appliedBy } = currentScope;
1303
- const by = appliedBy === "Scoped" ? `another @${appliedBy} decorator` : `@${appliedBy}`;
1359
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1304
1360
  const className = getTokenName(Class);
1305
- return `class ${className}: scope ${value} was already set by ${by},\n ` + `but @Scoped is trying to set a conflicting scope ${scope}.\n ` + `Only one decorator should set the class scope, or all must agree on the same value.`;
1361
+ 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.`;
1306
1362
  });
1307
1363
  metadata.scope = {
1308
1364
  value: scope,
1309
- appliedBy: "Scoped"
1365
+ appliedBy: decorator
1310
1366
  };
1311
1367
  };
1312
1368
  }
@@ -1370,6 +1426,7 @@ const Scope = {
1370
1426
  };
1371
1427
 
1372
1428
  exports.AutoRegister = AutoRegister;
1429
+ exports.ContainerScoped = ContainerScoped;
1373
1430
  exports.EagerInstantiate = EagerInstantiate;
1374
1431
  exports.Inject = Inject;
1375
1432
  exports.InjectAll = InjectAll;
@@ -1378,8 +1435,10 @@ exports.Injector = Injector;
1378
1435
  exports.Named = Named;
1379
1436
  exports.Optional = Optional;
1380
1437
  exports.OptionalAll = OptionalAll;
1438
+ exports.ResolutionScoped = ResolutionScoped;
1381
1439
  exports.Scope = Scope;
1382
1440
  exports.Scoped = Scoped;
1441
+ exports.TransientScoped = TransientScoped;
1383
1442
  exports.assertInjectionContext = assertInjectionContext;
1384
1443
  exports.build = build;
1385
1444
  exports.classRef = classRef;