UNPKG

@hey-api/client-next

Version:

🚀 Next.js client for `@hey-api/openapi-ts` codegen.

2 lines • 7.89 kB
var A=async(t,r)=>{let e=typeof r=="function"?await r(t):r;if(e)return t.scheme==="bearer"?`Bearer ${e}`:t.scheme==="basic"?`Basic ${btoa(e)}`:e},w=(t,r,e)=>{typeof e=="string"||e instanceof Blob?t.append(r,e):t.append(r,JSON.stringify(e));},z=(t,r,e)=>{typeof e=="string"?t.append(r,e):t.append(r,JSON.stringify(e));},v={bodySerializer:t=>{let r=new FormData;return Object.entries(t).forEach(([e,a])=>{a!=null&&(Array.isArray(a)?a.forEach(i=>w(r,e,i)):w(r,e,a));}),r}},b={bodySerializer:t=>JSON.stringify(t,(r,e)=>typeof e=="bigint"?e.toString():e)},$={bodySerializer:t=>{let r=new URLSearchParams;return Object.entries(t).forEach(([e,a])=>{a!=null&&(Array.isArray(a)?a.forEach(i=>z(r,e,i)):z(r,e,a));}),r.toString()}},T={$body_:"body",$headers_:"headers",$path_:"path",$query_:"query"},_=Object.entries(T),I=(t,r)=>{r||(r=new Map);for(let e of t)"in"in e?e.key&&r.set(e.key,{in:e.in,map:e.map}):e.args&&I(e.args,r);return r},U=t=>{for(let[r,e]of Object.entries(t))e&&typeof e=="object"&&!Object.keys(e).length&&delete t[r];},D=(t,r)=>{let e={body:{},headers:{},path:{},query:{}},a=I(r),i;for(let[o,s]of t.entries())if(r[o]&&(i=r[o]),!!i)if("in"in i)if(i.key){let n=a.get(i.key),l=n.map||i.key;e[n.in][l]=s;}else e.body=s;else for(let[n,l]of Object.entries(s??{})){let u=a.get(n);if(u){let c=u.map||n;e[u.in][c]=l;}else {let c=_.find(([p])=>n.startsWith(p));if(c){let[p,d]=c;e[d][n.slice(p.length)]=l;}else for(let[p,d]of Object.entries(i.allowExtra??{}))if(d){e[p][n]=l;break}}}return U(e),e},H=t=>{switch(t){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},W=t=>{switch(t){case "form":return ",";case "pipeDelimited":return "|";case "spaceDelimited":return "%20";default:return ","}},B=t=>{switch(t){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},O=({allowReserved:t,explode:r,name:e,style:a,value:i})=>{if(!r){let n=(t?i:i.map(l=>encodeURIComponent(l))).join(W(a));switch(a){case "label":return `.${n}`;case "matrix":return `;${e}=${n}`;case "simple":return n;default:return `${e}=${n}`}}let o=H(a),s=i.map(n=>a==="label"||a==="simple"?t?n:encodeURIComponent(n):h({allowReserved:t,name:e,value:n})).join(o);return a==="label"||a==="matrix"?o+s:s},h=({allowReserved:t,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}=${t?e:encodeURIComponent(e)}`},S=({allowReserved:t,explode:r,name:e,style:a,value:i,valueOnly:o})=>{if(i instanceof Date)return o?i.toISOString():`${e}=${i.toISOString()}`;if(a!=="deepObject"&&!r){let l=[];Object.entries(i).forEach(([c,p])=>{l=[...l,c,t?p:encodeURIComponent(p)];});let u=l.join(",");switch(a){case "form":return `${e}=${u}`;case "label":return `.${u}`;case "matrix":return `;${e}=${u}`;default:return u}}let s=B(a),n=Object.entries(i).map(([l,u])=>h({allowReserved:t,name:a==="deepObject"?`${e}[${l}]`:l,value:u})).join(s);return a==="label"||a==="matrix"?s+n:n};var N=/\{[^{}]+\}/g,Q=({path:t,url:r})=>{let e=r,a=r.match(N);if(a)for(let i of a){let o=false,s=i.substring(1,i.length-1),n="simple";s.endsWith("*")&&(o=true,s=s.substring(0,s.length-1)),s.startsWith(".")?(s=s.substring(1),n="label"):s.startsWith(";")&&(s=s.substring(1),n="matrix");let l=t[s];if(l==null)continue;if(Array.isArray(l)){e=e.replace(i,O({explode:o,name:s,style:n,value:l}));continue}if(typeof l=="object"){e=e.replace(i,S({explode:o,name:s,style:n,value:l,valueOnly:true}));continue}if(n==="matrix"){e=e.replace(i,`;${h({name:s,value:l})}`);continue}let u=encodeURIComponent(n==="label"?`.${l}`:l);e=e.replace(i,u);}return e},q=({allowReserved:t,array:r,object:e}={})=>i=>{let o=[];if(i&&typeof i=="object")for(let s in i){let n=i[s];if(n!=null)if(Array.isArray(n)){let l=O({allowReserved:t,explode:true,name:s,style:"form",value:n,...r});l&&o.push(l);}else if(typeof n=="object"){let l=S({allowReserved:t,explode:true,name:s,style:"deepObject",value:n,...e});l&&o.push(l);}else {let l=h({allowReserved:t,name:s,value:n});l&&o.push(l);}}return o.join("&")},P=t=>{if(!t)return "stream";let r=t.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"}},k=async({security:t,...r})=>{for(let e of t){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=t=>J({baseUrl:t.baseUrl,path:t.path,query:t.query,querySerializer:typeof t.querySerializer=="function"?t.querySerializer:q(t.querySerializer),url:t.url}),J=({baseUrl:t,path:r,query:e,querySerializer:a,url:i})=>{let o=i.startsWith("/")?i:`/${i}`,s=(t??"")+o;r&&(s=Q({path:r,url:s}));let n=e?a(e):"";return n.startsWith("?")&&(n=n.substring(1)),n&&(s+=`?${n}`),s},x=(t,r)=>{let e={...t,...r};return e.baseUrl?.endsWith("/")&&(e.baseUrl=e.baseUrl.substring(0,e.baseUrl.length-1)),e.headers=R(t.headers,r.headers),e},R=(...t)=>{let r=new Headers;for(let e of t){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 s of o)r.append(i,s);else o!==void 0&&r.set(i,typeof o=="object"?JSON.stringify(o):o);}return r},m=class{_fns;constructor(){this._fns=[];}clear(){this._fns=[];}getInterceptorIndex(r){return typeof r=="number"?this._fns[r]?r:-1:this._fns.indexOf(r)}exists(r){let e=this.getInterceptorIndex(r);return !!this._fns[e]}eject(r){let e=this.getInterceptorIndex(r);this._fns[e]&&(this._fns[e]=null);}update(r,e){let a=this.getInterceptorIndex(r);return this._fns[a]?(this._fns[a]=e,r):false}use(r){return this._fns=[...this._fns,r],this._fns.length-1}},E=()=>({error:new m,request:new m,response:new m}),M=q({allowReserved:false,array:{explode:true,style:"form"},object:{explode:true,style:"deepObject"}}),L={"Content-Type":"application/json"},j=(t={})=>({...b,headers:L,parseAs:"auto",querySerializer:M,...t});var V=(t={})=>{let r=x(j(),t),e=()=>({...r}),a=s=>(r=x(r,s),e()),i=E(),o=async s=>{let n={...r,...s,fetch:s.fetch??r.fetch??globalThis.fetch,headers:R(r.headers,s.headers)};n.security&&await k({...n,security:n.security}),n.body&&n.bodySerializer&&(n.body=n.bodySerializer(n.body)),(n.body===void 0||n.body==="")&&n.headers.delete("Content-Type");for(let f of i.request._fns)f&&await f(n);let l=C(n),u=n.fetch,c=await u(l,{...n,body:n.body});for(let f of i.response._fns)f&&(c=await f(c,n));let p={response:c};if(c.ok){if(c.status===204||c.headers.get("Content-Length")==="0")return {data:{},...p};let f=(n.parseAs==="auto"?P(c.headers.get("Content-Type")):n.parseAs)??"json";if(f==="stream")return {data:c.body,...p};let g=await c[f]();return f==="json"&&(n.responseValidator&&await n.responseValidator(g),n.responseTransformer&&(g=await n.responseTransformer(g))),{data:g,...p}}let d=await c.text();try{d=JSON.parse(d);}catch{}let y=d;for(let f of i.error._fns)f&&(y=await f(d,c,n));if(y=y||{},n.throwOnError)throw y;return {error:y,...p}};return {buildUrl:C,connect:s=>o({...s,method:"CONNECT"}),delete:s=>o({...s,method:"DELETE"}),get:s=>o({...s,method:"GET"}),getConfig:e,head:s=>o({...s,method:"HEAD"}),interceptors:i,options:s=>o({...s,method:"OPTIONS"}),patch:s=>o({...s,method:"PATCH"}),post:s=>o({...s,method:"POST"}),put:s=>o({...s,method:"PUT"}),request:o,setConfig:a,trace:s=>o({...s,method:"TRACE"})}};export{D as buildClientParams,V as createClient,j as createConfig,v as formDataBodySerializer,b as jsonBodySerializer,$ as urlSearchParamsBodySerializer};//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map