diff --git a/src/decorator/listen.decorator.spec.ts b/src/decorator/listen.decorator.spec.ts index f7f2017..78d4a46 100644 --- a/src/decorator/listen.decorator.spec.ts +++ b/src/decorator/listen.decorator.spec.ts @@ -22,7 +22,7 @@ describe('@Listen', () => { it('should set correctly the queue name and target data', () => { const metadata = Reflect.getMetadata(QUEUE_LISTEN_METADATA_KEY, instance.method1); - expect(metadata).toEqual(expect.objectContaining({ source: queueName, targetName: Test.name, callbackName: 'method1' })); + expect(metadata).toEqual(expect.objectContaining({ source: queueName, targetName: Test.name, target: Test, callbackName: 'method1' })); }); it('should set the queue options', () => { diff --git a/src/decorator/listen.decorator.ts b/src/decorator/listen.decorator.ts index fa49e71..d00651d 100644 --- a/src/decorator/listen.decorator.ts +++ b/src/decorator/listen.decorator.ts @@ -27,6 +27,7 @@ export const Listen = (source: string, options?: ListenOptions) => { metadata.options = options; metadata.targetName = target.constructor.name; + metadata.target = target.constructor; metadata.callback = descriptor.value; metadata.callbackName = propertyKey; diff --git a/src/domain/listener-metadata.domain.ts b/src/domain/listener-metadata.domain.ts index 0a070d0..7d20b1e 100644 --- a/src/domain/listener-metadata.domain.ts +++ b/src/domain/listener-metadata.domain.ts @@ -1,5 +1,7 @@ import { ListenOptions } from '../interface'; +/* eslint-disable @typescript-eslint/ban-types */ + /** * Metadata added by the `@Listener` decorator */ @@ -25,7 +27,12 @@ export class ListenerMetadata { public options: ListenOptions; /** - * The Class the method belongs to + * The name of Class the method belongs to */ public targetName: string; + + /** + * The Class the method belongs to + */ + public target: object; } diff --git a/src/queue.module.ts b/src/queue.module.ts index 2d4f4d0..9574e5b 100644 --- a/src/queue.module.ts +++ b/src/queue.module.ts @@ -1,6 +1,7 @@ import { DynamicModule, Inject, Module, OnModuleDestroy, OnModuleInit, Provider, Type } from '@nestjs/common'; import { Connection } from 'rhea-promise'; import { MetadataScanner, ModuleRef } from '@nestjs/core'; +import { UnknownElementException } from '@nestjs/core/errors/exceptions/unknown-element.exception'; import { QueueModuleOptions, QueueModuleAsyncOptions, QueueModuleOptionsFactory } from './interface'; import { AMQPService, ObjectValidatorService, QueueService } from './service'; @@ -145,7 +146,16 @@ export class QueueModule implements OnModuleInit, OnModuleDestroy { logger.debug(`attaching listener for @Listen: ${JSON.stringify(listener)}`); // fetch instance from DI framework - const target = this.moduleRef.get(listener.targetName, { strict: false }); + let target: any; + try { + target = this.moduleRef.get(listener.target as any, { strict: false }); + } catch (err) { + if (err instanceof UnknownElementException) { + target = this.moduleRef.get(listener.targetName, { strict: false }); + } else { + throw err; + } + } await this.queueService.listen(listener.source, listener.callback.bind(target), listener.options); }