@vtexlab/planner-message-bus
Version:
A Message Bus that uses AWS SNS, AWS SQS, and AWS EventBridge
40 lines (39 loc) • 1.88 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, TagResourceCommand, CreateTopicCommand, SubscribeCommand } from '@aws-sdk/client-sns';
const client = new SNSClient();
export function createSnsTopic(topicName) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const commandOutput = yield client.send(new CreateTopicCommand({
Name: topicName
}));
return (_a = commandOutput.TopicArn) !== null && _a !== void 0 ? _a : TOPIC_ARN_TEMPLATE(topicName);
});
}
export function tagTopic(topicArn, tags) {
return __awaiter(this, void 0, void 0, function* () {
yield client.send(new TagResourceCommand({
ResourceArn: topicArn,
Tags: tags
}));
});
}
export function subscribeSnsTopicToQueue(topicName, queueName) {
return __awaiter(this, void 0, void 0, function* () {
const commandOutput = yield client.send(new SubscribeCommand({
TopicArn: TOPIC_ARN_TEMPLATE(topicName),
Endpoint: QUEUE_ARN_TEMPLATE(queueName),
Protocol: 'sqs'
}));
return commandOutput.SubscriptionArn;
});
}