UNPKG

@inboundemail/sdk

Version:

Official SDK for Inbound Email API

2 lines 3.84 kB
function u(s){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s)}function o(s){let e=new URLSearchParams;Object.entries(s).forEach(([n,i])=>{i!=null&&e.append(n,String(i))});let t=e.toString();return t?`?${t}`:""}var d=class{constructor(e,t){this.mail={list:async e=>{let t=e?o(e):"";return this.request(`/mail${t}`)},get:async e=>this.request(`/mail/${e}`),reply:async e=>this.request("/mail",{method:"POST",body:JSON.stringify(e)})};this.endpoints={list:async e=>{let t=e?o(e):"";return this.request(`/endpoints${t}`)},create:async e=>this.request("/endpoints",{method:"POST",body:JSON.stringify(e)}),get:async e=>this.request(`/endpoints/${e}`),update:async(e,t)=>this.request(`/endpoints/${e}`,{method:"PUT",body:JSON.stringify(t)}),delete:async e=>this.request(`/endpoints/${e}`,{method:"DELETE"})};this.domains={list:async e=>{let t=e?o(e):"";return this.request(`/domains${t}`)},create:async e=>this.request("/domains",{method:"POST",body:JSON.stringify(e)}),get:async e=>this.request(`/domains/${e}`),update:async(e,t)=>this.request(`/domains/${e}`,{method:"PUT",body:JSON.stringify(t)})};this.emailAddresses={list:async e=>{let t=e?o(e):"";return this.request(`/email-addresses${t}`)},create:async e=>this.request("/email-addresses",{method:"POST",body:JSON.stringify(e)}),get:async e=>this.request(`/email-addresses/${e}`),update:async(e,t)=>this.request(`/email-addresses/${e}`,{method:"PUT",body:JSON.stringify(t)}),delete:async e=>this.request(`/email-addresses/${e}`,{method:"DELETE"})};this.emails={send:async e=>this.request("/emails",{method:"POST",body:JSON.stringify(e)}),get:async e=>this.request(`/emails/${e}`),reply:async(e,t)=>this.request(`/emails/${e}/reply`,{method:"POST",body:JSON.stringify(t)})};this.send=async e=>this.emails.send(e);this.reply=async(e,t)=>{let n=typeof e=="string"?e:e.id;if(!t.from)throw new Error('Reply requires a "from" address.');return this.emails.reply(n,t)};this.endpoint=this.endpoints;this.domain=this.domains;this.emailAddress=this.emailAddresses;this.email=this.emails;if(this.apiKey=e,this.baseUrl=t||"https://inbound.new/api/v2",!this.apiKey)throw new Error("API key is required")}async request(e,t={}){let n=`${this.baseUrl}${e}`,i={Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json",...t.headers},r=await fetch(n,{...t,headers:i});if(!r.ok){let a=await r.json().catch(()=>({}));throw new Error(a.error||`HTTP ${r.status}: ${r.statusText}`)}return r.json()}};function g(s){return s&&typeof s=="object"&&s.event==="email.received"&&typeof s.timestamp=="string"&&s.email&&typeof s.email=="object"&&typeof s.email.id=="string"&&s.endpoint&&typeof s.endpoint=="object"&&typeof s.endpoint.id=="string"}function c(s){let e=s.contentType?.toLowerCase()||"";return{...s,hasContent:!!s.size&&s.size>0,isImage:e.startsWith("image/"),isDocument:e.includes("pdf")||e.includes("document")||e.includes("text/")||e.includes("application/msword")||e.includes("application/vnd.openxmlformats")}}function y(s){return s.cleanedContent.text||s.parsedData.textBody||(s.cleanedContent.html?s.cleanedContent.html.replace(/<[^>]*>/g,""):"")||""}function R(s){return s.cleanedContent.html||s.parsedData.htmlBody||(s.cleanedContent.text?s.cleanedContent.text.replace(/\n/g,"<br>"):"")||""}function h(s){let e=s.from||s.parsedData.from;if(!e||!e.addresses||e.addresses.length===0)return{name:null,address:null};let t=e.addresses[0];return{name:t.name,address:t.address}}function E(s){let e=s.to||s.parsedData.to;return!e||!e.addresses?[s.recipient]:e.addresses.map(t=>t.address).filter(t=>t!==null)}var P="3.0.0";export{d as Inbound,d as InboundEmailClient,P as VERSION,o as buildQueryString,d as default,c as getAttachmentInfo,R as getEmailHtml,y as getEmailText,E as getRecipientAddresses,h as getSenderInfo,g as isInboundWebhook,u as isValidEmail}; //# sourceMappingURL=index.mjs.map