UNPKG

@inboundemail/sdk

Version:

Official SDK for Inbound Email API

2 lines 10.5 kB
"use strict";var f=Object.create;var m=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var x=(t,e)=>{for(var s in e)m(t,s,{get:e[s],enumerable:!0})},u=(t,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of I(e))!S.call(t,i)&&i!==s&&m(t,i,{get:()=>e[i],enumerable:!(n=A(e,i))||n.enumerable});return t};var G=(t,e,s)=>(s=t!=null?f(q(t)):{},u(e||!t||!t.__esModule?m(s,"default",{value:t,enumerable:!0}):s,t)),k=t=>u(m({},"__esModule",{value:!0}),t);var B={};x(B,{Inbound:()=>d,InboundEmailClient:()=>d,VERSION:()=>T,buildQueryString:()=>o,default:()=>d,getAttachmentInfo:()=>D,getEmailHtml:()=>E,getEmailText:()=>g,getRecipientAddresses:()=>b,getSenderInfo:()=>P,isInboundWebhook:()=>h,isValidEmail:()=>$});module.exports=k(B);function $(t){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)}function o(t){let e=new URLSearchParams;Object.entries(t).forEach(([n,i])=>{i!=null&&e.append(n,String(i))});let s=e.toString();return s?`?${s}`:""}var l=G(require("react")),p=require("react-dom/server");function c(t){try{let e=l.isValidElement(t)?t:l.createElement(t);return(0,p.renderToStaticMarkup)(e)}catch(e){throw new Error(`Failed to render React component: ${e instanceof Error?e.message:"Unknown error"}`)}}function y(){try{return typeof l<"u"&&typeof p.renderToStaticMarkup<"u"}catch{return!1}}function R(){return"React rendering is not available. Please install react and react-dom dependencies: npm install react react-dom"}var d=class{constructor(e,s){this.mail={list:async e=>{console.warn("\u26A0\uFE0F mail.list() is deprecated. Use email.received.list() instead.");let s=e?o(e):"";return this.request(`/mail${s}`)},get:async e=>(console.warn("\u26A0\uFE0F mail.get() is deprecated. Use email.received.get() instead."),this.request(`/mail/${e}`)),thread:async e=>(console.warn("\u26A0\uFE0F mail.thread() is deprecated. Use email.received.thread() instead."),this.request(`/mail/${e}/thread`)),markRead:async e=>(console.warn("\u26A0\uFE0F mail.markRead() is deprecated. Use email.received.markRead() instead."),this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isRead:!0})})),markUnread:async e=>(console.warn("\u26A0\uFE0F mail.markUnread() is deprecated. Use email.received.markUnread() instead."),this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isRead:!1})})),archive:async e=>(console.warn("\u26A0\uFE0F mail.archive() is deprecated. Use email.received.archive() instead."),this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isArchived:!0})})),unarchive:async e=>(console.warn("\u26A0\uFE0F mail.unarchive() is deprecated. Use email.received.unarchive() instead."),this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isArchived:!1})})),reply:async e=>(console.warn("\u26A0\uFE0F mail.reply() is deprecated. Use email.received.reply() instead."),this.request("/mail",{method:"POST",body:JSON.stringify(e)})),bulk:async(e,s)=>(console.warn("\u26A0\uFE0F mail.bulk() is deprecated. Use email.received.bulk() instead."),this.request("/mail/bulk",{method:"POST",body:JSON.stringify({emailIds:e,updates:s})}))};this.email={send:async(e,s)=>{let n=await this.processReactComponent(e),i=n.scheduled_at?"/emails/schedule":"/emails",r={"Content-Type":"application/json"};return s?.idempotencyKey&&(r["Idempotency-Key"]=s.idempotencyKey),this.request(i,{method:"POST",headers:r,body:JSON.stringify(n)})},get:async e=>{try{let s=await this.request(`/mail/${e}`);if(s.data)return s}catch{return this.request(`/emails/${e}`)}return this.request(`/emails/${e}`)},received:{list:async e=>{let s=e?o(e):"";return this.request(`/mail${s}`)},get:async e=>this.request(`/mail/${e}`),thread:async e=>this.request(`/mail/${e}/thread`),markRead:async e=>this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isRead:!0})}),markUnread:async e=>this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isRead:!1})}),archive:async e=>this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isArchived:!0})}),unarchive:async e=>this.request(`/mail/${e}`,{method:"PATCH",body:JSON.stringify({isArchived:!1})}),reply:async e=>this.request("/mail",{method:"POST",body:JSON.stringify(e)}),bulk:async(e,s)=>this.request("/mail/bulk",{method:"POST",body:JSON.stringify({emailIds:e,updates:s})})},sent:{get:async e=>this.request(`/emails/${e}`),reply:async(e,s,n)=>{let i={"Content-Type":"application/json"};return n?.idempotencyKey&&(i["Idempotency-Key"]=n.idempotencyKey),this.request(`/emails/${e}/reply`,{method:"POST",headers:i,body:JSON.stringify(s)})},listScheduled:async e=>{let s=e?o(e):"";return this.request(`/emails/schedule${s}`)},getScheduled:async e=>this.request(`/emails/schedule/${e}`),cancel:async e=>this.request(`/emails/schedule/${e}`,{method:"DELETE"})},reply:async(e,s,n)=>{console.warn("\u26A0\uFE0F email.reply() is deprecated. Use email.sent.reply() instead.");let i={"Content-Type":"application/json"};return n?.idempotencyKey&&(i["Idempotency-Key"]=n.idempotencyKey),this.request(`/emails/${e}/reply`,{method:"POST",headers:i,body:JSON.stringify(s)})},schedule:async(e,s)=>{let n=await this.processReactComponent(e),i={"Content-Type":"application/json"};return s?.idempotencyKey&&(i["Idempotency-Key"]=s.idempotencyKey),this.request("/emails/schedule",{method:"POST",headers:i,body:JSON.stringify(n)})},listScheduled:async e=>{console.warn("\u26A0\uFE0F email.listScheduled() is deprecated. Use email.sent.listScheduled() instead.");let s=e?o(e):"";return this.request(`/emails/schedule${s}`)},getScheduled:async e=>(console.warn("\u26A0\uFE0F email.getScheduled() is deprecated. Use email.sent.getScheduled() instead."),this.request(`/emails/schedule/${e}`)),cancel:async e=>(console.warn("\u26A0\uFE0F email.cancel() is deprecated. Use email.sent.cancel() instead."),this.request(`/emails/schedule/${e}`,{method:"DELETE"})),cancelScheduled:async e=>(console.warn("\u26A0\uFE0F email.cancelScheduled() is deprecated. Use email.sent.cancel() instead."),this.request(`/emails/schedule/${e}`,{method:"DELETE"})),address:{create:async e=>this.request("/email-addresses",{method:"POST",body:JSON.stringify(e)}),list:async e=>{let s=e?o(e):"";return this.request(`/email-addresses${s}`)},get:async e=>this.request(`/email-addresses/${e}`),update:async(e,s)=>this.request(`/email-addresses/${e}`,{method:"PUT",body:JSON.stringify(s)}),delete:async e=>this.request(`/email-addresses/${e}`,{method:"DELETE"})}};this.domain={create:async e=>this.request("/domains",{method:"POST",body:JSON.stringify(e)}),list:async e=>{let s=e?o(e):"";return this.request(`/domains${s}`)},get:async e=>this.request(`/domains/${e}`),update:async(e,s)=>this.request(`/domains/${e}`,{method:"PUT",body:JSON.stringify(s)}),delete:async e=>this.request(`/domains/${e}`,{method:"DELETE"}),verify:async e=>this.request(`/domains/${e}/auth`,{method:"POST"}),getDnsRecords:async e=>this.request(`/domains/${e}/dns-records`),checkStatus:async e=>this.request(`/domains/${e}/auth`,{method:"PATCH"})};this.endpoint={create:async e=>this.request("/endpoints",{method:"POST",body:JSON.stringify(e)}),list:async e=>{let s=e?o(e):"";return this.request(`/endpoints${s}`)},get:async e=>this.request(`/endpoints/${e}`),update:async(e,s)=>this.request(`/endpoints/${e}`,{method:"PUT",body:JSON.stringify(s)}),delete:async e=>this.request(`/endpoints/${e}`,{method:"DELETE"}),test:async e=>this.request(`/endpoints/${e}/test`,{method:"POST"})};this.quickReply=async(e,s,n,i)=>this.email.reply(e,{from:n,text:s},i);this.setupDomain=async(e,s)=>{let n=await this.domain.create({domain:e});if(n.error)return n;if(s&&n.data){let i=await this.endpoint.create({name:`${e} Webhook`,type:"webhook",config:{url:s,timeout:3e4,retryAttempts:3}});return{data:{domain:n.data,endpoint:i.data}}}return n};this.createForwarder=async(e,s)=>this.endpoint.create({name:`Forward ${e} to ${s}`,type:"email",config:{email:s}});this.scheduleReminder=async(e,s,n,i,r)=>this.email.schedule({from:i,to:e,subject:s,text:`Reminder: ${s}`,scheduled_at:n},r);this.send=async(e,s)=>this.email.send(e,s);this.reply=async(e,s,n)=>{let i=typeof e=="string"?e:e.id;return s.from?this.email.reply(i,s,n):{error:'Reply requires a "from" address.'}};this.domains=this.domain;this.endpoints=this.endpoint;this.emailAddresses=this.email.address;this.emails=this.email;if(this.apiKey=e,this.baseUrl=s||"https://inbound.new/api/v2",!this.apiKey)throw new Error("API key is required")}async request(e,s={}){let n=`${this.baseUrl}${e}`,i={Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json",...s.headers};try{let r=await fetch(n,{...s,headers:i}),a={};try{a=await r.json()}catch{a={}}return r.ok?{data:a}:{error:a&&typeof a=="object"&&a.error||`HTTP ${r.status}: ${r.statusText}`}}catch(r){return{error:r instanceof Error?r.message:"Network error occurred"}}}async processReactComponent(e){if(!e.react)return e;if(!y())throw new Error(R());try{let s=c(e.react),n={...e,html:s,react:void 0};return delete n.react,n}catch(s){throw new Error(`Failed to process React component: ${s instanceof Error?s.message:"Unknown error"}`)}}};function h(t){return t&&typeof t=="object"&&t.event==="email.received"&&typeof t.timestamp=="string"&&t.email&&typeof t.email=="object"&&typeof t.email.id=="string"&&t.endpoint&&typeof t.endpoint=="object"&&typeof t.endpoint.id=="string"}function D(t){let e=t.contentType?.toLowerCase()||"";return{...t,hasContent:!!t.size&&t.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 g(t){return t.cleanedContent.text||t.parsedData.textBody||(t.cleanedContent.html?t.cleanedContent.html.replace(/<[^>]*>/g,""):"")||""}function E(t){return t.cleanedContent.html||t.parsedData.htmlBody||(t.cleanedContent.text?t.cleanedContent.text.replace(/\n/g,"<br>"):"")||""}function P(t){let e=t.from||t.parsedData.from;if(!e||!e.addresses||e.addresses.length===0)return{name:null,address:null};let s=e.addresses[0];return{name:s.name,address:s.address}}function b(t){let e=t.to||t.parsedData.to;return!e||!e.addresses?[t.recipient]:e.addresses.map(s=>s.address).filter(s=>s!==null)}var T="4.0.0";0&&(module.exports={Inbound,InboundEmailClient,VERSION,buildQueryString,getAttachmentInfo,getEmailHtml,getEmailText,getRecipientAddresses,getSenderInfo,isInboundWebhook,isValidEmail}); //# sourceMappingURL=index.js.map