nodebasecli
Version:
Cli to create modules for nodebase
158 lines (141 loc) • 4.99 kB
JavaScript
const fileData= `/**
* @author "Abdul Quadir Dewaswala"
* @license MIT
* @version 1.0
* This module contains functions for interacting with AWS SNS.
* It uses the AWS SDK for JavaScript in Node.js to interact with SNS.
* The functions are:
* triggerSnsTopic: sends a message to a topic
* triggerSnsSms: sends a message to a phone number
* triggerSnsEmail: sends a message to an email
* subscribeSnsTopic: subscribes an endpoint to a topic
* unsubscribeSnsTopic: unsubscribes an endpoint from a topic
* The functions are all asynchronous and return promises.
* If an error occurs, the promise is rejected with the error.
*/
import SNS from 'aws-sdk/clients/sns';
/**
* Creates an SNS client with the region and credentials
* specified in the environment variables.
*/
const snsClient = new SNS({
region: process.env.AWS_REGION,
// credentials: {
// accessKeyId: process.env.AWS_ACCESS_KEY_ID,
// secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
// },
apiVersion: '2010-03-31',
});
/**
* Sends a message to an SNS topic.
* @param {Object} params - the parameters for the SNS publish function
* @return {Promise} - a promise that resolves if the message is sent successfully
*/
const triggerSns = async (params) => {
/**
* Calls the publish function on the SNS client with the given parameters.
* The promise is returned, so the caller can handle the result.
*/
return snsClient.publish(params).promise().catch(err => console.log(err));
};
/**
* Subscribes an endpoint to an SNS topic.
* @param {Object} params - the parameters for the SNS subscribe function
* @return {Promise} - a promise that resolves if the subscription is successful
*/
const subscribeSns = async (params) => {
/**
* Calls the subscribe function on the SNS client with the given parameters.
* The promise is returned, so the caller can handle the result.
*/
return snsClient.subscribe(params).promise().catch(err => console.log(err));
};
/**
* Sends a message to an SNS topic.
* @param {string} message - the message to send
* @param {string} topicArn - the ARN of the topic to send the message to
* @return {Promise} - a promise that resolves if the message is sent successfully
*/
const triggerSnsTopic = async (message, topicArn) => {
/**
* Creates an object with the message and topic ARN.
* Calls the triggerSns function with the object.
*/
const params = {
Message: message,
TopicArn: topicArn
}
return triggerSns(params);
};
/**
* Sends a message to a phone number.
* @param {string} message - the message to send
* @param {string} phoneNumber - the phone number to send the message to
* @return {Promise} - a promise that resolves if the message is sent successfully
*/
const triggerSnsSms = async (message, phoneNumber) => {
/**
* Creates an object with the message and phone number.
* Calls the triggerSns function with the object.
*/
const params = {
Message: message,
PhoneNumber: phoneNumber
}
return triggerSns(params);
};
/**
* Sends a message to an email.
* @param {string} message - the message to send
* @param {string} email - the email to send the message to
* @return {Promise} - a promise that resolves if the message is sent successfully
*/
const triggerSnsEmail = async (message, email) => {
/**
* Creates an object with the message and email.
* Calls the triggerSns function with the object.
*/
const params = {
Message: message,
TopicArn: email
}
return triggerSns(params);
};
/**
* Subscribes an endpoint to an SNS topic.
* @param {string} topicArn - the ARN of the topic to subscribe to
* @return {Promise} - a promise that resolves if the subscription is successful
*/
const subscribeSnsTopic = async (topicArn) => {
/**
* Creates an object with the topic ARN.
* Calls the subscribeSns function with the object.
*/
const params = {
TopicArn: topicArn
}
return subscribeSns(params);
};
/**
* Unsubscribes an endpoint from an SNS topic.
* @param {string} subscriptionArn - the ARN of the subscription to unsubscribe
* @return {Promise} - a promise that resolves if the unsubscription is successful
*/
const unsubscribeSnsTopic = async (subscriptionArn) => {
/**
* Creates an object with the subscription ARN.
* Calls the unsubscribe function on the SNS client with the object.
*/
const params = {
SubscriptionArn: subscriptionArn
}
return snsClient.unsubscribe(params).promise().catch(err => console.log(err));
};
export {
triggerSnsTopic,
triggerSnsSms,
triggerSnsEmail,
subscribeSnsTopic,
unsubscribeSnsTopic
}`
module.exports = fileData