From 523d279d307c436f3818eb4d66fa46d0275d14a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20Hugy=C3=A1k?= Date: Tue, 13 Apr 2021 16:03:44 +0200 Subject: [PATCH] feat(queue): add support to work only with module options arg --- README.md | 10 ++++++---- src/queue.module.spec.ts | 20 +++++++++++++++++++- src/queue.module.ts | 8 ++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c375eae..315d7dd 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,8 @@ connection options or other library settings with the module options. To create a connection, you have to import the `QueueModule.forRoot()` module into your application's root module. The `forRoot()` static method has 2 parameters: the first and required is the connection URI string, and the second is the *optional* module -options object. To see the available module options, scroll down. Here is the example: +options object, or you can just pass the module options object to it. To see the available module options, scroll down. +Here is the example: > Note: the @team-supercharge/nest-amqp package does not support multiple connection! @@ -53,7 +54,8 @@ import { QueueModule } from '@team-supercharge/nest-amqp'; @Module({ imports: [ QueueModule.forRoot('amqp://user:password@localhost:5672'), - // ... + // or alternatively + // QueueModule.forRoot({ connectionUri: 'amqp://user:password@localhost:5672' }), ], }) export class AppModule {} @@ -81,7 +83,7 @@ import { QueueModule, QueueModuleOptions } from '@team-supercharge/nest-amqp'; useFactory: (configService: ConfigService): QueueModuleOptions => ({ connectionUri: configService.get('AMQP_CONNECTION_URI'), connectionOptions: { - transport: 'tcp' + transport: configService.get('AMQP_CONNECTION_TRANSPORT'), } }), inject: [ConfigService], @@ -136,7 +138,7 @@ Second example with asynchronous configuration: connectionUri: configService.get('AMQP_CONNECTION_URI'), throwExceptionOnConnectionError: true, connectionOptions: { - transport: 'tcp' + transport: configService.get('AMQP_CONNECTION_TRANSPORT'), } }), inject: [ConfigService], diff --git a/src/queue.module.spec.ts b/src/queue.module.spec.ts index 0092063..a04d577 100644 --- a/src/queue.module.spec.ts +++ b/src/queue.module.spec.ts @@ -60,7 +60,7 @@ describe('QueueModule', () => { }); describe('forRoot()', () => { - it('should import as sync root module', async () => { + it('should work only with a connection URI', async () => { module = await Test.createTestingModule({ imports: [QueueModule.forRoot(connectionUri)], }).compile(); @@ -68,6 +68,24 @@ describe('QueueModule', () => { expect(amqpService.getModuleOptions()).toEqual(moduleOptions); }); + + it('should work with connection URI and module options arguments', async () => { + module = await Test.createTestingModule({ + imports: [QueueModule.forRoot(connectionUri, { throwExceptionOnConnectionError: true })], + }).compile(); + const amqpService = module.get(AMQPService); + + expect(amqpService.getModuleOptions()).toEqual({ throwExceptionOnConnectionError: true, connectionUri }); + }); + + it('should work only with module options', async () => { + module = await Test.createTestingModule({ + imports: [QueueModule.forRoot({ connectionUri, throwExceptionOnConnectionError: true })], + }).compile(); + const amqpService = module.get(AMQPService); + + expect(amqpService.getModuleOptions()).toEqual({ throwExceptionOnConnectionError: true, connectionUri }); + }); }); describe('forFeature()', () => { diff --git a/src/queue.module.ts b/src/queue.module.ts index 8c50bc7..02da6ed 100644 --- a/src/queue.module.ts +++ b/src/queue.module.ts @@ -17,8 +17,12 @@ export class QueueModule implements OnModuleInit, OnModuleDestroy { exports: [QueueService], }; - public static forRoot(connectionUri: string, options?: Omit): DynamicModule { - const queueModuleOptionsProvider = QueueModule.getQueueModuleOptionsProvider({ ...options, connectionUri }); + public static forRoot(options: QueueModuleOptions): DynamicModule; + public static forRoot(connectionUri: string, options?: Omit): DynamicModule; + public static forRoot(connectionUri: string | QueueModuleOptions, options?: Omit): DynamicModule { + const queueModuleOptionsProvider = QueueModule.getQueueModuleOptionsProvider( + typeof connectionUri === 'string' ? { ...options, connectionUri } : connectionUri, + ); const connectionProvider = QueueModule.getConnectionProvider(); Object.assign(QueueModule.moduleDefinition, {