mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const awsRequest = require('@serverless/test/aws-request');
|
|
const SNSService = require('aws-sdk').SNS;
|
|
|
|
function createSnsTopic(topicName) {
|
|
const params = {
|
|
Name: topicName,
|
|
};
|
|
|
|
return awsRequest(SNSService, 'createTopic', params);
|
|
}
|
|
|
|
function resolveTopicArn(topicName, nextToken = null) {
|
|
return awsRequest(SNSService, 'listTopics', { NextToken: nextToken }).then((data) => {
|
|
const targetTopic = data.Topics.find((topic) => RegExp(topicName, 'g').test(topic.TopicArn));
|
|
|
|
if (targetTopic) return targetTopic.TopicArn;
|
|
|
|
if (data.NextToken) return resolveTopicArn(topicName, data.NextToken);
|
|
return null;
|
|
});
|
|
}
|
|
|
|
function removeSnsTopic(topicName) {
|
|
return resolveTopicArn(topicName).then((topicArn) => {
|
|
const params = {
|
|
TopicArn: topicArn,
|
|
};
|
|
|
|
return awsRequest(SNSService, 'deleteTopic', params);
|
|
});
|
|
}
|
|
|
|
function publishSnsMessage(topicName, message, messageAttributes = null) {
|
|
return resolveTopicArn(topicName).then((topicArn) => {
|
|
const params = {
|
|
Message: message,
|
|
TopicArn: topicArn,
|
|
};
|
|
if (messageAttributes) {
|
|
params.MessageAttributes = messageAttributes;
|
|
}
|
|
|
|
return awsRequest(SNSService, 'publish', params);
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
createSnsTopic,
|
|
removeSnsTopic,
|
|
publishSnsMessage,
|
|
};
|