event-message-broker
Version:
A Message Bus that uses AWS Stack and RabbitMQ
73 lines (72 loc) • 3.24 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());
});
};
import { QUEUE_ARN_TEMPLATE, TOPIC_ARN_TEMPLATE } from '../utils/constants';
import { SNSClient, GetTopicAttributesCommand, CreateTopicCommand, TagResourceCommand, SubscribeCommand } from '@aws-sdk/client-sns';
import { Configuration } from '../../application/utils/configurations';
export class SNSInfrastructure {
constructor() {
this.client = new 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 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: TOPIC_ARN_TEMPLATE(topicName),
Created: !exists
};
});
}
subscribeEndpoints(topicName, queueName) {
return __awaiter(this, void 0, void 0, function* () {
const command = new SubscribeCommand({
TopicArn: TOPIC_ARN_TEMPLATE(topicName),
Endpoint: 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 (!Configuration.tags || Configuration.tags.length < 1) {
return;
}
const command = new TagResourceCommand({
ResourceArn: TOPIC_ARN_TEMPLATE(topicName),
Tags: Configuration.tags
});
yield this.client.send(command);
return Configuration.tags;
});
}
check(topicName) {
return __awaiter(this, void 0, void 0, function* () {
try {
const topicArn = TOPIC_ARN_TEMPLATE(topicName);
const command = new GetTopicAttributesCommand({ TopicArn: topicArn });
yield this.client.send(command);
return true;
}
catch (error) {
return false;
}
});
}
}