@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/es/index.mjs CHANGED
@@ -439,14 +439,13 @@ function describeParam(target, methodKey, parameterIndex) {
439
439
  // @__NO_SIDE_EFFECTS__
440
440
  function createType(typeName, provider, options) {
441
441
  const name = `Type<${typeName}>`;
442
- const decorator = (target, propertyKey, parameterIndex)=>{
442
+ const type = (target, propertyKey, parameterIndex)=>{
443
443
  updateParameterMetadata(name, target, propertyKey, parameterIndex, (dependency)=>{
444
444
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
445
445
  dependency.appliedBy = "Inject";
446
- dependency.tokenRef = tokenRef(()=>decorator);
446
+ dependency.tokenRef = tokenRef(()=>type);
447
447
  });
448
448
  };
449
- const type = decorator;
450
449
  Object.defineProperty(type, "name", {
451
450
  value: name
452
451
  });
@@ -1126,7 +1125,7 @@ function isDisposable(value) {
1126
1125
  * ```
1127
1126
  */ // @__NO_SIDE_EFFECTS__
1128
1127
  function AutoRegister() {
1129
- return function(Class) {
1128
+ return (Class)=>{
1130
1129
  const metadata = getMetadata(Class);
1131
1130
  metadata.autoRegister = true;
1132
1131
  };
@@ -1136,8 +1135,10 @@ function AutoRegister() {
1136
1135
  * Class decorator that sets the class scope to **Container** and enables
1137
1136
  * eager instantiation when the class is registered in the container.
1138
1137
  *
1139
- * This causes the container to immediately create and cache the instance of the class,
1140
- * instead of deferring instantiation until the first resolution.
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.
1141
1142
  *
1142
1143
  * Example:
1143
1144
  * ```ts
@@ -1145,18 +1146,19 @@ function AutoRegister() {
1145
1146
  * class Wizard {}
1146
1147
  *
1147
1148
  * // Wizard is registered with Container scope, and an instance
1148
- * // is immediately created and cached by the container
1149
+ * // is created and cached immediately by the container
1149
1150
  * container.register(Wizard);
1150
1151
  * ```
1151
1152
  */ // @__NO_SIDE_EFFECTS__
1152
1153
  function EagerInstantiate() {
1153
- return function(Class) {
1154
+ return (Class)=>{
1154
1155
  const metadata = getMetadata(Class);
1155
1156
  const currentScope = metadata.scope;
1156
1157
  check(!currentScope || currentScope.value === "Container", ()=>{
1157
1158
  const { value, appliedBy } = currentScope;
1159
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1158
1160
  const className = getTokenName(Class);
1159
- 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.`;
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.`;
1160
1162
  });
1161
1163
  metadata.eagerInstantiate = true;
1162
1164
  metadata.scope = {
@@ -1168,7 +1170,7 @@ function EagerInstantiate() {
1168
1170
 
1169
1171
  // @__NO_SIDE_EFFECTS__
1170
1172
  function Inject(token) {
1171
- return function(target, propertyKey, parameterIndex) {
1173
+ return (target, propertyKey, parameterIndex)=>{
1172
1174
  updateParameterMetadata("Inject", target, propertyKey, parameterIndex, (dependency)=>{
1173
1175
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1174
1176
  dependency.appliedBy = "Inject";
@@ -1179,7 +1181,7 @@ function Inject(token) {
1179
1181
 
1180
1182
  // @__NO_SIDE_EFFECTS__
1181
1183
  function Injectable(...args) {
1182
- return function(Class) {
1184
+ return (Class)=>{
1183
1185
  const metadata = getMetadata(Class);
1184
1186
  const arg0 = args[0];
1185
1187
  const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
@@ -1199,7 +1201,7 @@ function Injectable(...args) {
1199
1201
 
1200
1202
  // @__NO_SIDE_EFFECTS__
1201
1203
  function InjectAll(token) {
1202
- return function(target, propertyKey, parameterIndex) {
1204
+ return (target, propertyKey, parameterIndex)=>{
1203
1205
  updateParameterMetadata("InjectAll", target, propertyKey, parameterIndex, (dependency)=>{
1204
1206
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1205
1207
  dependency.appliedBy = "InjectAll";
@@ -1210,24 +1212,25 @@ function InjectAll(token) {
1210
1212
  }
1211
1213
 
1212
1214
  /**
1213
- * Qualifies a class or an injected parameter with a unique name.
1215
+ * Qualifies a class or an injected parameter with a name.
1216
+ *
1217
+ * On a class, the name is used when registering that class with the container.
1218
+ * On a parameter, the name selects a named registration or alias during injection.
1214
1219
  *
1215
- * This allows the container to distinguish between multiple implementations
1216
- * of the same interface or type during registration and injection.
1220
+ * This is useful when multiple registrations exist for the same token.
1217
1221
  *
1218
1222
  * Example:
1219
1223
  * ```ts
1220
1224
  * @Named("dumbledore")
1221
1225
  * class Dumbledore implements Wizard {}
1222
1226
  *
1223
- * // Register Dumbledore with Type<Wizard>
1224
1227
  * container.register(IWizard, { useClass: Dumbledore });
1225
1228
  * const dumbledore = container.resolve(IWizard, "dumbledore");
1226
1229
  * ```
1227
1230
  */ // @__NO_SIDE_EFFECTS__
1228
1231
  function Named(name) {
1229
1232
  check(name.trim(), "@Named qualifier must not be empty");
1230
- return function(target, propertyKey, parameterIndex) {
1233
+ return (target, propertyKey, parameterIndex)=>{
1231
1234
  if (parameterIndex === undefined) {
1232
1235
  // The decorator has been applied to the class
1233
1236
  const Class = target;
@@ -1251,7 +1254,7 @@ function Named(name) {
1251
1254
 
1252
1255
  // @__NO_SIDE_EFFECTS__
1253
1256
  function Optional(token) {
1254
- return function(target, propertyKey, parameterIndex) {
1257
+ return (target, propertyKey, parameterIndex)=>{
1255
1258
  updateParameterMetadata("Optional", target, propertyKey, parameterIndex, (dependency)=>{
1256
1259
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1257
1260
  dependency.appliedBy = "Optional";
@@ -1262,7 +1265,7 @@ function Optional(token) {
1262
1265
 
1263
1266
  // @__NO_SIDE_EFFECTS__
1264
1267
  function OptionalAll(token) {
1265
- return function(target, propertyKey, parameterIndex) {
1268
+ return (target, propertyKey, parameterIndex)=>{
1266
1269
  updateParameterMetadata("OptionalAll", target, propertyKey, parameterIndex, (dependency)=>{
1267
1270
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1268
1271
  dependency.appliedBy = "OptionalAll";
@@ -1272,6 +1275,56 @@ function OptionalAll(token) {
1272
1275
  };
1273
1276
  }
1274
1277
 
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");
1294
+ }
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");
1311
+ }
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");
1327
+ }
1275
1328
  /**
1276
1329
  * Class decorator for setting the scope of the decorated type when registering it.
1277
1330
  *
@@ -1293,18 +1346,21 @@ function OptionalAll(token) {
1293
1346
  * ```
1294
1347
  */ // @__NO_SIDE_EFFECTS__
1295
1348
  function Scoped(scope) {
1296
- return function(Class) {
1349
+ return scoped(scope, "Scoped");
1350
+ }
1351
+ function scoped(scope, decorator) {
1352
+ return (Class)=>{
1297
1353
  const metadata = getMetadata(Class);
1298
1354
  const currentScope = metadata.scope;
1299
1355
  check(!currentScope || currentScope.value === scope, ()=>{
1300
1356
  const { value, appliedBy } = currentScope;
1301
- const by = appliedBy === "Scoped" ? `another @${appliedBy} decorator` : `@${appliedBy}`;
1357
+ const by = appliedBy === "Scoped" ? `${appliedBy}(${value})` : appliedBy;
1302
1358
  const className = getTokenName(Class);
1303
- 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.`;
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.`;
1304
1360
  });
1305
1361
  metadata.scope = {
1306
1362
  value: scope,
1307
- appliedBy: "Scoped"
1363
+ appliedBy: decorator
1308
1364
  };
1309
1365
  };
1310
1366
  }
@@ -1367,5 +1423,5 @@ const Scope = {
1367
1423
  */ Container: "Container"
1368
1424
  };
1369
1425
 
1370
- 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 };
1426
+ 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 };
1371
1427
  //# sourceMappingURL=index.mjs.map