UNPKG

nodebasecli

Version:

Cli to create modules for nodebase

88 lines (78 loc) 3.09 kB
const fileData = `/** * @author "Abdul Quadir Dewaswala" * @license MIT * @version 1.0 * This module contains functions for interacting with AWS SQS. * It uses the AWS SDK for JavaScript in Node.js to interact with SQS. * The functions are: * triggerSqs: sends a message to an SQS queue * triggerBatchSqs: sends a batch of messages to an SQS queue * recieveSqs: receives a message from an SQS queue * The functions are all asynchronous and return promises. * If an error occurs, the promise is rejected with the error. */ import SQS from 'aws-sdk/clients/sqs'; // Create an SQS client with the region and credentials // specified in the environment variables. const sqsClient = new SQS({ region: process.env.AWS_REGION, // credentials: { // accessKeyId: process.env.AWS_ACCESS_KEY_ID, // secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY // }, apiVersion: '2012-11-05', }); /** * Sends a message to an SQS queue. * @param {Object} message - the message to send to the queue * @param {string} queueUrl - the URL of the SQS queue * @return {Promise} - a promise that resolves if the message is sent successfully */ const triggerSqs = async (message, queueUrl) => { // Create a params object with the message and queue URL const params = { MessageBody: JSON.stringify(message), QueueUrl: queueUrl }; // Use the SQS client to send the message to the queue // The promise is returned, so the caller can handle the result return sqsClient.sendMessage(params).promise().catch(err => console.log(err)); }; /** * Sends a batch of messages to an SQS queue. * @param {Array} messages - the messages to send to the queue * @param {string} queueUrl - the URL of the SQS queue * @return {Promise} - a promise that resolves if the messages are sent successfully */ const triggerBatchSqs = async (messages, queueUrl) => { // Create a params object with the messages and queue URL const params = { Entries: messages.map(message => ({ MessageBody: JSON.stringify(message), QueueUrl: queueUrl })) }; // Use the SQS client to send the messages to the queue // The promise is returned, so the caller can handle the result return sqsClient.sendMessageBatch(params).promise().catch(err => console.log(err)); }; /** * Receives a message from an SQS queue. * @param {string} queueUrl - the URL of the SQS queue * @return {Promise} - a promise that resolves if a message is received successfully */ const recieveSqs = async (queueUrl) => { // Create a params object with the queue URL const params = { QueueUrl: queueUrl }; // Use the SQS client to receive a message from the queue // The promise is returned, so the caller can handle the result return sqsClient.receiveMessage(params).promise().catch(err => console.log(err)); }; export { triggerSqs, triggerBatchSqs, recieveSqs }` module.exports = fileData