feathers-mailgun
Version:
Feathers Mailgun Service
110 lines (83 loc) • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.default = init;
var _feathersErrors = require('feathers-errors');
var _feathersErrors2 = _interopRequireDefault(_feathersErrors);
var _mailgunJs = require('mailgun-js');
var _mailgunJs2 = _interopRequireDefault(_mailgunJs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
if (!global._babelPolyfill) {
require('babel-polyfill');
}
var Service = function () {
function Service() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, Service);
if (!options.apiKey) {
throw new Error('Mailgun `apiKey` needs to be provided');
}
if (!options.domain) {
throw new Error('Mailgun `domain` needs to be provided');
}
this.options = options;
this.mailgun = new _mailgunJs2.default({ apiKey: options.apiKey, domain: options.domain });
}
_createClass(Service, [{
key: '_send',
value: function _send(data, callback) {
return this.mailgun.messages().send(data, callback);
}
}, {
key: 'create',
value: function create(data) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._validateParams(data);
_this._send(_this._formatData(data), function (err, body) {
if (err) {
return reject(err);
} else {
return resolve(body);
}
});
});
}
}, {
key: '_validateParams',
value: function _validateParams(data) {
if (!data.from) {
throw new _feathersErrors2.default.BadRequest('`from` must be specified');
}
if (!data.to) {
throw new _feathersErrors2.default.BadRequest('`to` must be specified');
}
if (!data.subject) {
throw new _feathersErrors2.default.BadRequest('`subject` must be specified');
}
if (!data.html) {
throw new _feathersErrors2.default.BadRequest('`html` must be specified');
}
}
// Convert array of emails to comma delimited if needed
}, {
key: '_formatData',
value: function _formatData(data) {
var to = data.to;
if (_typeof(data.to) === 'object') {
to = data.to.join(',');
}
return Object.assign(data, { to: to });
}
}]);
return Service;
}();
function init(options) {
return new Service(options);
}
init.Service = Service;
module.exports = exports['default'];