@hey-api/client-next
Version:
🚀 Next.js client for `@hey-api/openapi-ts` codegen.
2 lines • 6.82 kB
JavaScript
var A=async(s,r)=>{let e=typeof r=="function"?await r(s):r;if(e)return s.scheme==="bearer"?`Bearer ${e}`:s.scheme==="basic"?`Basic ${btoa(e)}`:e},z=(s,r,e)=>{typeof e=="string"||e instanceof Blob?s.append(r,e):s.append(r,JSON.stringify(e));},j=(s,r,e)=>{typeof e=="string"?s.append(r,e):s.append(r,JSON.stringify(e));},v={bodySerializer:s=>{let r=new FormData;return Object.entries(s).forEach(([e,a])=>{a!=null&&(Array.isArray(a)?a.forEach(i=>z(r,e,i)):z(r,e,a));}),r}},b={bodySerializer:s=>JSON.stringify(s,(r,e)=>typeof e=="bigint"?e.toString():e)},T={bodySerializer:s=>{let r=new URLSearchParams;return Object.entries(s).forEach(([e,a])=>{a!=null&&(Array.isArray(a)?a.forEach(i=>j(r,e,i)):j(r,e,a));}),r.toString()}},k=s=>{switch(s){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},$=s=>{switch(s){case "form":return ",";case "pipeDelimited":return "|";case "spaceDelimited":return "%20";default:return ","}},U=s=>{switch(s){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},O=({allowReserved:s,explode:r,name:e,style:a,value:i})=>{if(!r){let t=(s?i:i.map(l=>encodeURIComponent(l))).join($(a));switch(a){case "label":return `.${t}`;case "matrix":return `;${e}=${t}`;case "simple":return t;default:return `${e}=${t}`}}let o=k(a),n=i.map(t=>a==="label"||a==="simple"?s?t:encodeURIComponent(t):d({allowReserved:s,name:e,value:t})).join(o);return a==="label"||a==="matrix"?o+n:n},d=({allowReserved:s,name:r,value:e})=>{if(e==null)return "";if(typeof e=="object")throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");return `${r}=${s?e:encodeURIComponent(e)}`},S=({allowReserved:s,explode:r,name:e,style:a,value:i})=>{if(i instanceof Date)return `${e}=${i.toISOString()}`;if(a!=="deepObject"&&!r){let t=[];Object.entries(i).forEach(([p,c])=>{t=[...t,p,s?c:encodeURIComponent(c)];});let l=t.join(",");switch(a){case "form":return `${e}=${l}`;case "label":return `.${l}`;case "matrix":return `;${e}=${l}`;default:return l}}let o=U(a),n=Object.entries(i).map(([t,l])=>d({allowReserved:s,name:a==="deepObject"?`${e}[${t}]`:t,value:l})).join(o);return a==="label"||a==="matrix"?o+n:n};var _=/\{[^{}]+\}/g,D=({path:s,url:r})=>{let e=r,a=r.match(_);if(a)for(let i of a){let o=false,n=i.substring(1,i.length-1),t="simple";n.endsWith("*")&&(o=true,n=n.substring(0,n.length-1)),n.startsWith(".")?(n=n.substring(1),t="label"):n.startsWith(";")&&(n=n.substring(1),t="matrix");let l=s[n];if(l==null)continue;if(Array.isArray(l)){e=e.replace(i,O({explode:o,name:n,style:t,value:l}));continue}if(typeof l=="object"){e=e.replace(i,S({explode:o,name:n,style:t,value:l}));continue}if(t==="matrix"){e=e.replace(i,`;${d({name:n,value:l})}`);continue}let p=encodeURIComponent(t==="label"?`.${l}`:l);e=e.replace(i,p);}return e},q=({allowReserved:s,array:r,object:e}={})=>i=>{let o=[];if(i&&typeof i=="object")for(let n in i){let t=i[n];if(t!=null){if(Array.isArray(t)){o=[...o,O({allowReserved:s,explode:true,name:n,style:"form",value:t,...r})];continue}if(typeof t=="object"){o=[...o,S({allowReserved:s,explode:true,name:n,style:"deepObject",value:t,...e})];continue}o=[...o,d({allowReserved:s,name:n,value:t})];}}return o.join("&")},E=s=>{if(!s)return "stream";let r=s.split(";")[0]?.trim();if(r){if(r.startsWith("application/json")||r.endsWith("+json"))return "json";if(r==="multipart/form-data")return "formData";if(["application/","audio/","image/","video/"].some(e=>r.startsWith(e)))return "blob";if(r.startsWith("text/"))return "text"}},P=async({security:s,...r})=>{for(let e of s){let a=await A(e,r.auth);if(!a)continue;let i=e.name??"Authorization";switch(e.in){case "query":r.query||(r.query={}),r.query[i]=a;break;case "cookie":r.headers.append("Cookie",`${i}=${a}`);break;case "header":default:r.headers.set(i,a);break}return}},C=s=>H({baseUrl:s.baseUrl,path:s.path,query:s.query,querySerializer:typeof s.querySerializer=="function"?s.querySerializer:q(s.querySerializer),url:s.url}),H=({baseUrl:s,path:r,query:e,querySerializer:a,url:i})=>{let o=i.startsWith("/")?i:`/${i}`,n=(s??"")+o;r&&(n=D({path:r,url:n}));let t=e?a(e):"";return t.startsWith("?")&&(t=t.substring(1)),t&&(n+=`?${t}`),n},R=(s,r)=>{let e={...s,...r};return e.baseUrl?.endsWith("/")&&(e.baseUrl=e.baseUrl.substring(0,e.baseUrl.length-1)),e.headers=x(s.headers,r.headers),e},x=(...s)=>{let r=new Headers;for(let e of s){if(!e||typeof e!="object")continue;let a=e instanceof Headers?e.entries():Object.entries(e);for(let[i,o]of a)if(o===null)r.delete(i);else if(Array.isArray(o))for(let n of o)r.append(i,n);else o!==undefined&&r.set(i,typeof o=="object"?JSON.stringify(o):o);}return r},y=class{_fns;constructor(){this._fns=[];}clear(){this._fns=[];}exists(r){return this._fns.indexOf(r)!==-1}eject(r){let e=this._fns.indexOf(r);e!==-1&&(this._fns=[...this._fns.slice(0,e),...this._fns.slice(e+1)]);}use(r){this._fns=[...this._fns,r];}},I=()=>({error:new y,request:new y,response:new y}),B=q({allowReserved:false,array:{explode:true,style:"form"},object:{explode:true,style:"deepObject"}}),W={"Content-Type":"application/json"},w=(s={})=>({...b,headers:W,parseAs:"auto",querySerializer:B,...s});var N=(s={})=>{let r=R(w(),s),e=()=>({...r}),a=n=>(r=R(r,n),e()),i=I(),o=async n=>{let t={...r,...n,fetch:n.fetch??r.fetch??globalThis.fetch,headers:x(r.headers,n.headers)};t.security&&await P({...t,security:t.security}),t.body&&t.bodySerializer&&(t.body=t.bodySerializer(t.body)),(t.body===undefined||t.body==="")&&t.headers.delete("Content-Type");for(let u of i.request._fns)await u(t);let l=C(t),p=t.fetch,c=await p(l,{...t,body:t.body});for(let u of i.response._fns)c=await u(c,t);let h={response:c};if(c.ok){if(c.status===204||c.headers.get("Content-Length")==="0")return {data:{},...h};let u=(t.parseAs==="auto"?E(c.headers.get("Content-Type")):t.parseAs)??"json";if(u==="stream")return {data:c.body,...h};let g=await c[u]();return u==="json"&&(t.responseValidator&&await t.responseValidator(g),t.responseTransformer&&(g=await t.responseTransformer(g))),{data:g,...h}}let m=await c.text();try{m=JSON.parse(m);}catch{}let f=m;for(let u of i.error._fns)f=await u(m,c,t);if(f=f||{},t.throwOnError)throw f;return {error:f,...h}};return {buildUrl:C,connect:n=>o({...n,method:"CONNECT"}),delete:n=>o({...n,method:"DELETE"}),get:n=>o({...n,method:"GET"}),getConfig:e,head:n=>o({...n,method:"HEAD"}),interceptors:i,options:n=>o({...n,method:"OPTIONS"}),patch:n=>o({...n,method:"PATCH"}),post:n=>o({...n,method:"POST"}),put:n=>o({...n,method:"PUT"}),request:o,setConfig:a,trace:n=>o({...n,method:"TRACE"})}};export{N as createClient,w as createConfig,v as formDataBodySerializer,b as jsonBodySerializer,T as urlSearchParamsBodySerializer};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map