Skip to content

Commit

Permalink
fix(injector): correctly resolve config injections
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Oct 3, 2023
1 parent 2b89bae commit c4ed83a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
78 changes: 62 additions & 16 deletions packages/app/tests/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,23 +593,23 @@ test('events', async () => {
let shutdownEvent: AppEvent | undefined = undefined;

const app = new App({
providers: [MyService],
})
.command('test', (id: number, check: boolean & Flag = false) => {
if (id === 404) throw new Error('error');
})
.listen(onAppExecute, (event) => {
executeEvent = event.data;
})
.listen(onAppExecuted, (event) => {
executedEvent = event.data;
})
.listen(onAppError, (event) => {
errorEvent = event.data;
})
.listen(onAppShutdown, (event) => {
shutdownEvent = event.data;
providers: [MyService],
})
.command('test', (id: number, check: boolean & Flag = false) => {
if (id === 404) throw new Error('error');
})
.listen(onAppExecute, (event) => {
executeEvent = event.data;
})
.listen(onAppExecuted, (event) => {
executedEvent = event.data;
})
.listen(onAppError, (event) => {
errorEvent = event.data;
})
.listen(onAppShutdown, (event) => {
shutdownEvent = event.data;
})
;

{
Expand Down Expand Up @@ -651,3 +651,49 @@ test('events', async () => {
expect(shutdownEvent!.parameters).toEqual({ id: 404, check: true });
}
});

test('config injection into listener empty', async () => {
class Config {
host: string = '';
}

function registerHost(event: any, host: Config['host']) {
expect(host).toBe('');
}

const app = new App({
config: Config,
});

app.listen(onAppExecute, registerHost);

app.command('test', () => {
return 1;
});

const res = await app.execute(['test']);
expect(res).toBe(1);
});

test('config injection into listener configured', async () => {
class Config {
host: string = '';
}

function registerHost(event: any, host: Config['host']) {
expect(host).toBe('localhost');
}

const app = new App({
config: Config,
}).configure({ host: 'localhost' });

app.listen(onAppExecute, registerHost);

app.command('test', () => {
return 1;
});

const res = await app.execute(['test']);
expect(res).toBe(1);
});
7 changes: 3 additions & 4 deletions packages/injector/src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,23 +822,22 @@ export class Injector implements InjectorInterface {

if (type.indexAccessOrigin) {
let current = type;
let module: InjectorModule | undefined;
let config: { [name: string]: any } = {};
let config: { [name: string]: any } | undefined = undefined;

while (current && current.indexAccessOrigin) {
if (current.indexAccessOrigin.container.kind === ReflectionKind.class) {
const found = findModuleForConfig(current.indexAccessOrigin.container.classType, resolveDependenciesFrom);
if (!found) return () => undefined;
config = getPathValue(found.module.getConfig(), found.path);
}
if (current.indexAccessOrigin.index.kind === ReflectionKind.literal) {
if (config !== undefined && current.indexAccessOrigin.index.kind === ReflectionKind.literal) {
const index = current.indexAccessOrigin.index.literal;
config = config[String(index)];
}
current = current.indexAccessOrigin.container;
}

if (config) return () => config;
if (config !== undefined) return () => config;
}
}

Expand Down

0 comments on commit c4ed83a

Please sign in to comment.