UNPKG

@jiaxinjiang/nest-amqp

Version:

RabbitMQ component for NestJs.

57 lines 2.91 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash = require("lodash"); const common_1 = require("@nestjs/common"); const amqp_service_1 = require("./amqp.service"); const amqp_constants_1 = require("./amqp.constants"); function defaultEncode(msg) { return Buffer.from(JSON.stringify(msg)); } let Publisher = class Publisher { constructor(amqpService) { this.amqpService = amqpService; this.amqpConfig = this.amqpService.config; this.channel = this.amqpService.channel; this.logger = this.amqpConfig.logger || new common_1.Logger(amqp_service_1.AmqpService.name); } async setExchange(exchange) { this.exchangeOptions = this.amqpConfig.exchanges.find(option => option.exchange === exchange); const { type, options } = this.exchangeOptions; await this.channel.assertExchange(exchange, type || 'direct', options); this.logger.log(`[Publisher -> exchange:${exchange}] initialized`); } publish(params) { const { publish: publishConfig, exchange } = this.exchangeOptions; const content = lodash.isString(params) ? params : params.msg; const encodeFn = this.exchangeOptions.encode || defaultEncode; const data = lodash.isString(params) ? { msg: content, routingKey: '', encode: encodeFn, publishOptions: publishConfig, } : params; const { msg, routingKey = '', encode = encodeFn, publishOptions = publishConfig, callback, } = data; return this.channel.publish(exchange, routingKey, encode(msg), publishOptions, callback); } }; Publisher = __decorate([ common_1.Injectable({ scope: common_1.Scope.TRANSIENT }), __param(0, common_1.Inject(amqp_constants_1.AMQP_SERVICE)), __metadata("design:paramtypes", [amqp_service_1.AmqpService]) ], Publisher); exports.Publisher = Publisher; //# sourceMappingURL=publisher.service.js.map