-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rabbitmq): adds consistent rabbitmq config
use community convention of forRoot and forRootAsync to bootstrap module fix #34
- Loading branch information
1 parent
9bba7cb
commit 8d6de1d
Showing
12 changed files
with
329 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { RabbitMQModule, RabbitMQConfig } from '@nestjs-plus/rabbitmq'; | ||
import * as amqplib from 'amqplib'; | ||
|
||
const uri = 'amqp://rabbitmq:rabbitmq@localhost:5672'; | ||
|
||
class RabbitConfig { | ||
createOptions(): RabbitMQConfig { | ||
return { | ||
uri, | ||
}; | ||
} | ||
} | ||
|
||
describe('Module Configuration', () => { | ||
let app: TestingModule; | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
describe('forRoot', () => { | ||
it('should configure RabbitMQ', async () => { | ||
const spy = jest.spyOn(amqplib, 'connect'); | ||
|
||
app = await Test.createTestingModule({ | ||
imports: [ | ||
RabbitMQModule.forRoot({ | ||
uri, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(uri); | ||
}); | ||
}); | ||
|
||
describe('forRootAsync', () => { | ||
it('should configure RabbitMQ with useFactory', async () => { | ||
const spy = jest.spyOn(amqplib, 'connect'); | ||
|
||
app = await Test.createTestingModule({ | ||
imports: [ | ||
RabbitMQModule.forRootAsync({ | ||
useFactory: async () => { | ||
return { | ||
uri, | ||
}; | ||
}, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(uri); | ||
}); | ||
|
||
it('should configure RabbitMQ with useClass', async () => { | ||
const spy = jest.spyOn(amqplib, 'connect'); | ||
|
||
app = await Test.createTestingModule({ | ||
imports: [ | ||
RabbitMQModule.forRootAsync({ | ||
useClass: RabbitConfig, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(uri); | ||
}); | ||
|
||
it('should configure RabbitMQ with useExisting explicit provide', async () => { | ||
const spy = jest.spyOn(amqplib, 'connect'); | ||
|
||
const instance = new RabbitConfig(); | ||
|
||
app = await Test.createTestingModule({ | ||
imports: [ | ||
RabbitMQModule.forRootAsync({ | ||
useExisting: { | ||
provide: RabbitConfig, | ||
value: instance, | ||
}, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(uri); | ||
}); | ||
|
||
it('should configure RabbitMQ with useExisting implicit provide', async () => { | ||
const spy = jest.spyOn(amqplib, 'connect'); | ||
|
||
const instance = new RabbitConfig(); | ||
|
||
app = await Test.createTestingModule({ | ||
imports: [ | ||
RabbitMQModule.forRootAsync({ | ||
useExisting: { | ||
value: instance, | ||
}, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(uri); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './mixins'; | ||
export * from './options'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { ModuleMetadata, Provider } from '@nestjs/common/interfaces'; | ||
import { get } from 'lodash'; | ||
|
||
export interface OptionsFactory<T> { | ||
createOptions(): Promise<T> | T; | ||
} | ||
|
||
// type OptionsFactoryImpl<T extends OptionsFactory<T>> = T; | ||
|
||
export interface AsyncOptionsFactoryProvider<T> | ||
extends Pick<ModuleMetadata, 'imports'> { | ||
useExisting?: { | ||
value: OptionsFactory<T>; | ||
provide?: string | symbol | Type<any>; | ||
}; | ||
useClass?: Type<OptionsFactory<T>>; | ||
useFactory?: (...args: any[]) => Promise<T> | T; | ||
inject?: any[]; | ||
} | ||
|
||
export function createAsyncOptionsProvider<T>( | ||
provide: string | symbol | Type<any>, | ||
options: AsyncOptionsFactoryProvider<T> | ||
): Provider { | ||
if (options.useFactory) { | ||
return { | ||
provide, | ||
useFactory: options.useFactory, | ||
inject: options.inject || [] | ||
}; | ||
} | ||
|
||
return { | ||
provide, | ||
useFactory: async (optionsFactory: OptionsFactory<T>) => { | ||
const options = await optionsFactory.createOptions(); | ||
return options; | ||
}, | ||
inject: [ | ||
options.useClass || | ||
get( | ||
options, | ||
'useExisting.provide', | ||
(options.useExisting as any).value.constructor.name | ||
) | ||
] | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const RABBIT_HANDLER = Symbol('RABBIT_HANDLER'); | ||
export const RABBIT_CONFIG = Symbol('RABBIT_CONFIG'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.