mandrill-nodemailer-transport
Version:
Mandrill API and Nodemailer v4+. The plugin is very small, optimized and written in TypeScript
53 lines (52 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Requestly = void 0;
const https_1 = require("https");
var Requestly;
(function (Requestly) {
function sendRequest(options, params) {
return new Promise((resolve, reject) => {
let req = https_1.request(options, (res) => {
let chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
let answer = JsonParse(Buffer.concat(chunks).toString());
if (res.statusCode === 200) {
resolve(answer);
}
else {
reject(answer);
}
});
res.on('error', (error) => {
reject(error);
});
});
req.on('error', (error) => {
reject(error);
});
if (params) {
req.write(params);
}
req.end();
});
}
function postJSON(options, data) {
let json = JSON.stringify(data);
options.method = 'POST';
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(json)
};
return sendRequest(options, json);
}
Requestly.postJSON = postJSON;
})(Requestly = exports.Requestly || (exports.Requestly = {}));
function JsonParse(value) {
try {
return JSON.parse(value);
}
catch (error) {
return undefined;
}
}