UNPKG

@rxap/nest-amqp

Version:
88 lines (87 loc) 3.93 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 { config; logger; 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: await this.getSocketOptions(), }; } return options; } async getSocketOptions() { const socketOptions = { findServers: () => this.buildUrl(), }; if (this.config.get('RABBITMQ_HEARTBEAT_INTERVAL')) { socketOptions.heartbeatIntervalInSeconds = this.config.getOrThrow('RABBITMQ_HEARTBEAT_INTERVAL'); } if (this.config.get('RABBITMQ_RECONNECT_TIME')) { socketOptions.reconnectTimeInSeconds = this.config.getOrThrow('RABBITMQ_RECONNECT_TIME'); } return socketOptions; } 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'); this.logger.debug(`Using connection url 'amqp://${username}:*******@${host}:${port}/${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);