@lppedd/di-wise-neo 0.5.1 → 0.5.3

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.
@@ -792,6 +792,24 @@ interface Injector {
792
792
  */
793
793
  declare const Injector: Type<Injector>;
794
794
 
795
+ /**
796
+ * Registers a mapping between a generated (e.g., decorated or proxied) constructor
797
+ * and its original, underlying constructor.
798
+ *
799
+ * This allows libraries or consumers that manipulate constructors, such as through
800
+ * class decorators, to inform the DI system about the real "identity" of a class.
801
+ *
802
+ * @param transformedClass The constructor function returned by a class decorator or factory.
803
+ * @param originalClass The original constructor function.
804
+ *
805
+ * @remarks
806
+ * This API affects the core class identity resolution mechanism of the DI system.
807
+ * Incorrect usage may cause metadata to be misassociated, leading to subtle errors.
808
+ * Use only when manipulating constructors (e.g., via decorators or proxies),
809
+ * and ensure the mapping is correct.
810
+ */
811
+ declare function setClassIdentityMapping<T extends object>(transformedClass: Constructor<T>, originalClass: Constructor<T>): void;
812
+
795
813
  /**
796
814
  * Composer API for middleware functions.
797
815
  */
@@ -847,5 +865,5 @@ interface Middleware {
847
865
  */
848
866
  declare function applyMiddleware(container: Container, middlewares: Middleware[]): Container;
849
867
 
850
- export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
868
+ export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, setClassIdentityMapping };
851
869
  export type { ClassProvider, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, Type, ValueProvider };
package/dist/cjs/index.js CHANGED
@@ -156,16 +156,35 @@ function injectAll(token) {
156
156
  return context.container.resolveAll(token);
157
157
  }
158
158
 
159
+ /**
160
+ * Registers a mapping between a generated (e.g., decorated or proxied) constructor
161
+ * and its original, underlying constructor.
162
+ *
163
+ * This allows libraries or consumers that manipulate constructors, such as through
164
+ * class decorators, to inform the DI system about the real "identity" of a class.
165
+ *
166
+ * @param transformedClass The constructor function returned by a class decorator or factory.
167
+ * @param originalClass The original constructor function.
168
+ *
169
+ * @remarks
170
+ * This API affects the core class identity resolution mechanism of the DI system.
171
+ * Incorrect usage may cause metadata to be misassociated, leading to subtle errors.
172
+ * Use only when manipulating constructors (e.g., via decorators or proxies),
173
+ * and ensure the mapping is correct.
174
+ */ function setClassIdentityMapping(transformedClass, originalClass) {
175
+ classIdentityMap.set(transformedClass, originalClass);
176
+ }
159
177
  // @internal
160
178
  function getMetadata(Class) {
161
- let metadata = metadataMap.get(Class);
179
+ const originalClass = classIdentityMap.get(Class) ?? Class;
180
+ let metadata = metadataMap.get(originalClass);
162
181
  if (!metadata) {
163
- metadataMap.set(Class, metadata = {
182
+ metadataMap.set(originalClass, metadata = {
164
183
  tokensRef: {
165
184
  getRefTokens: ()=>new Set()
166
185
  },
167
186
  provider: {
168
- useClass: Class
187
+ useClass: originalClass
169
188
  },
170
189
  dependencies: {
171
190
  constructor: [],
@@ -173,8 +192,25 @@ function getMetadata(Class) {
173
192
  }
174
193
  });
175
194
  }
195
+ if (metadata.provider.useClass !== Class) {
196
+ // This is part of the class identity mapping API (see setClassIdentityMapping).
197
+ //
198
+ // Scenario:
199
+ // 1. Metadata is created for class A (the original class) because of a parameter decorator.
200
+ // 2. Later, because of a class decorator that extends the decorated class, a third-party
201
+ // registers a class identity mapping from class B to class A, where B is the class
202
+ // generated from the class decorator by extending A.
203
+ //
204
+ // We must update useClass to be the extender class B so that instances created by the
205
+ // DI container match the consumer's registered class. Without this update, the DI
206
+ // system would instantiate the original class A, causing behavior inconsistencies.
207
+ //
208
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
209
+ metadata.provider.useClass = Class;
210
+ }
176
211
  return metadata;
177
212
  }
213
+ const classIdentityMap = new WeakMap();
178
214
  const metadataMap = new WeakMap();
179
215
 
180
216
  function optional(token) {
@@ -1120,4 +1156,5 @@ exports.forwardRef = forwardRef;
1120
1156
  exports.inject = inject;
1121
1157
  exports.injectAll = injectAll;
1122
1158
  exports.injectBy = injectBy;
1159
+ exports.setClassIdentityMapping = setClassIdentityMapping;
1123
1160
  //# sourceMappingURL=index.js.map