mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
refactor(core): Migrate binary-data config to a decorated config class (#14616)
This commit is contained in:
committed by
GitHub
parent
a12c9522d5
commit
2ca742cb15
@@ -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', () => {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user