Skip to content

Commit

Permalink
improve codes and add dto and constant
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Sardari committed Dec 17, 2022
1 parent 581933e commit bf5ab9c
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 115 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gadin-rabbit",
"version": "0.0.1",
"version": "1.0.0",
"description": "A module for quick setup and convenient use of RabbitMQ in nestjs.",
"main": "dist/index.js",
"scripts": {
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
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';
5 changes: 5 additions & 0 deletions src/rmq.constant.ts
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;
21 changes: 21 additions & 0 deletions src/rmq.decorator.ts
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,
});
8 changes: 8 additions & 0 deletions src/rmq.dto.ts
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
}
38 changes: 38 additions & 0 deletions src/rmq.module.ts
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,
}),
}),
};
}
}
34 changes: 34 additions & 0 deletions src/rmq.service.ts
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,
});
}
}
1 change: 0 additions & 1 deletion src/rmq/rmq-handler.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/rmq/rmq.decorator.ts

This file was deleted.

66 changes: 0 additions & 66 deletions src/rmq/rmq.module.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/rmq/rmq.service.ts

This file was deleted.

0 comments on commit bf5ab9c

Please sign in to comment.