UNPKG

@visulima/email

Version:

A comprehensive email library with multi-provider support, crypto utilities, and template engines

2 lines (1 loc) 3.11 kB
var w=Object.defineProperty;var b=(e,l)=>w(e,"name",{value:l,configurable:!0});import u from"./EmailError-zm2ffVav.js";import $ from"./RequiredOptionError-CevW3u2K.js";import A from"./generateMessageId-11Ls5JsR.js";import{defineProvider as E}from"./defineProvider-B9rSklAJ.js";import z from"./createLogger-DlElSVQP.js";var P=Object.defineProperty,p=b((e,l)=>P(e,"name",{value:l,configurable:!0}),"v");const k=p(e=>typeof e=="function","isProviderFactory"),F=p(e=>e!==null&&typeof e=="object"&&"sendEmail"in e&&"initialize"in e&&"isAvailable"in e,"isProvider"),o="failover",C=E((e={})=>{if(!e.mailers||e.mailers.length===0)throw new $(o,"mailers");const l={debug:e.debug||!1,mailers:e.mailers,retryAfter:e.retryAfter??60};let h=!1;const t=[],s=z(o,e.logger);let g;const m=p(async()=>g||(g=(async()=>{t.length=0;for(const a of l.mailers)try{let r;if(k(a))r=a({});else if(F(a))r=a;else{s.debug(`Skipping invalid mailer: ${a}`);continue}try{await r.initialize(),t.push(r),s.debug(`Initialized provider: ${r.name||"unknown"}`)}catch(c){s.debug(`Failed to initialize provider ${r.name||"unknown"}:`,c)}}catch(r){s.debug("Error processing mailer:",r)}if(t.length===0)throw new u(o,"No providers could be initialized")})(),g),"initializeProviders");return{features:{attachments:!0,batchSending:!1,customHeaders:!0,html:!0,replyTo:!0,scheduling:!1,tagging:!1,templates:!1,tracking:!1},async initialize(){if(!h)try{await m(),h=!0,s.debug(`Failover provider initialized with ${t.length} provider(s)`)}catch(a){throw new u(o,`Failed to initialize: ${a.message}`,{cause:a})}},async isAvailable(){try{t.length===0&&await m();for(const a of t)try{if(await a.isAvailable())return!0}catch{}return!1}catch{return!1}},name:o,options:l,async sendEmail(a){try{if(t.length===0&&await m(),t.length===0)return{error:new u(o,"No providers available"),success:!1};const r=[];let c;for(let n=0;n<t.length;n+=1){const f=t[n];if(!f)continue;const d=f.name||`provider-${n+1}`;s.debug(`Attempting to send email via ${d} (${n+1}/${t.length})`);try{if(!await f.isAvailable()){s.debug(`Provider ${d} is not available, skipping`),r.push(new u(o,`Provider ${d} is not available`));continue}const i=await f.sendEmail(a);if(i.success&&i.data)return s.debug(`Email sent successfully via ${d}`),{data:{...i.data,messageId:i.data.messageId||A(),provider:`${o}(${d})`},success:!0};if(i.error){c=i.error,r.push(i.error);const v=i.error instanceof Error?i.error.message:String(i.error);s.debug(`Failed to send via ${d}:`,v)}}catch(i){c=i instanceof Error?i:new Error(String(i)),r.push(c);const v=c instanceof Error?c.message:String(c);s.debug(`Exception sending via ${d}:`,v)}n<t.length-1&&l.retryAfter>0&&(s.debug(`Waiting ${l.retryAfter}ms before trying next provider`),await new Promise(i=>{setTimeout(()=>{i()},l.retryAfter)}))}const y=r.map(n=>n instanceof Error?n.message:String(n)).join("; ");return{error:new u(o,`All providers failed. Errors: ${y}`,{cause:c}),success:!1}}catch(r){return{error:new u(o,`Failed to send email: ${r.message}`,{cause:r}),success:!1}}},async validateCredentials(){return this.isAvailable()}}});export{C as default};