-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmq.service.ts
34 lines (30 loc) · 1007 Bytes
/
rmq.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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,
});
}
}