openapi-fetch
Advanced tools
Comparing version 0.12.5 to 0.13.0
# openapi-fetch | ||
## 0.13.0 | ||
### Minor Changes | ||
- [#1968](https://github.com/openapi-ts/openapi-typescript/pull/1968) [`267977e`](https://github.com/openapi-ts/openapi-typescript/commit/267977e369f7a84c5d828023e6175729e4dd4c22) Thanks [@DjordyKoert](https://github.com/DjordyKoert)! - 204 responses or response with a Content-Length of 0 will now return undefined instead of an empty object | ||
## 0.12.5 | ||
@@ -4,0 +10,0 @@ |
@@ -154,5 +154,4 @@ // settings & const | ||
// handle empty content | ||
// note: we return `{}` because we want user truthy checks for `.data` or `.error` to succeed | ||
if (response.status === 204 || response.headers.get("Content-Length") === "0") { | ||
return response.ok ? { data: {}, response } : { error: {}, response }; | ||
return response.ok ? { data: undefined, response } : { error: undefined, response }; | ||
} | ||
@@ -159,0 +158,0 @@ |
@@ -1,1 +0,1 @@ | ||
var v=/\{[^{}]+\}/g;function W(){return Math.random().toString(36).slice(2,11)}function H(n){let{baseUrl:e="",Request:t=globalThis.Request,fetch:i=globalThis.fetch,querySerializer:r,bodySerializer:o,headers:l,...c}={...n};e=D(e);let d=[];async function h(a,s){let{baseUrl:m,fetch:C=i,Request:_=t,headers:N,params:b={},parseAs:T="json",querySerializer:R,bodySerializer:q=o??M,body:U,...j}=s||{};m&&(e=D(m));let x=typeof r=="function"?r:P(r);R&&(x=typeof R=="function"?R:P({...typeof r=="object"?r:{},...R}));let E=U===void 0?void 0:q(U),k=E===void 0||E instanceof FormData?{}:{"Content-Type":"application/json"},B={redirect:"follow",...c,...j,body:E,headers:J(k,l,N,b.header)},g,A,p=new t(Q(a,{baseUrl:e,params:b,querySerializer:x}),B);for(let f in j)f in p||(p[f]=j[f]);if(d.length){g=W(),A=Object.freeze({baseUrl:e,fetch:C,parseAs:T,querySerializer:x,bodySerializer:q});for(let f of d)if(f&&typeof f=="object"&&typeof f.onRequest=="function"){let y=await f.onRequest({request:p,schemaPath:a,params:b,options:A,id:g});if(y){if(!(y instanceof t))throw new Error("onRequest: must return new Request() when modifying the request");p=y}}}let u=await C(p);if(d.length)for(let f=d.length-1;f>=0;f--){let y=d[f];if(y&&typeof y=="object"&&typeof y.onResponse=="function"){let $=await y.onResponse({request:p,response:u,schemaPath:a,params:b,options:A,id:g});if($){if(!($ instanceof Response))throw new Error("onResponse: must return new Response() when modifying the response");u=$}}}if(u.status===204||u.headers.get("Content-Length")==="0")return u.ok?{data:{},response:u}:{error:{},response:u};if(u.ok)return T==="stream"?{data:u.body,response:u}:{data:await u[T](),response:u};let S=await u.text();try{S=JSON.parse(S)}catch{}return{error:S,response:u}}return{GET(a,s){return h(a,{...s,method:"GET"})},PUT(a,s){return h(a,{...s,method:"PUT"})},POST(a,s){return h(a,{...s,method:"POST"})},DELETE(a,s){return h(a,{...s,method:"DELETE"})},OPTIONS(a,s){return h(a,{...s,method:"OPTIONS"})},HEAD(a,s){return h(a,{...s,method:"HEAD"})},PATCH(a,s){return h(a,{...s,method:"PATCH"})},TRACE(a,s){return h(a,{...s,method:"TRACE"})},use(...a){for(let s of a)if(s){if(typeof s!="object"||!("onRequest"in s||"onResponse"in s))throw new Error("Middleware must be an object with one of `onRequest()` or `onResponse()`");d.push(s)}},eject(...a){for(let s of a){let m=d.indexOf(s);m!==-1&&d.splice(m,1)}}}}var O=class{constructor(e,t){this.client=e,this.url=t}GET(e){return this.client.GET(this.url,e)}PUT(e){return this.client.PUT(this.url,e)}POST(e){return this.client.POST(this.url,e)}DELETE(e){return this.client.DELETE(this.url,e)}OPTIONS(e){return this.client.OPTIONS(this.url,e)}HEAD(e){return this.client.HEAD(this.url,e)}PATCH(e){return this.client.PATCH(this.url,e)}TRACE(e){return this.client.TRACE(this.url,e)}},z=class{constructor(){this.client=null}get(e,t){let i=new O(e,t);return this.client[t]=i,i}};function F(n){let e=new z,t=new Proxy(n,e);function i(){}i.prototype=t;let r=new i;return e.client=r,r}function K(n){return F(H(n))}function w(n,e,t){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`${n}=${t?.allowReserved===!0?e:encodeURIComponent(e)}`}function I(n,e,t){if(!e||typeof e!="object")return"";let i=[],r={simple:",",label:".",matrix:";"}[t.style]||"&";if(t.style!=="deepObject"&&t.explode===!1){for(let c in e)i.push(c,t.allowReserved===!0?e[c]:encodeURIComponent(e[c]));let l=i.join(",");switch(t.style){case"form":return`${n}=${l}`;case"label":return`.${l}`;case"matrix":return`;${n}=${l}`;default:return l}}for(let l in e){let c=t.style==="deepObject"?`${n}[${l}]`:l;i.push(w(c,e[l],t))}let o=i.join(r);return t.style==="label"||t.style==="matrix"?`${r}${o}`:o}function L(n,e,t){if(!Array.isArray(e))return"";if(t.explode===!1){let o={form:",",spaceDelimited:"%20",pipeDelimited:"|"}[t.style]||",",l=(t.allowReserved===!0?e:e.map(c=>encodeURIComponent(c))).join(o);switch(t.style){case"simple":return l;case"label":return`.${l}`;case"matrix":return`;${n}=${l}`;default:return`${n}=${l}`}}let i={simple:",",label:".",matrix:";"}[t.style]||"&",r=[];for(let o of e)t.style==="simple"||t.style==="label"?r.push(t.allowReserved===!0?o:encodeURIComponent(o)):r.push(w(n,o,t));return t.style==="label"||t.style==="matrix"?`${i}${r.join(i)}`:r.join(i)}function P(n){return function(t){let i=[];if(t&&typeof t=="object")for(let r in t){let o=t[r];if(o!=null){if(Array.isArray(o)){if(o.length===0)continue;i.push(L(r,o,{style:"form",explode:!0,...n?.array,allowReserved:n?.allowReserved||!1}));continue}if(typeof o=="object"){i.push(I(r,o,{style:"deepObject",explode:!0,...n?.object,allowReserved:n?.allowReserved||!1}));continue}i.push(w(r,o,n))}}return i.join("&")}}function G(n,e){let t=n;for(let i of n.match(v)??[]){let r=i.substring(1,i.length-1),o=!1,l="simple";if(r.endsWith("*")&&(o=!0,r=r.substring(0,r.length-1)),r.startsWith(".")?(l="label",r=r.substring(1)):r.startsWith(";")&&(l="matrix",r=r.substring(1)),!e||e[r]===void 0||e[r]===null)continue;let c=e[r];if(Array.isArray(c)){t=t.replace(i,L(r,c,{style:l,explode:o}));continue}if(typeof c=="object"){t=t.replace(i,I(r,c,{style:l,explode:o}));continue}if(l==="matrix"){t=t.replace(i,`;${w(r,c)}`);continue}t=t.replace(i,l==="label"?`.${encodeURIComponent(c)}`:encodeURIComponent(c))}return t}function M(n){return n instanceof FormData?n:JSON.stringify(n)}function Q(n,e){let t=`${e.baseUrl}${n}`;e.params?.path&&(t=G(t,e.params.path));let i=e.querySerializer(e.params.query??{});return i.startsWith("?")&&(i=i.substring(1)),i&&(t+=`?${i}`),t}function J(...n){let e=new Headers;for(let t of n){if(!t||typeof t!="object")continue;let i=t instanceof Headers?t.entries():Object.entries(t);for(let[r,o]of i)if(o===null)e.delete(r);else if(Array.isArray(o))for(let l of o)e.append(r,l);else o!==void 0&&e.set(r,o)}return e}function D(n){return n.endsWith("/")?n.substring(0,n.length-1):n}export{Q as createFinalURL,K as createPathBasedClient,P as createQuerySerializer,H as default,M as defaultBodySerializer,G as defaultPathSerializer,J as mergeHeaders,W as randomID,D as removeTrailingSlash,L as serializeArrayParam,I as serializeObjectParam,w as serializePrimitiveParam,F as wrapAsPathBasedClient}; | ||
var v=/\{[^{}]+\}/g;function W(){return Math.random().toString(36).slice(2,11)}function H(n){let{baseUrl:e="",Request:t=globalThis.Request,fetch:i=globalThis.fetch,querySerializer:r,bodySerializer:o,headers:l,...c}={...n};e=D(e);let d=[];async function h(a,s){let{baseUrl:m,fetch:C=i,Request:_=t,headers:N,params:b={},parseAs:T="json",querySerializer:R,bodySerializer:q=o??M,body:U,...j}=s||{};m&&(e=D(m));let x=typeof r=="function"?r:P(r);R&&(x=typeof R=="function"?R:P({...typeof r=="object"?r:{},...R}));let E=U===void 0?void 0:q(U),k=E===void 0||E instanceof FormData?{}:{"Content-Type":"application/json"},B={redirect:"follow",...c,...j,body:E,headers:J(k,l,N,b.header)},g,A,p=new t(Q(a,{baseUrl:e,params:b,querySerializer:x}),B);for(let f in j)f in p||(p[f]=j[f]);if(d.length){g=W(),A=Object.freeze({baseUrl:e,fetch:C,parseAs:T,querySerializer:x,bodySerializer:q});for(let f of d)if(f&&typeof f=="object"&&typeof f.onRequest=="function"){let y=await f.onRequest({request:p,schemaPath:a,params:b,options:A,id:g});if(y){if(!(y instanceof t))throw new Error("onRequest: must return new Request() when modifying the request");p=y}}}let u=await C(p);if(d.length)for(let f=d.length-1;f>=0;f--){let y=d[f];if(y&&typeof y=="object"&&typeof y.onResponse=="function"){let $=await y.onResponse({request:p,response:u,schemaPath:a,params:b,options:A,id:g});if($){if(!($ instanceof Response))throw new Error("onResponse: must return new Response() when modifying the response");u=$}}}if(u.status===204||u.headers.get("Content-Length")==="0")return u.ok?{data:void 0,response:u}:{error:void 0,response:u};if(u.ok)return T==="stream"?{data:u.body,response:u}:{data:await u[T](),response:u};let S=await u.text();try{S=JSON.parse(S)}catch{}return{error:S,response:u}}return{GET(a,s){return h(a,{...s,method:"GET"})},PUT(a,s){return h(a,{...s,method:"PUT"})},POST(a,s){return h(a,{...s,method:"POST"})},DELETE(a,s){return h(a,{...s,method:"DELETE"})},OPTIONS(a,s){return h(a,{...s,method:"OPTIONS"})},HEAD(a,s){return h(a,{...s,method:"HEAD"})},PATCH(a,s){return h(a,{...s,method:"PATCH"})},TRACE(a,s){return h(a,{...s,method:"TRACE"})},use(...a){for(let s of a)if(s){if(typeof s!="object"||!("onRequest"in s||"onResponse"in s))throw new Error("Middleware must be an object with one of `onRequest()` or `onResponse()`");d.push(s)}},eject(...a){for(let s of a){let m=d.indexOf(s);m!==-1&&d.splice(m,1)}}}}var O=class{constructor(e,t){this.client=e,this.url=t}GET(e){return this.client.GET(this.url,e)}PUT(e){return this.client.PUT(this.url,e)}POST(e){return this.client.POST(this.url,e)}DELETE(e){return this.client.DELETE(this.url,e)}OPTIONS(e){return this.client.OPTIONS(this.url,e)}HEAD(e){return this.client.HEAD(this.url,e)}PATCH(e){return this.client.PATCH(this.url,e)}TRACE(e){return this.client.TRACE(this.url,e)}},z=class{constructor(){this.client=null}get(e,t){let i=new O(e,t);return this.client[t]=i,i}};function F(n){let e=new z,t=new Proxy(n,e);function i(){}i.prototype=t;let r=new i;return e.client=r,r}function K(n){return F(H(n))}function w(n,e,t){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`${n}=${t?.allowReserved===!0?e:encodeURIComponent(e)}`}function I(n,e,t){if(!e||typeof e!="object")return"";let i=[],r={simple:",",label:".",matrix:";"}[t.style]||"&";if(t.style!=="deepObject"&&t.explode===!1){for(let c in e)i.push(c,t.allowReserved===!0?e[c]:encodeURIComponent(e[c]));let l=i.join(",");switch(t.style){case"form":return`${n}=${l}`;case"label":return`.${l}`;case"matrix":return`;${n}=${l}`;default:return l}}for(let l in e){let c=t.style==="deepObject"?`${n}[${l}]`:l;i.push(w(c,e[l],t))}let o=i.join(r);return t.style==="label"||t.style==="matrix"?`${r}${o}`:o}function L(n,e,t){if(!Array.isArray(e))return"";if(t.explode===!1){let o={form:",",spaceDelimited:"%20",pipeDelimited:"|"}[t.style]||",",l=(t.allowReserved===!0?e:e.map(c=>encodeURIComponent(c))).join(o);switch(t.style){case"simple":return l;case"label":return`.${l}`;case"matrix":return`;${n}=${l}`;default:return`${n}=${l}`}}let i={simple:",",label:".",matrix:";"}[t.style]||"&",r=[];for(let o of e)t.style==="simple"||t.style==="label"?r.push(t.allowReserved===!0?o:encodeURIComponent(o)):r.push(w(n,o,t));return t.style==="label"||t.style==="matrix"?`${i}${r.join(i)}`:r.join(i)}function P(n){return function(t){let i=[];if(t&&typeof t=="object")for(let r in t){let o=t[r];if(o!=null){if(Array.isArray(o)){if(o.length===0)continue;i.push(L(r,o,{style:"form",explode:!0,...n?.array,allowReserved:n?.allowReserved||!1}));continue}if(typeof o=="object"){i.push(I(r,o,{style:"deepObject",explode:!0,...n?.object,allowReserved:n?.allowReserved||!1}));continue}i.push(w(r,o,n))}}return i.join("&")}}function G(n,e){let t=n;for(let i of n.match(v)??[]){let r=i.substring(1,i.length-1),o=!1,l="simple";if(r.endsWith("*")&&(o=!0,r=r.substring(0,r.length-1)),r.startsWith(".")?(l="label",r=r.substring(1)):r.startsWith(";")&&(l="matrix",r=r.substring(1)),!e||e[r]===void 0||e[r]===null)continue;let c=e[r];if(Array.isArray(c)){t=t.replace(i,L(r,c,{style:l,explode:o}));continue}if(typeof c=="object"){t=t.replace(i,I(r,c,{style:l,explode:o}));continue}if(l==="matrix"){t=t.replace(i,`;${w(r,c)}`);continue}t=t.replace(i,l==="label"?`.${encodeURIComponent(c)}`:encodeURIComponent(c))}return t}function M(n){return n instanceof FormData?n:JSON.stringify(n)}function Q(n,e){let t=`${e.baseUrl}${n}`;e.params?.path&&(t=G(t,e.params.path));let i=e.querySerializer(e.params.query??{});return i.startsWith("?")&&(i=i.substring(1)),i&&(t+=`?${i}`),t}function J(...n){let e=new Headers;for(let t of n){if(!t||typeof t!="object")continue;let i=t instanceof Headers?t.entries():Object.entries(t);for(let[r,o]of i)if(o===null)e.delete(r);else if(Array.isArray(o))for(let l of o)e.append(r,l);else o!==void 0&&e.set(r,o)}return e}function D(n){return n.endsWith("/")?n.substring(0,n.length-1):n}export{Q as createFinalURL,K as createPathBasedClient,P as createQuerySerializer,H as default,M as defaultBodySerializer,G as defaultPathSerializer,J as mergeHeaders,W as randomID,D as removeTrailingSlash,L as serializeArrayParam,I as serializeObjectParam,w as serializePrimitiveParam,F as wrapAsPathBasedClient}; |
{ | ||
"name": "openapi-fetch", | ||
"description": "Fast, type-safe fetch client for your OpenAPI schema. Only 6 kb (min). Works with React, Vue, Svelte, or vanilla JS.", | ||
"version": "0.12.5", | ||
"version": "0.13.0", | ||
"author": { | ||
@@ -60,5 +60,5 @@ "name": "Drew Powers", | ||
"openapi-typescript-fetch": "^2.0.0", | ||
"superagent": "^10.1.0", | ||
"typescript": "^5.6.2", | ||
"vite": "^5.4.6", | ||
"superagent": "^10.1.1", | ||
"typescript": "^5.6.3", | ||
"vite": "^5.4.10", | ||
"openapi-typescript": "^7.4.2" | ||
@@ -65,0 +65,0 @@ }, |
@@ -154,5 +154,4 @@ // settings & const | ||
// handle empty content | ||
// note: we return `{}` because we want user truthy checks for `.data` or `.error` to succeed | ||
if (response.status === 204 || response.headers.get("Content-Length") === "0") { | ||
return response.ok ? { data: {}, response } : { error: {}, response }; | ||
return response.ok ? { data: undefined, response } : { error: undefined, response }; | ||
} | ||
@@ -159,0 +158,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136659
2398