From c4ed83ab3a66a6a6c6dc474a33ab41a4946c64e4 Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Tue, 3 Oct 2023 23:47:02 +0200 Subject: [PATCH] fix(injector): correctly resolve config injections --- packages/app/tests/application.spec.ts | 78 ++++++++++++++++++++------ packages/injector/src/injector.ts | 7 +-- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/packages/app/tests/application.spec.ts b/packages/app/tests/application.spec.ts index 1adbaf2ac..54a9ba6b0 100644 --- a/packages/app/tests/application.spec.ts +++ b/packages/app/tests/application.spec.ts @@ -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; + }) ; { @@ -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); +}); diff --git a/packages/injector/src/injector.ts b/packages/injector/src/injector.ts index bd1a0018a..8724019a2 100644 --- a/packages/injector/src/injector.ts +++ b/packages/injector/src/injector.ts @@ -822,8 +822,7 @@ 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) { @@ -831,14 +830,14 @@ export class Injector implements InjectorInterface { 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; } }