@httpc/kit
Version:
httpc toolbox for building function-based API with minimal code and end-to-end type safety
100 lines (99 loc) • 4.55 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailersendEmailSender = void 0;
const assert_1 = __importDefault(require("assert"));
const tsyringe_1 = require("tsyringe");
const fetch_1 = require("../fetch");
const services_1 = require("../services");
const di_1 = require("../di");
const logging_1 = require("../logging");
const utils_1 = require("../utils");
const MAILERSEND_API = "https://api.mailersend.com/v1";
let MailersendEmailSender = class MailersendEmailSender extends (0, services_1.BaseService)() {
constructor(logger, options) {
//@ts-expect-error
super(...arguments);
this.options = options;
}
async send(params) {
(0, assert_1.default)(!!(params.bodyHtml || params.bodyText), "one of bodyHtml and bodyText is required");
if (Array.isArray(params.to)) {
(0, assert_1.default)(params.to.length > 0, "options.to must have at least an item");
}
const to = mapEmailRecipient(params.to);
const from = params.from ? mapEmailRecipient(params.from)[0] :
this.options.defaultSender ? mapEmailRecipient(this.options.defaultSender) :
undefined;
const request = (0, utils_1.cleanUndefined)({
to,
from,
html: params.bodyHtml,
text: params.bodyText,
subject: params.subject,
cc: params.cc ? mapEmailRecipient(params.cc) : undefined,
bcc: params.bcc ? mapEmailRecipient(params.bcc) : undefined,
});
if (this.logger.isLevelEnabled("debug")) {
this.logger.debug("Email params: %o", { ...request, bodyHtml: "<omitted>", bodyText: "<omitted>" });
}
await this._sendMail(request);
this.logger.verbose("Email sent(%s)", to[0].email);
}
async _sendMail(params) {
(0, assert_1.default)(this.options.apiKey, "ApiKey must be set");
try {
const token = this.options.apiKey;
const response = await (0, fetch_1.fetch)(`${MAILERSEND_API}/email`, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json"
},
body: JSON.stringify(params)
});
if (response.status >= 400) {
throw response;
}
}
catch (ex) {
const error = ex instanceof Error ? ex : undefined;
const response = ex instanceof Error ? undefined : ex;
const body = response ? await response.json().catch(() => null) : undefined;
this._raiseError("processing_error", "Email send failed", {
errorMessage: error && `[${error.name}] ${error.message}`,
httpStatus: response?.status,
httpResponse: body
});
}
}
};
MailersendEmailSender = __decorate([
(0, tsyringe_1.injectable)(),
__param(0, (0, logging_1.logger)()),
__param(1, (0, di_1.options)()),
__metadata("design:paramtypes", [Object, Object])
], MailersendEmailSender);
exports.MailersendEmailSender = MailersendEmailSender;
function mapEmailRecipient(r) {
if (!Array.isArray(r)) {
return mapEmailRecipient([r]);
}
return r.map(x => (typeof x === "string" ?
{ email: x } :
x));
}