-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve codes and add dto and constant
- Loading branch information
Showing
11 changed files
with
110 additions
and
115 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './rmq/rmq.decorator'; | ||
export * from './rmq/rmq.module'; | ||
export * from './rmq/rmq.service'; | ||
export * from './rmq/rmq-handler' | ||
export * from './rmq.decorator'; | ||
export * from './rmq.module'; | ||
export * from './rmq.service'; |
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,5 @@ | ||
export const DEFAULT_EXCHANGE_TYPE = 'topic'; | ||
export const DEFAULT_EXCHANGE_NAME = 'rmq'; | ||
export const DEFAULT_TIMEOUT = 20000; | ||
export const DEFAULT_WAIT = true; | ||
export const DEFAULT_PREFETCH_COUNT = 10; |
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,21 @@ | ||
import { | ||
RabbitRPC, | ||
RabbitSubscribe, | ||
MessageHandlerOptions, | ||
MessageHandlerErrorBehavior, | ||
} from '@golevelup/nestjs-rabbitmq'; | ||
import {DEFAULT_EXCHANGE_NAME} from "./rmq.constant"; | ||
|
||
export const Rpc = (options: MessageHandlerOptions) => | ||
RabbitRPC({ | ||
...options, | ||
exchange: options.exchange || DEFAULT_EXCHANGE_NAME, | ||
errorBehavior: MessageHandlerErrorBehavior.NACK, | ||
}); | ||
|
||
export const Subscribe = (options: MessageHandlerOptions) => | ||
RabbitSubscribe({ | ||
...options, | ||
exchange: options.exchange || DEFAULT_EXCHANGE_NAME, | ||
errorBehavior: MessageHandlerErrorBehavior.NACK, | ||
}); |
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,8 @@ | ||
export class RmqDto { | ||
uri: string; | ||
wait?: boolean; | ||
timeout?: number; | ||
name?: string; | ||
type?: string; | ||
prefetchCount?: number | ||
} |
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,38 @@ | ||
import {DynamicModule, Module} from "@nestjs/common"; | ||
import {RabbitMQModule} from "@golevelup/nestjs-rabbitmq"; | ||
import {RmqDto} from "./rmq.dto"; | ||
import {RmqService} from "./rmq.service"; | ||
import { | ||
DEFAULT_EXCHANGE_NAME, | ||
DEFAULT_EXCHANGE_TYPE, | ||
DEFAULT_PREFETCH_COUNT, | ||
DEFAULT_TIMEOUT, | ||
DEFAULT_WAIT | ||
} from "./rmq.constant"; | ||
|
||
@Module({ | ||
providers: [RmqService], | ||
exports: [RmqService] | ||
}) | ||
export class RmqModule extends RabbitMQModule { | ||
static register(options: RmqDto): DynamicModule { | ||
return { | ||
...super.forRootAsync(RabbitMQModule, { | ||
useFactory: () => ({ | ||
exchanges: [ | ||
{ | ||
name: options.name || DEFAULT_EXCHANGE_NAME, | ||
type: options.type || DEFAULT_EXCHANGE_TYPE, | ||
}, | ||
], | ||
connectionInitOptions: { | ||
wait: options.wait || DEFAULT_WAIT, | ||
timeout: options.timeout || DEFAULT_TIMEOUT, | ||
}, | ||
uri: options.uri, | ||
prefetchCount: options.prefetchCount || DEFAULT_PREFETCH_COUNT, | ||
}), | ||
}), | ||
}; | ||
} | ||
} |
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,34 @@ | ||
import {Injectable} from "@nestjs/common"; | ||
import {AmqpConnection, RequestOptions} from "@golevelup/nestjs-rabbitmq"; | ||
import {DEFAULT_EXCHANGE_NAME, DEFAULT_TIMEOUT} from "./rmq.constant"; | ||
|
||
type MessageOptions = Pick<RequestOptions, | 'payload' | 'routingKey'> & { | ||
exchange?: string, | ||
timeout?: number, | ||
headers?: any | ||
}; | ||
|
||
@Injectable() | ||
export class RmqService { | ||
constructor(private readonly rmq: AmqpConnection) { | ||
} | ||
|
||
async publish(options: MessageOptions) { | ||
const {exchange, routingKey, payload, ...otherOptions} = options; | ||
await this.rmq.publish( | ||
exchange || DEFAULT_EXCHANGE_NAME, | ||
routingKey, | ||
payload, | ||
otherOptions | ||
); | ||
} | ||
|
||
async request<T>(options: MessageOptions) { | ||
const {exchange, timeout} = options; | ||
return await this.rmq.request<T>({ | ||
...options, | ||
exchange: exchange || DEFAULT_EXCHANGE_NAME, | ||
timeout: timeout || DEFAULT_TIMEOUT, | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.