@visulima/email
Version:
A comprehensive email library with multi-provider support, crypto utilities, and template engines
2 lines (1 loc) • 2.72 kB
JavaScript
var v=Object.defineProperty;var p=(r,a)=>v(r,"name",{value:a,configurable:!0});import d from"./EmailError-zm2ffVav.js";import A from"./RequiredOptionError-CevW3u2K.js";import j from"./generateMessageId-11Ls5JsR.js";import{makeRequest as u}from"./makeRequest-DwxHX0xo.js";import O from"./validateEmailOptions-BzlJECG5.js";import{defineProvider as P}from"./defineProvider-B9rSklAJ.js";var C=Object.defineProperty,h=p((r,a)=>C(r,"name",{value:a,configurable:!0}),"f");const n="http",I="POST",f=3e4,z=P((r={})=>{if(!r.endpoint)throw new A(n,"endpoint");const a={apiKey:r.apiKey||"",endpoint:r.endpoint,headers:r.headers||{},method:r.method||I},m=h(()=>{const e={"Content-Type":"application/json",...a.headers};return a.apiKey&&(e.Authorization=`Bearer ${a.apiKey}`),e},"getStandardHeaders"),y=h(e=>{const t={from:e.from.email,from_name:e.from.name,html:e.html,subject:e.subject,text:e.text,to:Array.isArray(e.to)?e.to.map(s=>s.email):e.to.email};return e.cc&&(t.cc=Array.isArray(e.cc)?e.cc.map(s=>s.email):e.cc.email),e.bcc&&(t.bcc=Array.isArray(e.bcc)?e.bcc.map(s=>s.email):e.bcc.email),e.customParams&&Object.assign(t,e.customParams),t},"formatRequest");let l=!1;return{features:{attachments:!1,batchSending:!1,customHeaders:!0,html:!0,replyTo:!1,scheduling:!1,tagging:!1,templates:!1,tracking:!1},async initialize(){if(!l){if(!await this.isAvailable())throw new d(n,"API endpoint not available");l=!0}},async isAvailable(){try{return!!(await u(a.endpoint,{headers:m(),method:"OPTIONS",timeout:f})).success}catch(e){if(e instanceof Error){const t=e.message;if(t.includes("401")||t.includes("403"))return!1}return!1}},name:n,options:a,async sendEmail(e){try{const t=O(e);if(t.length>0)return{error:new d(n,`Invalid email options: ${t.join(", ")}`),success:!1};l||await this.initialize();const s=m();e.headers&&Object.assign(s,e.headers);const b=y(e),g=e.endpointOverride||a.endpoint,w=e.methodOverride||a.method,o=await u(g,{headers:s,method:w,timeout:f},JSON.stringify(b));if(!o.success)return{error:new d(n,`Failed to send email: ${o.error?.message||"Unknown error"}`,{cause:o.error}),success:!1};let c;const i=o.data?.body;return i&&typeof i=="object"&&(c=i.id||i.messageId||i.data&&typeof i.data=="object"&&(i.data.id||i.data.messageId)),c||(c=j()),{data:{messageId:c,provider:n,response:o.data?.body,sent:!0,timestamp:new Date},success:!0}}catch(t){return{error:new d(n,`Failed to send email: ${t.message}`,{cause:t}),success:!1}}},async validateCredentials(){try{const e=await u(a.endpoint,{headers:m(),method:"GET",timeout:f});return!!(e.data&&typeof e.data=="object"&&"statusCode"in e.data&&typeof e.data.statusCode=="number"&&e.data.statusCode>=200&&e.data.statusCode<300)}catch{return!1}}}});export{z as default};