@email-service/email-service
Version:
email-service is a versatile npm package designed to simplify the integration and standardization of email communications across multiple Email Service Providers (ESPs).
64 lines (63 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ESP = void 0;
const normalizeEmailRecipients_js_1 = require("../utils/normalizeEmailRecipients.js");
const rateLimit_js_1 = require("../utils/rateLimit.js");
const bulkRunner_js_1 = require("../utils/bulkRunner.js");
class ESP {
constructor(service, opts) {
this.mailMultiple = false;
this.transporter = service;
this.rateLimiter = (0, rateLimit_js_1.createRateLimiter)(service.esp, service.rateLimit);
this.hooks = opts?.hooks;
if (this.transporter.logger)
console.log('******** ES ******** New Instance of ', this.transporter.esp);
}
checkRecipients(to) {
return (0, normalizeEmailRecipients_js_1.normalizeRecipients)(to);
}
checkFrom(from) {
return (0, normalizeEmailRecipients_js_1.normalizeFrom)(from);
}
/**
* Envoi public : applique le rate limit puis délègue à `doSendMail`
* implémenté par chaque ESP concret (template method). Les ESP
* concrets ne doivent PAS override `sendMail` directement, sinon
* ils contourneraient le throttle.
*/
async sendMail(options) {
if (this.rateLimiter) {
const waited = await this.rateLimiter.acquire();
if (waited > 0 && this.transporter.logger) {
console.log(`******** ES ******** rate limited ${this.transporter.esp}, waited ${waited}ms`);
}
}
return this.doSendMail(options);
}
/**
* Envoi en lot avec suppression list, stream (transactional|marketing),
* templating par destinataire et hooks de persistance.
*
* Implémentation unique sur la classe de base — réutilise `sendMail()`
* pour chaque destinataire, ce qui fait bénéficier chaque envoi du
* rate limit automatiquement.
*
* Les règles de blocage (transactional vs marketing) sont appliquées
* par `runBulk` en interne — le consommateur implémente juste un
* `checkSuppression` qui retourne la reason brute.
*/
async sendBulk(payload) {
return (0, bulkRunner_js_1.runBulk)(payload, this.hooks, (p) => this.sendMail(p));
}
/** À implémenter par chaque ESP concret. */
async doSendMail(options) {
return ({ success: false, status: 500, error: { name: 'NO_METHOD_sendMail', message: 'This function do never to be call, contact the developper' } });
}
async webHookManagement(req) {
return ({ success: false, status: 500, error: { name: 'NO_METHOD_webHookManagement', message: 'This function do never to be call, contact the developper' } });
}
async sendMailMultiple(options) {
return ([{ success: false, status: 500, error: { name: 'NO_METHOD_sendMail', message: 'This function do never to be call, contact the developper' } }]);
}
}
exports.ESP = ESP;