refactor(core): Migrate binary-data config to a decorated config class (#14616)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-04-15 10:32:38 +02:00
committed by GitHub
parent a12c9522d5
commit 2ca742cb15
23 changed files with 208 additions and 166 deletions

View File

@@ -159,6 +159,19 @@ describe('DI Container', () => {
expect(() => Container.get(ErrorFactoryService)).toThrow('Factory error');
});
it('should handle factory with dependencies', () => {
const factory = jest.fn().mockReturnValue({});
@Service({ factory })
class FactoryWithDependencies {
constructor(readonly simpleService: SimpleService) {}
}
const instance = Container.get(FactoryWithDependencies);
expect(instance.simpleService).toBeUndefined();
expect(factory).toHaveBeenCalledWith(Container.get(SimpleService));
});
});
describe('instance management', () => {

View File

@@ -11,13 +11,15 @@ type AbstractConstructable<T = unknown> = abstract new (...args: unknown[]) => T
type ServiceIdentifier<T = unknown> = Constructable<T> | AbstractConstructable<T>;
type Factory<T = unknown> = (...args: unknown[]) => T;
interface Metadata<T = unknown> {
instance?: T;
factory?: () => T;
factory?: Factory<T>;
}
interface Options<T> {
factory?: () => T;
factory?: Factory<T>;
}
const instances = new Map<ServiceIdentifier, Metadata>();
@@ -84,20 +86,20 @@ class ContainerClass {
try {
let instance: T;
if (metadata?.factory) {
instance = metadata.factory();
} else {
const paramTypes = (Reflect.getMetadata('design:paramtypes', type) ??
[]) as Constructable[];
const paramTypes = (Reflect.getMetadata('design:paramtypes', type) ?? []) as Constructable[];
const dependencies = paramTypes.map(<P>(paramType: Constructable<P>, index: number) => {
if (paramType === undefined) {
throw new DIError(
`Circular dependency detected in ${type.name} at index ${index}.\n${resolutionStack.map((t) => t.name).join(' -> ')}\n`,
);
}
return this.get(paramType);
});
const dependencies = paramTypes.map(<P>(paramType: Constructable<P>, index: number) => {
if (paramType === undefined) {
throw new DIError(
`Circular dependency detected in ${type.name} at index ${index}.\n${resolutionStack.map((t) => t.name).join(' -> ')}\n`,
);
}
return this.get(paramType);
});
if (metadata?.factory) {
instance = metadata.factory(...dependencies);
} else {
// Create new instance with resolved dependencies
instance = new (type as Constructable)(...dependencies) as T;
}