openapi-fetch
Advanced tools
Comparing version 0.11.3 to 0.12.0
# openapi-fetch | ||
## 0.12.0 | ||
### Minor Changes | ||
- ⚠️ **Breaking Change** [#1826](https://github.com/openapi-ts/openapi-typescript/pull/1826) [`b893c44`](https://github.com/openapi-ts/openapi-typescript/commit/b893c44f4290917f24c2ef7cda106c540df9cb3d) Thanks [@goce-cz](https://github.com/goce-cz)! - Do not set content-type on body-less requests | ||
## 0.11.3 | ||
@@ -32,3 +38,3 @@ | ||
- [#1791](https://github.com/openapi-ts/openapi-typescript/pull/1791) [`a956d5d`](https://github.com/openapi-ts/openapi-typescript/commit/a956d5d8480834402536283ee2f24ce8086698dc) Thanks [@gzm0](https://github.com/gzm0)! - Add support for `client["/endpoint"].GET()` style calls | ||
-- ⚠️ **Breaking Change** [#1791](https://github.com/openapi-ts/openapi-typescript/pull/1791) [`a956d5d`](https://github.com/openapi-ts/openapi-typescript/commit/a956d5d8480834402536283ee2f24ce8086698dc) Thanks [@gzm0](https://github.com/gzm0)! - Add support for `client["/endpoint"].GET()` style calls | ||
@@ -35,0 +41,0 @@ ## 0.10.6 |
// settings & const | ||
const DEFAULT_HEADERS = { | ||
"Content-Type": "application/json", | ||
}; | ||
const PATH_PARAM_RE = /\{[^{}]+\}/g; | ||
@@ -44,3 +40,2 @@ | ||
baseUrl = removeTrailingSlash(baseUrl); | ||
baseHeaders = mergeHeaders(DEFAULT_HEADERS, baseHeaders); | ||
const middlewares = []; | ||
@@ -62,2 +57,3 @@ | ||
bodySerializer = globalBodySerializer ?? defaultBodySerializer, | ||
body, | ||
...init | ||
@@ -83,2 +79,14 @@ } = fetchOptions || {}; | ||
const serializedBody = body === undefined ? undefined : bodySerializer(body); | ||
const defaultHeaders = | ||
// with no body, we should not to set Content-Type | ||
serializedBody === undefined || | ||
// if serialized body is FormData; browser will correctly set Content-Type & boundary expression | ||
serializedBody instanceof FormData | ||
? {} | ||
: { | ||
"Content-Type": "application/json", | ||
}; | ||
const requestInit = { | ||
@@ -88,11 +96,5 @@ redirect: "follow", | ||
...init, | ||
headers: mergeHeaders(baseHeaders, headers, params.header), | ||
body: serializedBody, | ||
headers: mergeHeaders(defaultHeaders, baseHeaders, headers, params.header), | ||
}; | ||
if (requestInit.body !== undefined) { | ||
requestInit.body = bodySerializer(requestInit.body); | ||
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression | ||
if (requestInit.body instanceof FormData) { | ||
requestInit.headers.delete("Content-Type"); | ||
} | ||
} | ||
@@ -99,0 +101,0 @@ let id; |
@@ -1,1 +0,1 @@ | ||
var k={"Content-Type":"application/json"},v=/\{[^{}]+\}/g,g=class extends Request{constructor(e,t){super(e,t);for(let r in t)r in this||(this[r]=t[r])}};function B(){return Math.random().toString(36).slice(2,11)}function q(n){let{baseUrl:e="",fetch:t=globalThis.fetch,querySerializer:r,bodySerializer:i,headers:o,...l}={...n};e=P(e),o=D(k,o);let c=[];async function u(a,s){let{baseUrl:p,fetch:z=t,headers:L,params:b={},parseAs:T="json",querySerializer:m,bodySerializer:C=i??G,...N}=s||{};p&&(e=P(p));let E=typeof r=="function"?r:U(r);m&&(E=typeof m=="function"?m:U({...typeof r=="object"?r:{},...m}));let y={redirect:"follow",...l,...N,headers:D(o,L,b.header)};y.body!==void 0&&(y.body=C(y.body),y.body instanceof FormData&&y.headers.delete("Content-Type"));let j,x,w=new g(M(a,{baseUrl:e,params:b,querySerializer:E}),y);if(c.length){j=B(),x=Object.freeze({baseUrl:e,fetch:z,parseAs:T,querySerializer:E,bodySerializer:C});for(let d of c)if(d&&typeof d=="object"&&typeof d.onRequest=="function"){let h=await d.onRequest({request:w,schemaPath:a,params:b,options:x,id:j});if(h){if(!(h instanceof Request))throw new Error("onRequest: must return new Request() when modifying the request");w=h}}}let f=await z(w);if(c.length)for(let d=c.length-1;d>=0;d--){let h=c[d];if(h&&typeof h=="object"&&typeof h.onResponse=="function"){let S=await h.onResponse({request:w,response:f,schemaPath:a,params:b,options:x,id:j});if(S){if(!(S instanceof Response))throw new Error("onResponse: must return new Response() when modifying the response");f=S}}}if(f.status===204||f.headers.get("Content-Length")==="0")return f.ok?{data:{},response:f}:{error:{},response:f};if(f.ok)return T==="stream"?{data:f.body,response:f}:{data:await f[T](),response:f};let A=await f.text();try{A=JSON.parse(A)}catch{}return{error:A,response:f}}return{GET(a,s){return u(a,{...s,method:"GET"})},PUT(a,s){return u(a,{...s,method:"PUT"})},POST(a,s){return u(a,{...s,method:"POST"})},DELETE(a,s){return u(a,{...s,method:"DELETE"})},OPTIONS(a,s){return u(a,{...s,method:"OPTIONS"})},HEAD(a,s){return u(a,{...s,method:"HEAD"})},PATCH(a,s){return u(a,{...s,method:"PATCH"})},TRACE(a,s){return u(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()`");c.push(s)}},eject(...a){for(let s of a){let p=c.indexOf(s);p!==-1&&c.splice(p,1)}}}}var $=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)}},O=class{constructor(){this.client=null}get(e,t){let r=new $(e,t);return this.client[t]=r,r}};function F(n){let e=new O,t=new Proxy(n,e);function r(){}r.prototype=t;let i=new r;return e.client=i,i}function Q(n){return F(q(n))}function R(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 H(n,e,t){if(!e||typeof e!="object")return"";let r=[],i={simple:",",label:".",matrix:";"}[t.style]||"&";if(t.style!=="deepObject"&&t.explode===!1){for(let c in e)r.push(c,t.allowReserved===!0?e[c]:encodeURIComponent(e[c]));let l=r.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;r.push(R(c,e[l],t))}let o=r.join(i);return t.style==="label"||t.style==="matrix"?`${i}${o}`:o}function I(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 r={simple:",",label:".",matrix:";"}[t.style]||"&",i=[];for(let o of e)t.style==="simple"||t.style==="label"?i.push(t.allowReserved===!0?o:encodeURIComponent(o)):i.push(R(n,o,t));return t.style==="label"||t.style==="matrix"?`${r}${i.join(r)}`:i.join(r)}function U(n){return function(t){let r=[];if(t&&typeof t=="object")for(let i in t){let o=t[i];if(o!=null){if(Array.isArray(o)){r.push(I(i,o,{style:"form",explode:!0,...n?.array,allowReserved:n?.allowReserved||!1}));continue}if(typeof o=="object"){r.push(H(i,o,{style:"deepObject",explode:!0,...n?.object,allowReserved:n?.allowReserved||!1}));continue}r.push(R(i,o,n))}}return r.join("&")}}function W(n,e){let t=n;for(let r of n.match(v)??[]){let i=r.substring(1,r.length-1),o=!1,l="simple";if(i.endsWith("*")&&(o=!0,i=i.substring(0,i.length-1)),i.startsWith(".")?(l="label",i=i.substring(1)):i.startsWith(";")&&(l="matrix",i=i.substring(1)),!e||e[i]===void 0||e[i]===null)continue;let c=e[i];if(Array.isArray(c)){t=t.replace(r,I(i,c,{style:l,explode:o}));continue}if(typeof c=="object"){t=t.replace(r,H(i,c,{style:l,explode:o}));continue}if(l==="matrix"){t=t.replace(r,`;${R(i,c)}`);continue}t=t.replace(r,l==="label"?`.${encodeURIComponent(c)}`:encodeURIComponent(c))}return t}function G(n){return n instanceof FormData?n:JSON.stringify(n)}function M(n,e){let t=`${e.baseUrl}${n}`;e.params?.path&&(t=W(t,e.params.path));let r=e.querySerializer(e.params.query??{});return r.startsWith("?")&&(r=r.substring(1)),r&&(t+=`?${r}`),t}function D(...n){let e=new Headers;for(let t of n){if(!t||typeof t!="object")continue;let r=t instanceof Headers?t.entries():Object.entries(t);for(let[i,o]of r)if(o===null)e.delete(i);else if(Array.isArray(o))for(let l of o)e.append(i,l);else o!==void 0&&e.set(i,o)}return e}function P(n){return n.endsWith("/")?n.substring(0,n.length-1):n}export{M as createFinalURL,Q as createPathBasedClient,U as createQuerySerializer,q as default,G as defaultBodySerializer,W as defaultPathSerializer,D as mergeHeaders,B as randomID,P as removeTrailingSlash,I as serializeArrayParam,H as serializeObjectParam,R as serializePrimitiveParam,F as wrapAsPathBasedClient}; | ||
var v=/\{[^{}]+\}/g,g=class extends Request{constructor(e,t){super(e,t);for(let r in t)r in this||(this[r]=t[r])}};function W(){return Math.random().toString(36).slice(2,11)}function D(n){let{baseUrl:e="",fetch:t=globalThis.fetch,querySerializer:r,bodySerializer:i,headers:s,...l}={...n};e=q(e);let a=[];async function f(c,o){let{baseUrl:p,fetch:z=t,headers:L,params:y={},parseAs:R="json",querySerializer:m,bodySerializer:C=i??M,body:U,...N}=o||{};p&&(e=q(p));let T=typeof r=="function"?r:P(r);m&&(T=typeof m=="function"?m:P({...typeof r=="object"?r:{},...m}));let j=U===void 0?void 0:C(U),k=j===void 0||j instanceof FormData?{}:{"Content-Type":"application/json"},B={redirect:"follow",...l,...N,body:j,headers:J(k,s,L,y.header)},x,E,b=new g(Q(c,{baseUrl:e,params:y,querySerializer:T}),B);if(a.length){x=W(),E=Object.freeze({baseUrl:e,fetch:z,parseAs:R,querySerializer:T,bodySerializer:C});for(let d of a)if(d&&typeof d=="object"&&typeof d.onRequest=="function"){let h=await d.onRequest({request:b,schemaPath:c,params:y,options:E,id:x});if(h){if(!(h instanceof Request))throw new Error("onRequest: must return new Request() when modifying the request");b=h}}}let u=await z(b);if(a.length)for(let d=a.length-1;d>=0;d--){let h=a[d];if(h&&typeof h=="object"&&typeof h.onResponse=="function"){let S=await h.onResponse({request:b,response:u,schemaPath:c,params:y,options:E,id:x});if(S){if(!(S instanceof Response))throw new Error("onResponse: must return new Response() when modifying the response");u=S}}}if(u.status===204||u.headers.get("Content-Length")==="0")return u.ok?{data:{},response:u}:{error:{},response:u};if(u.ok)return R==="stream"?{data:u.body,response:u}:{data:await u[R](),response:u};let A=await u.text();try{A=JSON.parse(A)}catch{}return{error:A,response:u}}return{GET(c,o){return f(c,{...o,method:"GET"})},PUT(c,o){return f(c,{...o,method:"PUT"})},POST(c,o){return f(c,{...o,method:"POST"})},DELETE(c,o){return f(c,{...o,method:"DELETE"})},OPTIONS(c,o){return f(c,{...o,method:"OPTIONS"})},HEAD(c,o){return f(c,{...o,method:"HEAD"})},PATCH(c,o){return f(c,{...o,method:"PATCH"})},TRACE(c,o){return f(c,{...o,method:"TRACE"})},use(...c){for(let o of c)if(o){if(typeof o!="object"||!("onRequest"in o||"onResponse"in o))throw new Error("Middleware must be an object with one of `onRequest()` or `onResponse()`");a.push(o)}},eject(...c){for(let o of c){let p=a.indexOf(o);p!==-1&&a.splice(p,1)}}}}var $=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)}},O=class{constructor(){this.client=null}get(e,t){let r=new $(e,t);return this.client[t]=r,r}};function F(n){let e=new O,t=new Proxy(n,e);function r(){}r.prototype=t;let i=new r;return e.client=i,i}function _(n){return F(D(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 H(n,e,t){if(!e||typeof e!="object")return"";let r=[],i={simple:",",label:".",matrix:";"}[t.style]||"&";if(t.style!=="deepObject"&&t.explode===!1){for(let a in e)r.push(a,t.allowReserved===!0?e[a]:encodeURIComponent(e[a]));let l=r.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 a=t.style==="deepObject"?`${n}[${l}]`:l;r.push(w(a,e[l],t))}let s=r.join(i);return t.style==="label"||t.style==="matrix"?`${i}${s}`:s}function I(n,e,t){if(!Array.isArray(e))return"";if(t.explode===!1){let s={form:",",spaceDelimited:"%20",pipeDelimited:"|"}[t.style]||",",l=(t.allowReserved===!0?e:e.map(a=>encodeURIComponent(a))).join(s);switch(t.style){case"simple":return l;case"label":return`.${l}`;case"matrix":return`;${n}=${l}`;default:return`${n}=${l}`}}let r={simple:",",label:".",matrix:";"}[t.style]||"&",i=[];for(let s of e)t.style==="simple"||t.style==="label"?i.push(t.allowReserved===!0?s:encodeURIComponent(s)):i.push(w(n,s,t));return t.style==="label"||t.style==="matrix"?`${r}${i.join(r)}`:i.join(r)}function P(n){return function(t){let r=[];if(t&&typeof t=="object")for(let i in t){let s=t[i];if(s!=null){if(Array.isArray(s)){r.push(I(i,s,{style:"form",explode:!0,...n?.array,allowReserved:n?.allowReserved||!1}));continue}if(typeof s=="object"){r.push(H(i,s,{style:"deepObject",explode:!0,...n?.object,allowReserved:n?.allowReserved||!1}));continue}r.push(w(i,s,n))}}return r.join("&")}}function G(n,e){let t=n;for(let r of n.match(v)??[]){let i=r.substring(1,r.length-1),s=!1,l="simple";if(i.endsWith("*")&&(s=!0,i=i.substring(0,i.length-1)),i.startsWith(".")?(l="label",i=i.substring(1)):i.startsWith(";")&&(l="matrix",i=i.substring(1)),!e||e[i]===void 0||e[i]===null)continue;let a=e[i];if(Array.isArray(a)){t=t.replace(r,I(i,a,{style:l,explode:s}));continue}if(typeof a=="object"){t=t.replace(r,H(i,a,{style:l,explode:s}));continue}if(l==="matrix"){t=t.replace(r,`;${w(i,a)}`);continue}t=t.replace(r,l==="label"?`.${encodeURIComponent(a)}`:encodeURIComponent(a))}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 r=e.querySerializer(e.params.query??{});return r.startsWith("?")&&(r=r.substring(1)),r&&(t+=`?${r}`),t}function J(...n){let e=new Headers;for(let t of n){if(!t||typeof t!="object")continue;let r=t instanceof Headers?t.entries():Object.entries(t);for(let[i,s]of r)if(s===null)e.delete(i);else if(Array.isArray(s))for(let l of s)e.append(i,l);else s!==void 0&&e.set(i,s)}return e}function q(n){return n.endsWith("/")?n.substring(0,n.length-1):n}export{Q as createFinalURL,_ as createPathBasedClient,P as createQuerySerializer,D as default,M as defaultBodySerializer,G as defaultPathSerializer,J as mergeHeaders,W as randomID,q as removeTrailingSlash,I as serializeArrayParam,H 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.11.3", | ||
"version": "0.12.0", | ||
"author": { | ||
@@ -64,3 +64,3 @@ "name": "Drew Powers", | ||
"vite": "^5.3.5", | ||
"openapi-typescript": "^7.3.2" | ||
"openapi-typescript": "^7.3.3" | ||
}, | ||
@@ -67,0 +67,0 @@ "scripts": { |
// settings & const | ||
const DEFAULT_HEADERS = { | ||
"Content-Type": "application/json", | ||
}; | ||
const PATH_PARAM_RE = /\{[^{}]+\}/g; | ||
@@ -44,3 +40,2 @@ | ||
baseUrl = removeTrailingSlash(baseUrl); | ||
baseHeaders = mergeHeaders(DEFAULT_HEADERS, baseHeaders); | ||
const middlewares = []; | ||
@@ -62,2 +57,3 @@ | ||
bodySerializer = globalBodySerializer ?? defaultBodySerializer, | ||
body, | ||
...init | ||
@@ -83,2 +79,14 @@ } = fetchOptions || {}; | ||
const serializedBody = body === undefined ? undefined : bodySerializer(body); | ||
const defaultHeaders = | ||
// with no body, we should not to set Content-Type | ||
serializedBody === undefined || | ||
// if serialized body is FormData; browser will correctly set Content-Type & boundary expression | ||
serializedBody instanceof FormData | ||
? {} | ||
: { | ||
"Content-Type": "application/json", | ||
}; | ||
const requestInit = { | ||
@@ -88,11 +96,5 @@ redirect: "follow", | ||
...init, | ||
headers: mergeHeaders(baseHeaders, headers, params.header), | ||
body: serializedBody, | ||
headers: mergeHeaders(defaultHeaders, baseHeaders, headers, params.header), | ||
}; | ||
if (requestInit.body !== undefined) { | ||
requestInit.body = bodySerializer(requestInit.body); | ||
// remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression | ||
if (requestInit.body instanceof FormData) { | ||
requestInit.headers.delete("Content-Type"); | ||
} | ||
} | ||
@@ -99,0 +101,0 @@ let id; |
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
132651
2367