UNPKG

@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).

60 lines (59 loc) 2.67 kB
import { normalizeFrom, normalizeRecipients } from "../utils/normalizeEmailRecipients.js"; import { createRateLimiter } from "../utils/rateLimit.js"; import { runBulk } from "../utils/bulkRunner.js"; export class ESP { constructor(service, opts) { this.mailMultiple = false; this.transporter = service; this.rateLimiter = 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 normalizeRecipients(to); } checkFrom(from) { return 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 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' } }]); } }