openapi-fetch
Advanced tools
Comparing version 0.1.3 to 0.1.4
# openapi-fetch | ||
## 0.1.4 | ||
### Patch Changes | ||
- 63ebe48: Fix request body type when optional (#48) | ||
## 0.1.3 | ||
@@ -4,0 +10,0 @@ |
@@ -42,3 +42,3 @@ /** options for each client instance */ | ||
export type RequestBodyObj<O> = O extends { | ||
requestBody: any; | ||
requestBody?: any; | ||
} ? O['requestBody'] : never; | ||
@@ -57,3 +57,3 @@ export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, 'content'> | undefined : FilterKeys<RequestBodyObj<O>, 'content'>; | ||
} ? O['parameters']['query'] : Record<string, unknown>) => string; | ||
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'> & { | ||
export type RequestOptions<T> = Params<T> & RequestBody<T> & { | ||
querySerializer?: QuerySerializer<T>; | ||
@@ -63,2 +63,3 @@ }; | ||
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, 'content'>; | ||
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, 'body'>; | ||
export type FetchResponse<T> = { | ||
@@ -65,0 +66,0 @@ data: T extends { |
@@ -42,3 +42,3 @@ /** options for each client instance */ | ||
export type RequestBodyObj<O> = O extends { | ||
requestBody: any; | ||
requestBody?: any; | ||
} ? O['requestBody'] : never; | ||
@@ -57,3 +57,3 @@ export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, 'content'> | undefined : FilterKeys<RequestBodyObj<O>, 'content'>; | ||
} ? O['parameters']['query'] : Record<string, unknown>) => string; | ||
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'> & { | ||
export type RequestOptions<T> = Params<T> & RequestBody<T> & { | ||
querySerializer?: QuerySerializer<T>; | ||
@@ -63,2 +63,3 @@ }; | ||
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, 'content'>; | ||
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, 'body'>; | ||
export type FetchResponse<T> = { | ||
@@ -65,0 +66,0 @@ data: T extends { |
@@ -1,1 +0,1 @@ | ||
var x={"Content-Type":"application/json"};function F(i){let P=new Headers({...x,...i?.headers??{}});async function s(e,t){let{headers:u,body:d,params:r={},querySerializer:c=a=>new URLSearchParams(a).toString(),...O}=t||{},y=`${i?.baseUrl??""}${e}`;if(r.path)for(let[a,o]of Object.entries(r.path))y=y.replace(`{${a}}`,encodeURIComponent(String(o)));r.query&&Object.keys(r.query).length&&(y+=`?${c(r.query)}`);let h=new Headers(P),l=new Headers(u);for(let[a,o]of l.entries())o==null?h.delete(a):h.set(a,o);let n=await fetch(y,{redirect:"follow",...i,...O,headers:h,body:typeof d=="string"?d:JSON.stringify(d)}),p=n.status===204||n.headers.get("Content-Length")==="0"?{}:await n.json();return n.ok?{data:p,response:n}:{error:p,response:n}}return{async get(e,t){return s(e,{...t,method:"GET"})},async put(e,t){return s(e,{...t,method:"PUT"})},async post(e,t){return s(e,{...t,method:"POST"})},async del(e,t){return s(e,{...t,method:"DELETE"})},async options(e,t){return s(e,{...t,method:"OPTIONS"})},async head(e,t){return s(e,{...t,method:"HEAD"})},async patch(e,t){return s(e,{...t,method:"PATCH"})},async trace(e,t){return s(e,{...t,method:"TRACE"})}}}export{F as default}; | ||
var x={"Content-Type":"application/json"};function F(i){let P=new Headers({...x,...i?.headers??{}});async function s(e,t){let{headers:u,body:d,params:r={},querySerializer:c=a=>new URLSearchParams(a).toString(),...O}=t||{},y=`${i?.baseUrl??""}${e}`;if(r.path)for(let[a,o]of Object.entries(r.path))y=y.replace(`{${a}}`,encodeURIComponent(String(o)));r.query&&Object.keys(r.query).length&&(y+=`?${c(r.query)}`);let p=new Headers(P),l=new Headers(u);for(let[a,o]of l.entries())o==null?p.delete(a):p.set(a,o);let n=await fetch(y,{redirect:"follow",...i,...O,headers:p,body:typeof d=="string"?d:JSON.stringify(d)}),h=n.status===204||n.headers.get("Content-Length")==="0"?{}:await n.json();return n.ok?{data:h,response:n}:{error:h,response:n}}return{async get(e,t){return s(e,{...t,method:"GET"})},async put(e,t){return s(e,{...t,method:"PUT"})},async post(e,t){return s(e,{...t,method:"POST"})},async del(e,t){return s(e,{...t,method:"DELETE"})},async options(e,t){return s(e,{...t,method:"OPTIONS"})},async head(e,t){return s(e,{...t,method:"HEAD"})},async patch(e,t){return s(e,{...t,method:"PATCH"})},async trace(e,t){return s(e,{...t,method:"TRACE"})}}}export{F as default}; |
{ | ||
"name": "openapi-fetch", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Drew Powers", |
@@ -354,2 +354,38 @@ import { atom, computed } from 'nanostores'; | ||
}); | ||
it('request body type when optional', async () => { | ||
fetchMocker.mockResponse(() => ({ status: 201, body: '{}' })); | ||
const client = createClient<paths>(); | ||
// expect error on wrong body type | ||
// @ts-expect-error | ||
await client.post('/post/optional', { body: { error: true } }); | ||
// (no error) | ||
await client.post('/post/optional', { | ||
body: { | ||
title: '', | ||
publish_date: 3, | ||
body: '', | ||
}, | ||
}); | ||
}); | ||
it('request body type when optional inline', async () => { | ||
fetchMocker.mockResponse(() => ({ status: 201, body: '{}' })); | ||
const client = createClient<paths>(); | ||
// expect error on wrong body type | ||
// @ts-expect-error | ||
await client.post('/post/optional/inline', { body: { error: true } }); | ||
// (no error) | ||
await client.post('/post/optional/inline', { | ||
body: { | ||
title: '', | ||
publish_date: 3, | ||
body: '', | ||
}, | ||
}); | ||
}); | ||
}); | ||
@@ -356,0 +392,0 @@ |
@@ -37,5 +37,5 @@ // settings | ||
// fetch types | ||
// general purpose types | ||
export type Params<O> = O extends { parameters: any } ? { params: NonNullable<O['parameters']> } : BaseParams; | ||
export type RequestBodyObj<O> = O extends { requestBody: any } ? O['requestBody'] : never; | ||
export type RequestBodyObj<O> = O extends { requestBody?: any } ? O['requestBody'] : never; | ||
export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, 'content'> | undefined : FilterKeys<RequestBodyObj<O>, 'content'>; | ||
@@ -45,5 +45,8 @@ export type RequestBodyJSON<O> = FilterKeys<RequestBodyContent<O>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<O>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<O>, JSONLike>; | ||
export type QuerySerializer<O> = (query: O extends { parameters: { query: any } } ? O['parameters']['query'] : Record<string, unknown>) => string; | ||
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'> & { querySerializer?: QuerySerializer<T> }; | ||
export type RequestOptions<T> = Params<T> & RequestBody<T> & { querySerializer?: QuerySerializer<T> }; | ||
export type Success<O> = FilterKeys<FilterKeys<O, OkStatus>, 'content'>; | ||
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, 'content'>; | ||
// fetch types | ||
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, 'body'>; | ||
export type FetchResponse<T> = | ||
@@ -50,0 +53,0 @@ | { data: T extends { responses: any } ? NonNullable<FilterKeys<Success<T['responses']>, JSONLike>> : unknown; error?: never; response: Response } |
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
61927
833