UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 3.42 kB
var e=class extends Error{body;status;constructor(e,t){super(e),this.name=`TwilioApiError`,this.body=t.body,this.status=t.status}},t=`https://api.twilio.com`;async function n(t,n){let r=t??process.env[n];if(!r)throw new e(`${n} is required`,{body:null,status:0});return typeof r==`function`?await r():r}async function r(r,i={}){let a=typeof r==`string`?{...i,path:r}:r,o=await n(a.credentials?.accountSid,`TWILIO_ACCOUNT_SID`),s=await n(a.credentials?.authToken,`TWILIO_AUTH_TOKEN`),c=new URL(a.path,a.apiUrl??a.apiBaseUrl??t);for(let[e,t]of d(a.search)??[])c.searchParams.append(e,t);let l=d(a.body),u=await(a.fetch??fetch)(c,{body:l,headers:{authorization:f(o,s),...l?{"content-type":`application/x-www-form-urlencoded;charset=UTF-8`}:{}},method:a.method??`POST`}),p=await m(u);if(!u.ok)throw new e(`Twilio API returned HTTP ${u.status}`,{body:p,status:u.status});return{body:p,ok:u.ok,status:u.status}}async function i(e){let t=await n(e.credentials?.accountSid,`TWILIO_ACCOUNT_SID`),i=p(e.mediaUrl);if(!e.body&&i.length===0)throw TypeError(`body or mediaUrl is required`);if(!(e.from||e.messagingServiceSid))throw TypeError(`from or messagingServiceSid is required`);let a=u({Body:e.body,From:e.from,MediaUrl:i,MessagingServiceSid:e.messagingServiceSid,StatusCallback:e.statusCallbackUrl,To:e.to});return(await r(`/2010-04-01/Accounts/${encodeURIComponent(t)}/Messages.json`,{...e,body:a})).body}async function a(e){let t=await n(e.credentials?.accountSid,`TWILIO_ACCOUNT_SID`);return(await r(`/2010-04-01/Accounts/${encodeURIComponent(t)}/Messages/${encodeURIComponent(e.messageSid)}.json`,{...e,method:`GET`})).body}async function o(e){let t=await n(e.credentials?.accountSid,`TWILIO_ACCOUNT_SID`);await r(`/2010-04-01/Accounts/${encodeURIComponent(t)}/Messages/${encodeURIComponent(e.messageSid)}.json`,{...e,method:`DELETE`})}async function s(e){let t=await n(e.credentials?.accountSid,`TWILIO_ACCOUNT_SID`);if(!(e.twiml||e.url||e.status))throw TypeError(`twiml, url, or status is required`);if(e.twiml&&e.url)throw TypeError(`twiml and url are mutually exclusive`);let i=u({Method:e.method,Status:e.status,Twiml:e.twiml,Url:e.url});return(await r(`/2010-04-01/Accounts/${encodeURIComponent(t)}/Calls/${encodeURIComponent(e.callSid)}.json`,{...e,body:i})).body}async function c(t){let r=await n(t.credentials?.accountSid,`TWILIO_ACCOUNT_SID`),i=await n(t.credentials?.authToken,`TWILIO_AUTH_TOKEN`),a=await(t.fetch??fetch)(t.url,{headers:{authorization:f(r,i)},method:`GET`});if(!a.ok)throw new e(`Twilio API returned HTTP ${a.status}`,{body:await m(a),status:a.status});return a.arrayBuffer()}async function l(e={}){let t=await n(e.credentials?.accountSid,`TWILIO_ACCOUNT_SID`),i=u({From:e.from,PageSize:e.pageSize,To:e.to});return((await r(`/2010-04-01/Accounts/${encodeURIComponent(t)}/Messages.json`,{...e,method:`GET`,search:i})).body.messages??[]).slice(0,e.limit)}function u(e){let t=new URLSearchParams;for(let[n,r]of Object.entries(e))if(r!=null){if(Array.isArray(r)){for(let e of r)t.append(n,e);continue}t.set(n,String(r))}return t}function d(e){if(e!==void 0)return e instanceof URLSearchParams?e:u(e)}function f(e,t){return`Basic ${btoa(`${e}:${t}`)}`}function p(e){return e===void 0?[]:typeof e==`string`?[e]:[...e]}async function m(e){let t=await e.text();if(!t)return null;try{return JSON.parse(t)}catch{return t}}export{c as a,n as c,u as i,i as l,r as n,a as o,o as r,l as s,e as t,s as u};