@incdevco/framework
Version:
node.js lambda framework
91 lines (54 loc) • 1.55 kB
JavaScript
var AWS = require('aws-sdk');
function Producer(config) {
config = config || {};
this.queueUrl = config.queueUrl;
this.sns = config.sns || new AWS.SNS();
this.sqs = config.sqs || new AWS.SQS();
this.useSns = (config.useSns) ? true : false;
this.topicArn = config.topicArn;
}
Producer.prototype.publish = function (name, data, id) {
var action = {
data: data,
id: id,
name: name
};
if (this.useSns) {
return this._withSns(action);
} else {
return this._withSqs(action);
}
};
Producer.prototype._withSns = function (action) {
var params = {
Message: JSON.stringify(action),
TopicArn: this.topicArn
};
console.log('sns.publish', JSON.stringify(params, null, 2));
return this.sns.publish(params).promise()
.then(function (result) {
console.log('result', result);
return result;
})
.catch(function (exception) {
console.log('exception', exception);
throw exception;
});
};
Producer.prototype._withSqs = function (action) {
var params = {
MessageBody: JSON.stringify(action),
QueueUrl: this.queueUrl
};
console.log('sqs.sendMessage', JSON.stringify(params, null, 2));
return this.sqs.sendMessage(params).promise()
.then(function (result) {
console.log('result', result);
return result;
})
.catch(function (exception) {
console.error('exception', exception);
throw exception;
});
};
module.exports = Producer;