@elysiajs/eden
Version:
Fully type-safe Elysia client
2 lines (1 loc) • 4.83 kB
JavaScript
import{a as x,b as O,c as M}from"./chunk-XYW4OUFN.mjs";var F=class{constructor(t){this.url=t;this.ws=new WebSocket(t)}ws;send(t){return Array.isArray(t)?(t.forEach(n=>this.send(n)),this):(this.ws.send(typeof t=="object"?JSON.stringify(t):t.toString()),this)}on(t,n,s){return this.addEventListener(t,n,s)}off(t,n,s){return this.ws.removeEventListener(t,n,s),this}subscribe(t,n){return this.addEventListener("message",t,n)}addEventListener(t,n,s){return this.ws.addEventListener(t,c=>{if(t==="message"){let f=M(c);n({...c,data:f})}else n(c)},s),this}removeEventListener(t,n,s){return this.off(t,n,s),this}close(){return this.ws.close(),this}};var N=["get","post","put","delete","patch","options","head","connect","subscribe"],I=["localhost","127.0.0.1","0.0.0.0"],C=typeof FileList>"u",q=e=>C?e instanceof Blob:e instanceof FileList||e instanceof File,P=e=>{if(!e)return!1;for(let t in e)if(q(e[t])||Array.isArray(e[t])&&e[t].find(q))return!0;return!1},j=e=>C?e:new Promise(t=>{let n=new FileReader;n.onload=()=>{let s=new File([n.result],e.name,{lastModified:e.lastModified,type:e.type});t(s)},n.readAsArrayBuffer(e)}),b=(e,t,n={},s={})=>{if(Array.isArray(e)){for(let c of e)if(!Array.isArray(c))s=b(c,t,n,s);else{let f=c[0];if(typeof f=="string")s[f.toLowerCase()]=c[1];else for(let[a,w]of f)s[a.toLowerCase()]=w}return s}if(!e)return s;switch(typeof e){case"function":if(e instanceof Headers)return b(e,t,n,s);let c=e(t,n);return c?b(c,t,n,s):s;case"object":if(e instanceof Headers)return e.forEach((f,a)=>{s[a.toLowerCase()]=f}),s;for(let[f,a]of Object.entries(e))s[f.toLowerCase()]=a;return s;default:return s}};async function*U(e){let t=e.body;if(!t)return;let n=t.getReader(),s=new TextDecoder;try{for(;;){let{done:c,value:f}=await n.read();if(c)break;let a=s.decode(f);yield O(a)}}finally{n.releaseLock()}}var A=(e,t,n=[],s)=>new Proxy(()=>{},{get(c,f){return A(e,t,f==="index"?n:[...n,f],s)},apply(c,f,[a,w]){if(!a||w||typeof a=="object"&&Object.keys(a).length!==1||N.includes(n.at(-1))){let K=[...n],k=K.pop(),g="/"+K.join("/"),{fetcher:D=fetch,headers:L,onRequest:p,onResponse:E,fetch:H}=t,m=k==="get"||k==="head"||k==="subscribe";L=b(L,g,w);let T=m?a?.query:w?.query,R="";if(T){let r=(h,d)=>{R+=(R?"&":"?")+`${encodeURIComponent(h)}=${encodeURIComponent(d)}`};for(let[h,d]of Object.entries(T)){if(Array.isArray(d)){for(let o of d)r(h,o);continue}if(typeof d=="object"){r(h,JSON.stringify(d));continue}r(h,`${d}`)}}if(k==="subscribe"){let r=e.replace(/^([^]+):\/\//,e.startsWith("https://")?"wss://":e.startsWith("http://")||I.find(h=>e.includes(h))?"ws://":"wss://")+g+R;return new F(r)}return(async()=>{let r={method:k?.toUpperCase(),body:a,...H,headers:L};r.headers={...L,...b(m?a?.headers:w?.headers,g,r)};let h=m&&typeof a=="object"?a.fetch:w?.fetch;if(r={...r,...h},m&&delete r.body,p){Array.isArray(p)||(p=[p]);for(let y of p){let i=await y(g,r);typeof i=="object"&&(r={...r,...i,headers:{...r.headers,...b(i.headers,g,r)}})}}if(m&&delete r.body,P(a)){let y=new FormData;for(let[i,u]of Object.entries(r.body)){if(C){y.append(i,u);continue}if(u instanceof File){y.append(i,await j(u));continue}if(u instanceof FileList){for(let v=0;v<u.length;v++)y.append(i,await j(u[v]));continue}if(Array.isArray(u)){for(let v=0;v<u.length;v++){let W=u[v];y.append(i,W instanceof File?await j(W):W)}continue}y.append(i,u)}r.body=y}else typeof a=="object"?(r.headers["content-type"]="application/json",r.body=JSON.stringify(a)):a!=null&&(r.headers["content-type"]="text/plain");if(m&&delete r.body,p){Array.isArray(p)||(p=[p]);for(let y of p){let i=await y(g,r);typeof i=="object"&&(r={...r,...i,headers:{...r.headers,...b(i.headers,g,r)}})}}let d=e+g+R,o=await(s?.handle(new Request(d,r))??D(d,r)),l=null,S=null;if(E){Array.isArray(E)||(E=[E]);for(let y of E)try{let i=await y(o.clone());if(i!=null){l=i;break}}catch(i){i instanceof x?S=i:S=new x(422,i);break}}if(l!==null)return{data:l,error:S,response:o,status:o.status,headers:o.headers};switch(o.headers.get("Content-Type")?.split(";")[0]){case"text/event-stream":l=U(o);break;case"application/json":l=await o.json();break;case"application/octet-stream":l=await o.arrayBuffer();break;case"multipart/form-data":let y=await o.formData();l={},y.forEach((i,u)=>{l[u]=i});break;default:l=await o.text().then(O)}return(o.status>=300||o.status<200)&&(S=new x(o.status,l),l=null),{data:l,error:S,response:o,status:o.status,headers:o.headers}})()}return typeof a=="object"?A(e,t,[...n,Object.values(a)[0]],s):A(e,t,n)}}),V=(e,t={})=>typeof e=="string"?(t.keepDomain||(e.includes("://")||(e=(I.find(n=>e.includes(n))?"http://":"https://")+e),e.endsWith("/")&&(e=e.slice(0,-1))),A(e,t)):(typeof window<"u"&&console.warn("Elysia instance server found on client side, this is not recommended for security reason. Use generic type instead."),A("http://e.ly",t,[],e));export{U as a,V as b};