UNPKG

event-message-broker

Version:

A Message Bus that uses AWS Stack and RabbitMQ

77 lines (76 loc) 3.5 kB
"use strict"; 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.SNSInfrastructure = void 0; const constants_1 = require("../utils/constants"); const client_sns_1 = require("@aws-sdk/client-sns"); const configurations_1 = require("../../application/utils/configurations"); class SNSInfrastructure { constructor() { this.client = new client_sns_1.SNSClient(); } create(topicName, span) { return __awaiter(this, void 0, void 0, function* () { span.setAttribute('topic', topicName); const exists = yield this.check(topicName); span.addEvent(exists ? 'topic-already-created' : 'topic-does-not-exists'); if (!exists) { const command = new client_sns_1.CreateTopicCommand({ Name: topicName }); const output = yield this.client.send(command); span.addEvent('topic-created', { arn: output.TopicArn }); const tags = yield this.tag(topicName); span.addEvent('topic-tags-attached', { tags: JSON.stringify(tags) }); } return { TopicArn: (0, constants_1.TOPIC_ARN_TEMPLATE)(topicName), Created: !exists }; }); } subscribeEndpoints(topicName, queueName) { return __awaiter(this, void 0, void 0, function* () { const command = new client_sns_1.SubscribeCommand({ TopicArn: (0, constants_1.TOPIC_ARN_TEMPLATE)(topicName), Endpoint: (0, constants_1.QUEUE_ARN_TEMPLATE)(queueName), Protocol: 'sqs' }); const output = yield this.client.send(command); return output.SubscriptionArn; }); } tag(topicName) { return __awaiter(this, void 0, void 0, function* () { if (!configurations_1.Configuration.tags || configurations_1.Configuration.tags.length < 1) { return; } const command = new client_sns_1.TagResourceCommand({ ResourceArn: (0, constants_1.TOPIC_ARN_TEMPLATE)(topicName), Tags: configurations_1.Configuration.tags }); yield this.client.send(command); return configurations_1.Configuration.tags; }); } check(topicName) { return __awaiter(this, void 0, void 0, function* () { try { const topicArn = (0, constants_1.TOPIC_ARN_TEMPLATE)(topicName); const command = new client_sns_1.GetTopicAttributesCommand({ TopicArn: topicArn }); yield this.client.send(command); return true; } catch (error) { return false; } }); } } exports.SNSInfrastructure = SNSInfrastructure;