event-message-broker
Version:
A Message Bus that uses AWS Stack and RabbitMQ
60 lines (59 loc) • 2.81 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageBus = void 0;
const AWSBus_1 = require("./aws-provider/services/AWSBus");
const configurations_1 = require("./application/utils/configurations");
const AWSInfrastructure_1 = require("./aws-provider/infrastructure/AWSInfrastructure");
const RabbitMqInfrastructure_1 = require("./rabbitmq-provider/infrastructure/RabbitMqInfrastructure");
const RabbitMQBus_1 = require("./rabbitmq-provider/services/RabbitMQBus");
const RabbitMqContext_1 = require("./rabbitmq-provider/infrastructure/RabbitMqContext");
class MessageBus {
static useAws() {
this.infrastructure = new AWSInfrastructure_1.AWSInfrastructure();
this.bus = new AWSBus_1.AWSBus();
return this;
}
static useRabbitMq() {
return __awaiter(this, void 0, void 0, function* () {
const channel = yield RabbitMqContext_1.RabbitMqContext.configureContext();
this.infrastructure = new RabbitMqInfrastructure_1.RabbitMqInfrastructure(channel);
this.bus = new RabbitMQBus_1.RabbitMQBus(channel);
return this;
});
}
static configureTags(tags) {
configurations_1.Configuration.pushTags(tags);
return this;
}
static configurePrefetch(prefetch) {
configurations_1.Configuration.configurePrefetch(prefetch);
return this;
}
static get Bus() {
return this.bus;
}
static configureEndpoints(endpoints) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.infrastructure || !this.bus) {
throw new Error('Need to define what provider will be use');
}
yield Promise.all(endpoints.map((endpoint) => __awaiter(this, void 0, void 0, function* () {
yield this.infrastructure.createQueue(endpoint.Queue);
yield this.infrastructure.bindTopic({
QueueName: endpoint.Queue,
TopicName: endpoint.Topic
});
})));
});
}
}
exports.MessageBus = MessageBus;