Skip to content

Commit

Permalink
feat(queue): add support to work only with module options arg
Browse files Browse the repository at this point in the history
  • Loading branch information
tahubu committed Apr 13, 2021
1 parent de4a3df commit 523d279
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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 {}
Expand Down Expand Up @@ -81,7 +83,7 @@ import { QueueModule, QueueModuleOptions } from '@team-supercharge/nest-amqp';
useFactory: (configService: ConfigService): QueueModuleOptions => ({
connectionUri: configService.get<string>('AMQP_CONNECTION_URI'),
connectionOptions: {
transport: 'tcp'
transport: configService.get<string>('AMQP_CONNECTION_TRANSPORT'),
}
}),
inject: [ConfigService],
Expand Down Expand Up @@ -136,7 +138,7 @@ Second example with asynchronous configuration:
connectionUri: configService.get<string>('AMQP_CONNECTION_URI'),
throwExceptionOnConnectionError: true,
connectionOptions: {
transport: 'tcp'
transport: configService.get<string>('AMQP_CONNECTION_TRANSPORT'),
}
}),
inject: [ConfigService],
Expand Down
20 changes: 19 additions & 1 deletion src/queue.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,32 @@ 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();
const amqpService = module.get<AMQPService>(AMQPService);

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>(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>(AMQPService);

expect(amqpService.getModuleOptions()).toEqual({ throwExceptionOnConnectionError: true, connectionUri });
});
});

describe('forFeature()', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/queue.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ export class QueueModule implements OnModuleInit, OnModuleDestroy {
exports: [QueueService],
};

public static forRoot(connectionUri: string, options?: Omit<QueueModuleOptions, 'connectionUri'>): DynamicModule {
const queueModuleOptionsProvider = QueueModule.getQueueModuleOptionsProvider({ ...options, connectionUri });
public static forRoot(options: QueueModuleOptions): DynamicModule;
public static forRoot(connectionUri: string, options?: Omit<QueueModuleOptions, 'connectionUri'>): DynamicModule;
public static forRoot(connectionUri: string | QueueModuleOptions, options?: Omit<QueueModuleOptions, 'connectionUri'>): DynamicModule {
const queueModuleOptionsProvider = QueueModule.getQueueModuleOptionsProvider(
typeof connectionUri === 'string' ? { ...options, connectionUri } : connectionUri,
);
const connectionProvider = QueueModule.getConnectionProvider();

Object.assign(QueueModule.moduleDefinition, {
Expand Down

0 comments on commit 523d279

Please sign in to comment.