UNPKG

@elysiajs/eden

Version:

Fully type-safe Elysia client

2 lines (1 loc) 6.53 kB
"use strict";var Eden=(()=>{var M=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var $=(e,t)=>{for(var n in t)M(e,n,{get:t[n],enumerable:!0})},B=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of U(t))!V.call(e,o)&&o!==n&&M(e,o,{get:()=>t[o],enumerable:!(s=H(t,o))||s.enumerable});return e};var Z=e=>B(M({},"__esModule",{value:!0}),e);var re={};$(re,{streamResponse:()=>J,treaty:()=>se});var k=class extends Error{constructor(n,s){super(s+"");this.status=n;this.value=s}};var G=/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/,z=/(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{2}\s\d{4}\s\d{2}:\d{2}:\d{2}\sGMT(?:\+|-)\d{4}\s\([^)]+\)/,Q=/^(?:(?:(?:(?:0?[1-9]|[12][0-9]|3[01])[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:19|20)\d{2})|(?:(?:19|20)\d{2}[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:0?[1-9]|[12][0-9]|3[01]))))(?:\s(?:1[012]|0?[1-9]):[0-5][0-9](?::[0-5][0-9])?(?:\s[AP]M)?)?$/,X=e=>e.trim().length!==0&&!Number.isNaN(Number(e)),K=e=>{if(typeof e!="string")return null;let t=e.replace(/"/g,"");if(G.test(t)||z.test(t)||Q.test(t)){let n=new Date(t);if(!Number.isNaN(n.getTime()))return n}return null},Y=e=>{let t=e.charCodeAt(0),n=e.charCodeAt(e.length-1);return t===123&&n===125||t===91&&n===93},ee=e=>JSON.parse(e,(t,n)=>{let s=K(n);return s||n}),T=e=>{if(!e)return e;if(X(e))return+e;if(e==="true")return!0;if(e==="false")return!1;let t=K(e);if(t)return t;if(Y(e))try{return ee(e)}catch{}return e},D=e=>{let t=e.data.toString();return t==="null"?null:T(t)};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,o=>{if(t==="message"){let f=D(o);n({...o,data:f})}else n(o)},s),this}removeEventListener(t,n,s){return this.off(t,n,s),this}close(){return this.ws.close(),this}};var te=["get","post","put","delete","patch","options","head","connect","subscribe"],I=["localhost","127.0.0.1","0.0.0.0"],j=typeof FileList>"u",q=e=>j?e instanceof Blob:e instanceof FileList||e instanceof File,ne=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},W=e=>j?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)}),w=(e,t,n={},s={})=>{if(Array.isArray(e)){for(let o of e)if(!Array.isArray(o))s=w(o,t,n,s);else{let f=o[0];if(typeof f=="string")s[f.toLowerCase()]=o[1];else for(let[i,b]of f)s[i.toLowerCase()]=b}return s}if(!e)return s;switch(typeof e){case"function":if(e instanceof Headers)return w(e,t,n,s);let o=e(t,n);return o?w(o,t,n,s):s;case"object":if(e instanceof Headers)return e.forEach((f,i)=>{s[i.toLowerCase()]=f}),s;for(let[f,i]of Object.entries(e))s[f.toLowerCase()]=i;return s;default:return s}};async function*J(e){let t=e.body;if(!t)return;let n=t.getReader(),s=new TextDecoder;try{for(;;){let{done:o,value:f}=await n.read();if(o)break;let i=s.decode(f);yield T(i)}}finally{n.releaseLock()}}var x=(e,t,n=[],s)=>new Proxy(()=>{},{get(o,f){return x(e,t,f==="index"?n:[...n,f],s)},apply(o,f,[i,b]){if(!i||b||typeof i=="object"&&Object.keys(i).length!==1||te.includes(n.at(-1))){let C=[...n],E=C.pop(),h="/"+C.join("/"),{fetcher:P=fetch,headers:O,onRequest:y,onResponse:v,fetch:_}=t,S=E==="get"||E==="head"||E==="subscribe";O=w(O,h,b);let N=S?i?.query:b?.query,L="";if(N){let r=(g,l)=>{L+=(L?"&":"?")+`${encodeURIComponent(g)}=${encodeURIComponent(l)}`};for(let[g,l]of Object.entries(N)){if(Array.isArray(l)){for(let c of l)r(g,c);continue}if(typeof l=="object"){r(g,JSON.stringify(l));continue}r(g,`${l}`)}}if(E==="subscribe"){let r=e.replace(/^([^]+):\/\//,e.startsWith("https://")?"wss://":e.startsWith("http://")||I.find(g=>e.includes(g))?"ws://":"wss://")+h+L;return new F(r)}return(async()=>{let r={method:E?.toUpperCase(),body:i,..._,headers:O};r.headers={...O,...w(S?i?.headers:b?.headers,h,r)};let g=S&&typeof i=="object"?i.fetch:b?.fetch;if(r={...r,...g},S&&delete r.body,y){Array.isArray(y)||(y=[y]);for(let d of y){let a=await d(h,r);typeof a=="object"&&(r={...r,...a,headers:{...r.headers,...w(a.headers,h,r)}})}}if(S&&delete r.body,ne(i)){let d=new FormData;for(let[a,p]of Object.entries(r.body)){if(j){d.append(a,p);continue}if(p instanceof File){d.append(a,await W(p));continue}if(p instanceof FileList){for(let m=0;m<p.length;m++)d.append(a,await W(p[m]));continue}if(Array.isArray(p)){for(let m=0;m<p.length;m++){let R=p[m];d.append(a,R instanceof File?await W(R):R)}continue}d.append(a,p)}r.body=d}else typeof i=="object"?(r.headers["content-type"]="application/json",r.body=JSON.stringify(i)):i!=null&&(r.headers["content-type"]="text/plain");if(S&&delete r.body,y){Array.isArray(y)||(y=[y]);for(let d of y){let a=await d(h,r);typeof a=="object"&&(r={...r,...a,headers:{...r.headers,...w(a.headers,h,r)}})}}let l=e+h+L,c=await(s?.handle(new Request(l,r))??P(l,r)),u=null,A=null;if(v){Array.isArray(v)||(v=[v]);for(let d of v)try{let a=await d(c.clone());if(a!=null){u=a;break}}catch(a){a instanceof k?A=a:A=new k(422,a);break}}if(u!==null)return{data:u,error:A,response:c,status:c.status,headers:c.headers};switch(c.headers.get("Content-Type")?.split(";")[0]){case"text/event-stream":u=J(c);break;case"application/json":u=await c.json();break;case"application/octet-stream":u=await c.arrayBuffer();break;case"multipart/form-data":let d=await c.formData();u={},d.forEach((a,p)=>{u[p]=a});break;default:u=await c.text().then(T)}return(c.status>=300||c.status<200)&&(A=new k(c.status,u),u=null),{data:u,error:A,response:c,status:c.status,headers:c.headers}})()}return typeof i=="object"?x(e,t,[...n,Object.values(i)[0]],s):x(e,t,n)}}),se=(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))),x(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."),x("http://e.ly",t,[],e));return Z(re);})();