UNPKG

@rxap/nest-rabbitmq

Version:

This package provides a NestJS module for integrating with RabbitMQ using exchanges. It offers a client and server implementation for message queuing and supports features like health checks and error serialization. It simplifies the process of setting up

74 lines (73 loc) 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RabbitmqOptionsFactory = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const utilities_1 = require("@rxap/utilities"); let RabbitmqOptionsFactory = class RabbitmqOptionsFactory { async build() { let options; if (this.config.get('RABBITMQ_DISABLED')) { this.logger.debug('RabbitMQ is disabled', 'RabbitMQModuleConfigFactory'); const host = this.config.getOrThrow('RABBITMQ_HOST'); const port = this.config.getOrThrow('RABBITMQ_PORT'); const vhost = this.config.getOrThrow('RABBITMQ_VHOST'); options = { urls: [`amqp://${host}:${port}/${vhost}`], socketOptions: { reconnectTimeInSeconds: 24 * 60 * 60 } }; } else { this.logger.debug('RabbitMQ is enabled', 'RabbitMQModuleConfigFactory'); options = { socketOptions: { findServers: () => this.buildUrl() } }; } return options; } async getCredentials() { this.logger.verbose('Getting RabbitMQ credentials', 'RabbitMQModuleConfigFactory'); if (this.config.get('RABBITMQ_USERNAME') && this.config.get('RABBITMQ_PASSWORD')) { this.logger.debug('Using username and password from env', 'RabbitMQModuleConfigFactory'); return { username: this.config.getOrThrow('RABBITMQ_USERNAME'), password: this.config.getOrThrow('RABBITMQ_PASSWORD') }; } return null; } async buildUrl() { this.logger.verbose('Building RabbitMQ url', 'RabbitMQModuleConfigFactory'); if (this.config.get('RABBITMQ_URI')) { this.logger.debug('Using uri from env RABBITMQ_URI', 'RabbitMQModuleConfigFactory'); return this.config.getOrThrow('RABBITMQ_URI'); } const credentials = await this.getCredentials(); if (!credentials) { throw new Error('No credentials found for RabbitMQ connection'); } const { username, password } = credentials; const host = this.config.getOrThrow('RABBITMQ_HOST'); const port = this.config.getOrThrow('RABBITMQ_PORT'); const vhost = this.config.getOrThrow('RABBITMQ_VHOST'); this.logger.debug(`Using uri amqp://${username}:*****@${host}:${port}${(0, utilities_1.CoercePrefix)(vhost, '/')}`, 'RabbitMQModuleConfigFactory'); return `amqp://${username}:${password}@${host}:${port}/${vhost}`; } }; exports.RabbitmqOptionsFactory = RabbitmqOptionsFactory; tslib_1.__decorate([ (0, common_1.Inject)(config_1.ConfigService), tslib_1.__metadata("design:type", config_1.ConfigService) ], RabbitmqOptionsFactory.prototype, "config", void 0); tslib_1.__decorate([ (0, common_1.Inject)(common_1.Logger), tslib_1.__metadata("design:type", common_1.Logger) ], RabbitmqOptionsFactory.prototype, "logger", void 0); exports.RabbitmqOptionsFactory = RabbitmqOptionsFactory = tslib_1.__decorate([ (0, common_1.Injectable)() ], RabbitmqOptionsFactory);