@rxap/nest-amqp
Version:
@rxap/nest-amqp
88 lines (87 loc) • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmqpOptionsFactory = 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 AmqpOptionsFactory = class AmqpOptionsFactory {
config;
logger;
async create() {
let options;
if (this.config.get('AMQP_DISABLED')) {
this.logger.debug('Amqp is disabled', 'AmqpOptionsFactory');
const host = this.config.getOrThrow('AMQP_HOST');
const port = this.config.getOrThrow('AMQP_PORT');
const vhost = this.config.getOrThrow('AMQP_VHOST');
options = {
connection: {
findServers: () => `amqp://${host}:${port}/${vhost}`,
reconnectTimeInSeconds: 24 * 60 * 60
},
queue: await this.getQueue(),
exchange: await this.getExchange(),
};
}
else {
this.logger.debug('Amqp is enabled', 'AmqpOptionsFactory');
options = {
connection: {
findServers: () => this.buildUrl()
},
queue: await this.getQueue(),
exchange: await this.getExchange(),
};
}
options.channel ??= {};
options.channel.prefetchCount = this.config.get('AMQP_PREFETCH_COUNT', 100);
return options;
}
async getQueue() {
return undefined;
}
async getExchange() {
return undefined;
}
async getCredentials() {
this.logger.verbose('Getting Amqp credentials', 'AmqpOptionsFactory');
if (this.config.get('AMQP_USERNAME') && this.config.get('AMQP_PASSWORD')) {
this.logger.debug('Using username and password from env', 'AmqpOptionsFactory');
return {
username: this.config.getOrThrow('AMQP_USERNAME'),
password: this.config.getOrThrow('AMQP_PASSWORD')
};
}
return null;
}
async buildUrl() {
this.logger.verbose('Building Amqp url', 'AmqpOptionsFactory');
if (this.config.get('AMQP_URI')) {
this.logger.debug('Using uri from env AMQP_URI', 'AmqpOptionsFactory');
return this.config.getOrThrow('AMQP_URI');
}
const credentials = await this.getCredentials();
if (!credentials) {
throw new Error('No credentials found for Amqp connection');
}
const { username, password } = credentials;
const host = this.config.getOrThrow('AMQP_HOST');
const port = this.config.getOrThrow('AMQP_PORT');
const vhost = this.config.getOrThrow('AMQP_VHOST');
this.logger.debug(`Using uri amqp://${username}:*****@${host}:${port}${(0, utilities_1.CoercePrefix)(vhost, '/')}`, 'AmqpOptionsFactory');
return `amqp://${username}:${password}@${host}:${port}/${vhost}`;
}
};
exports.AmqpOptionsFactory = AmqpOptionsFactory;
tslib_1.__decorate([
(0, common_1.Inject)(config_1.ConfigService),
tslib_1.__metadata("design:type", config_1.ConfigService)
], AmqpOptionsFactory.prototype, "config", void 0);
tslib_1.__decorate([
(0, common_1.Inject)(common_1.Logger),
tslib_1.__metadata("design:type", common_1.Logger)
], AmqpOptionsFactory.prototype, "logger", void 0);
exports.AmqpOptionsFactory = AmqpOptionsFactory = tslib_1.__decorate([
(0, common_1.Injectable)()
], AmqpOptionsFactory);