UNPKG

@inboundemail/sdk

Version:

Official SDK for Inbound Email API

2 lines 4.48 kB
"use strict";var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var g=(s,e)=>{for(var t in e)a(s,t,{get:e[t],enumerable:!0})},c=(s,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of l(e))!p.call(s,n)&&n!==t&&a(s,n,{get:()=>e[n],enumerable:!(i=m(e,n))||i.enumerable});return s};var y=s=>c(a({},"__esModule",{value:!0}),s);var x={};g(x,{Inbound:()=>o,InboundEmailClient:()=>o,VERSION:()=>q,buildQueryString:()=>r,default:()=>o,getAttachmentInfo:()=>E,getEmailHtml:()=>P,getEmailText:()=>b,getRecipientAddresses:()=>f,getSenderInfo:()=>I,isInboundWebhook:()=>h,isValidEmail:()=>R});module.exports=y(x);function R(s){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s)}function r(s){let e=new URLSearchParams;Object.entries(s).forEach(([i,n])=>{n!=null&&e.append(i,String(n))});let t=e.toString();return t?`?${t}`:""}var o=class{constructor(e,t){this.mail={list:async e=>{let t=e?r(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?r(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?r(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?r(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 i=typeof e=="string"?e:e.id;if(!t.from)throw new Error('Reply requires a "from" address.');return this.emails.reply(i,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 i=`${this.baseUrl}${e}`,n={Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json",...t.headers},d=await fetch(i,{...t,headers:n});if(!d.ok){let u=await d.json().catch(()=>({}));throw new Error(u.error||`HTTP ${d.status}: ${d.statusText}`)}return d.json()}};function h(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 E(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 b(s){return s.cleanedContent.text||s.parsedData.textBody||(s.cleanedContent.html?s.cleanedContent.html.replace(/<[^>]*>/g,""):"")||""}function P(s){return s.cleanedContent.html||s.parsedData.htmlBody||(s.cleanedContent.text?s.cleanedContent.text.replace(/\n/g,"<br>"):"")||""}function I(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 f(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 q="3.0.0";0&&(module.exports={Inbound,InboundEmailClient,VERSION,buildQueryString,getAttachmentInfo,getEmailHtml,getEmailText,getRecipientAddresses,getSenderInfo,isInboundWebhook,isValidEmail}); //# sourceMappingURL=index.js.map