@hey-api/client-fetch
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -52,8 +52,2 @@ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited'; | ||
/** | ||
* Any body that you want to add to your request. | ||
* | ||
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body} | ||
*/ | ||
body?: RequestInit['body'] | Record<string, unknown> | Array<Record<string, unknown>> | Array<unknown> | number; | ||
/** | ||
* A function for serializing request body parameter. By default, | ||
@@ -110,3 +104,15 @@ * {@link JSON.stringify()} will be used. | ||
} | ||
interface RequestOptionsBase<ThrowOnError extends boolean, Url extends string = string> extends Config<ThrowOnError> { | ||
interface RequestOptions<ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<ThrowOnError> { | ||
/** | ||
* Any body that you want to add to your request. | ||
* | ||
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body} | ||
*/ | ||
body?: RequestInit['body'] | Record<string, unknown> | Array<Record<string, unknown>> | Array<unknown> | number; | ||
/** | ||
* You can provide a client instance returned by `createClient()` instead of | ||
* individual options. This might be also useful if you want to implement a | ||
* custom client. | ||
*/ | ||
client?: Client; | ||
path?: Record<string, unknown>; | ||
@@ -130,4 +136,4 @@ query?: Record<string, unknown>; | ||
}>; | ||
type MethodFn = <Data = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptionsBase<ThrowOnError>, 'method'>) => RequestResult<Data, TError, ThrowOnError>; | ||
type RequestFn = <Data = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptionsBase<ThrowOnError>, 'method'> & Pick<Required<RequestOptionsBase<ThrowOnError>>, 'method'>) => RequestResult<Data, TError, ThrowOnError>; | ||
type MethodFn = <Data = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<ThrowOnError>, 'method'>) => RequestResult<Data, TError, ThrowOnError>; | ||
type RequestFn = <Data = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<ThrowOnError>, 'method'> & Pick<Required<RequestOptions<ThrowOnError>>, 'method'>) => RequestResult<Data, TError, ThrowOnError>; | ||
interface Client<Req = Request, Res = Response, Err = unknown, Opts = RequestOptions> { | ||
@@ -137,5 +143,8 @@ /** | ||
*/ | ||
buildUrl: <T extends { | ||
buildUrl: <Data extends { | ||
body?: unknown; | ||
path?: Record<string, unknown>; | ||
query?: Record<string, unknown>; | ||
url: string; | ||
}>(options: T & Options<T>) => string; | ||
}>(options: Pick<Data, 'url'> & Options<Data>) => string; | ||
connect: MethodFn; | ||
@@ -155,34 +164,20 @@ delete: MethodFn; | ||
} | ||
type RequestOptions = RequestOptionsBase<false> & Config<false> & { | ||
headers: Headers; | ||
}; | ||
type OptionsBase<ThrowOnError extends boolean> = Omit<RequestOptionsBase<ThrowOnError>, 'url'> & { | ||
/** | ||
* You can provide a client instance returned by `createClient()` instead of | ||
* individual options. This might be also useful if you want to implement a | ||
* custom client. | ||
*/ | ||
client?: Client; | ||
}; | ||
type Options<T extends { | ||
interface DataShape { | ||
body?: unknown; | ||
headers?: unknown; | ||
path?: unknown; | ||
query?: unknown; | ||
url: string; | ||
} = { | ||
url: string; | ||
}, ThrowOnError extends boolean = boolean> = T extends { | ||
} | ||
type Options<Data extends DataShape = DataShape, ThrowOnError extends boolean = boolean> = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & Omit<Data, 'url'>; | ||
type OptionsLegacyParser<Data = unknown, ThrowOnError extends boolean = boolean> = Data extends { | ||
body?: any; | ||
} ? T extends { | ||
} ? Data extends { | ||
headers?: any; | ||
} ? OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'headers'> & Omit<T, 'url'> : OmitKeys<OptionsBase<ThrowOnError>, 'body'> & Omit<T, 'url'> & Pick<OptionsBase<ThrowOnError>, 'headers'> : T extends { | ||
} ? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & Data : OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> & Data & Pick<RequestOptions<ThrowOnError>, 'headers'> : Data extends { | ||
headers?: any; | ||
} ? OmitKeys<OptionsBase<ThrowOnError>, 'headers'> & Omit<T, 'url'> & Pick<OptionsBase<ThrowOnError>, 'body'> : OptionsBase<ThrowOnError> & Omit<T, 'url'>; | ||
type OptionsLegacyParser<T = unknown, ThrowOnError extends boolean = boolean> = T extends { | ||
body?: any; | ||
} ? T extends { | ||
headers?: any; | ||
} ? OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'headers'> & T : OmitKeys<OptionsBase<ThrowOnError>, 'body'> & T & Pick<OptionsBase<ThrowOnError>, 'headers'> : T extends { | ||
headers?: any; | ||
} ? OmitKeys<OptionsBase<ThrowOnError>, 'headers'> & T & Pick<OptionsBase<ThrowOnError>, 'body'> : OptionsBase<ThrowOnError> & T; | ||
} ? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> & Data & Pick<RequestOptions<ThrowOnError>, 'body'> : OmitKeys<RequestOptions<ThrowOnError>, 'url'> & Data; | ||
declare const createClient: (config?: Config) => Client; | ||
export { type Client, type Config, type Options, type OptionsLegacyParser, type QuerySerializerOptions, type RequestOptionsBase, type RequestResult, createClient, createConfig, formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer }; | ||
export { type Client, type Config, type Options, type OptionsLegacyParser, type QuerySerializerOptions, type RequestOptions, type RequestResult, createClient, createConfig, formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer }; |
@@ -1,5 +0,5 @@ | ||
var U=/\{[^{}]+\}/g,S=({allowReserved:n,name:t,value:r})=>{if(r==null)return "";if(typeof r=="object")throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");return `${t}=${n?r:encodeURIComponent(r)}`},$=n=>{switch(n){case"label":return ".";case"matrix":return ";";case"simple":return ",";default:return "&"}},D=n=>{switch(n){case"form":return ",";case"pipeDelimited":return "|";case"spaceDelimited":return "%20";default:return ","}},_=n=>{switch(n){case"label":return ".";case"matrix":return ";";case"simple":return ",";default:return "&"}},C=({allowReserved:n,explode:t,name:r,style:o,value:a})=>{if(!t){let e=(n?a:a.map(s=>encodeURIComponent(s))).join(D(o));switch(o){case"label":return `.${e}`;case"matrix":return `;${r}=${e}`;case"simple":return e;default:return `${r}=${e}`}}let c=$(o),i=a.map(e=>o==="label"||o==="simple"?n?e:encodeURIComponent(e):S({allowReserved:n,name:r,value:e})).join(c);return o==="label"||o==="matrix"?c+i:i},A=({allowReserved:n,explode:t,name:r,style:o,value:a})=>{if(a instanceof Date)return `${r}=${a.toISOString()}`;if(o!=="deepObject"&&!t){let e=[];Object.entries(a).forEach(([u,g])=>{e=[...e,u,n?g:encodeURIComponent(g)];});let s=e.join(",");switch(o){case"form":return `${r}=${s}`;case"label":return `.${s}`;case"matrix":return `;${r}=${s}`;default:return s}}let c=_(o),i=Object.entries(a).map(([e,s])=>S({allowReserved:n,name:o==="deepObject"?`${r}[${e}]`:e,value:s})).join(c);return o==="label"||o==="matrix"?c+i:i},k=({path:n,url:t})=>{let r=t,o=t.match(U);if(o)for(let a of o){let c=!1,i=a.substring(1,a.length-1),e="simple";i.endsWith("*")&&(c=!0,i=i.substring(0,i.length-1)),i.startsWith(".")?(i=i.substring(1),e="label"):i.startsWith(";")&&(i=i.substring(1),e="matrix");let s=n[i];if(s==null)continue;if(Array.isArray(s)){r=r.replace(a,C({explode:c,name:i,style:e,value:s}));continue}if(typeof s=="object"){r=r.replace(a,A({explode:c,name:i,style:e,value:s}));continue}if(e==="matrix"){r=r.replace(a,`;${S({name:i,value:s})}`);continue}let u=encodeURIComponent(e==="label"?`.${s}`:s);r=r.replace(a,u);}return r},R=({allowReserved:n,array:t,object:r}={})=>a=>{let c=[];if(a&&typeof a=="object")for(let i in a){let e=a[i];if(e!=null){if(Array.isArray(e)){c=[...c,C({allowReserved:n,explode:!0,name:i,style:"form",value:e,...t})];continue}if(typeof e=="object"){c=[...c,A({allowReserved:n,explode:!0,name:i,style:"deepObject",value:e,...r})];continue}c=[...c,S({allowReserved:n,name:i,value:e})];}}return c.join("&")},w=n=>{if(!n)return;let t=n.split(";")[0].trim();if(t.startsWith("application/json")||t.endsWith("+json"))return "json";if(t==="multipart/form-data")return "formData";if(["application/","audio/","image/","video/"].some(r=>t.startsWith(r)))return "blob";if(t.startsWith("text/"))return "text"},P=({baseUrl:n,path:t,query:r,querySerializer:o,url:a})=>{let c=a.startsWith("/")?a:`/${a}`,i=n+c;t&&(i=k({path:t,url:i}));let e=r?o(r):"";return e.startsWith("?")&&(e=e.substring(1)),e&&(i+=`?${e}`),i},O=(n,t)=>{let r={...n,...t};return r.baseUrl?.endsWith("/")&&(r.baseUrl=r.baseUrl.substring(0,r.baseUrl.length-1)),r.headers=x(n.headers,t.headers),r},x=(...n)=>{let t=new Headers;for(let r of n){if(!r||typeof r!="object")continue;let o=r instanceof Headers?r.entries():Object.entries(r);for(let[a,c]of o)if(c===null)t.delete(a);else if(Array.isArray(c))for(let i of c)t.append(a,i);else c!==void 0&&t.set(a,typeof c=="object"?JSON.stringify(c):c);}return t},y=class{_fns;constructor(){this._fns=[];}clear(){this._fns=[];}exists(t){return this._fns.indexOf(t)!==-1}eject(t){let r=this._fns.indexOf(t);r!==-1&&(this._fns=[...this._fns.slice(0,r),...this._fns.slice(r+1)]);}use(t){this._fns=[...this._fns,t];}},E=()=>({error:new y,request:new y,response:new y}),z=(n,t,r)=>{typeof r=="string"||r instanceof Blob?n.append(t,r):n.append(t,JSON.stringify(r));},H={bodySerializer:n=>{let t=new FormData;return Object.entries(n).forEach(([r,o])=>{o!=null&&(Array.isArray(o)?o.forEach(a=>z(t,r,a)):z(t,r,o));}),t}},I={bodySerializer:n=>JSON.stringify(n)},j=(n,t,r)=>{typeof r=="string"?n.append(t,r):n.append(t,JSON.stringify(r));},W={bodySerializer:n=>{let t=new URLSearchParams;return Object.entries(n).forEach(([r,o])=>{o!=null&&(Array.isArray(o)?o.forEach(a=>j(t,r,a)):j(t,r,o));}),t}},B=R({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),N={"Content-Type":"application/json"},q=(n={})=>({...I,baseUrl:"",headers:N,parseAs:"auto",querySerializer:B,...n});var L=(n={})=>{let t=O(q(),n),r=()=>({...t}),o=e=>(t=O(t,e),r()),a=e=>P({baseUrl:e.baseUrl??"",path:e.path,query:e.query,querySerializer:typeof e.querySerializer=="function"?e.querySerializer:R(e.querySerializer),url:e.url}),c=E(),i=async e=>{let s={...t,...e,fetch:e.fetch??t.fetch??globalThis.fetch,headers:x(t.headers,e.headers)};s.body&&s.bodySerializer&&(s.body=s.bodySerializer(s.body)),s.body||s.headers.delete("Content-Type");let u=a(s),g={redirect:"follow",...s},f=new Request(u,g);for(let p of c.request._fns)f=await p(f,s);let T=s.fetch,l=await T(f);for(let p of c.response._fns)l=await p(l,f,s);let m={request:f,response:l};if(l.ok){if(l.status===204||l.headers.get("Content-Length")==="0")return {data:{},...m};if(s.parseAs==="stream")return {data:l.body,...m};let p=(s.parseAs==="auto"?w(l.headers.get("Content-Type")):s.parseAs)??"json",b=await l[p]();return p==="json"&&s.responseTransformer&&(b=await s.responseTransformer(b)),{data:b,...m}}let h=await l.text();try{h=JSON.parse(h);}catch{}let d=h;for(let p of c.error._fns)d=await p(h,l,f,s);if(d=d||{},s.throwOnError)throw d;return {error:d,...m}};return {buildUrl:a,connect:e=>i({...e,method:"CONNECT"}),delete:e=>i({...e,method:"DELETE"}),get:e=>i({...e,method:"GET"}),getConfig:r,head:e=>i({...e,method:"HEAD"}),interceptors:c,options:e=>i({...e,method:"OPTIONS"}),patch:e=>i({...e,method:"PATCH"}),post:e=>i({...e,method:"POST"}),put:e=>i({...e,method:"PUT"}),request:i,setConfig:o,trace:e=>i({...e,method:"TRACE"})}}; | ||
var U=/\{[^{}]+\}/g,S=({allowReserved:n,name:t,value:r})=>{if(r==null)return "";if(typeof r=="object")throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");return `${t}=${n?r:encodeURIComponent(r)}`},$=n=>{switch(n){case"label":return ".";case"matrix":return ";";case"simple":return ",";default:return "&"}},D=n=>{switch(n){case"form":return ",";case"pipeDelimited":return "|";case"spaceDelimited":return "%20";default:return ","}},_=n=>{switch(n){case"label":return ".";case"matrix":return ";";case"simple":return ",";default:return "&"}},C=({allowReserved:n,explode:t,name:r,style:o,value:a})=>{if(!t){let e=(n?a:a.map(s=>encodeURIComponent(s))).join(D(o));switch(o){case"label":return `.${e}`;case"matrix":return `;${r}=${e}`;case"simple":return e;default:return `${r}=${e}`}}let c=$(o),i=a.map(e=>o==="label"||o==="simple"?n?e:encodeURIComponent(e):S({allowReserved:n,name:r,value:e})).join(c);return o==="label"||o==="matrix"?c+i:i},A=({allowReserved:n,explode:t,name:r,style:o,value:a})=>{if(a instanceof Date)return `${r}=${a.toISOString()}`;if(o!=="deepObject"&&!t){let e=[];Object.entries(a).forEach(([u,g])=>{e=[...e,u,n?g:encodeURIComponent(g)];});let s=e.join(",");switch(o){case"form":return `${r}=${s}`;case"label":return `.${s}`;case"matrix":return `;${r}=${s}`;default:return s}}let c=_(o),i=Object.entries(a).map(([e,s])=>S({allowReserved:n,name:o==="deepObject"?`${r}[${e}]`:e,value:s})).join(c);return o==="label"||o==="matrix"?c+i:i},k=({path:n,url:t})=>{let r=t,o=t.match(U);if(o)for(let a of o){let c=!1,i=a.substring(1,a.length-1),e="simple";i.endsWith("*")&&(c=!0,i=i.substring(0,i.length-1)),i.startsWith(".")?(i=i.substring(1),e="label"):i.startsWith(";")&&(i=i.substring(1),e="matrix");let s=n[i];if(s==null)continue;if(Array.isArray(s)){r=r.replace(a,C({explode:c,name:i,style:e,value:s}));continue}if(typeof s=="object"){r=r.replace(a,A({explode:c,name:i,style:e,value:s}));continue}if(e==="matrix"){r=r.replace(a,`;${S({name:i,value:s})}`);continue}let u=encodeURIComponent(e==="label"?`.${s}`:s);r=r.replace(a,u);}return r},R=({allowReserved:n,array:t,object:r}={})=>a=>{let c=[];if(a&&typeof a=="object")for(let i in a){let e=a[i];if(e!=null){if(Array.isArray(e)){c=[...c,C({allowReserved:n,explode:!0,name:i,style:"form",value:e,...t})];continue}if(typeof e=="object"){c=[...c,A({allowReserved:n,explode:!0,name:i,style:"deepObject",value:e,...r})];continue}c=[...c,S({allowReserved:n,name:i,value:e})];}}return c.join("&")},w=n=>{if(!n)return;let t=n.split(";")[0].trim();if(t.startsWith("application/json")||t.endsWith("+json"))return "json";if(t==="multipart/form-data")return "formData";if(["application/","audio/","image/","video/"].some(r=>t.startsWith(r)))return "blob";if(t.startsWith("text/"))return "text"},P=({baseUrl:n,path:t,query:r,querySerializer:o,url:a})=>{let c=a.startsWith("/")?a:`/${a}`,i=n+c;t&&(i=k({path:t,url:i}));let e=r?o(r):"";return e.startsWith("?")&&(e=e.substring(1)),e&&(i+=`?${e}`),i},O=(n,t)=>{let r={...n,...t};return r.baseUrl?.endsWith("/")&&(r.baseUrl=r.baseUrl.substring(0,r.baseUrl.length-1)),r.headers=x(n.headers,t.headers),r},x=(...n)=>{let t=new Headers;for(let r of n){if(!r||typeof r!="object")continue;let o=r instanceof Headers?r.entries():Object.entries(r);for(let[a,c]of o)if(c===null)t.delete(a);else if(Array.isArray(c))for(let i of c)t.append(a,i);else c!==void 0&&t.set(a,typeof c=="object"?JSON.stringify(c):c);}return t},y=class{_fns;constructor(){this._fns=[];}clear(){this._fns=[];}exists(t){return this._fns.indexOf(t)!==-1}eject(t){let r=this._fns.indexOf(t);r!==-1&&(this._fns=[...this._fns.slice(0,r),...this._fns.slice(r+1)]);}use(t){this._fns=[...this._fns,t];}},E=()=>({error:new y,request:new y,response:new y}),j=(n,t,r)=>{typeof r=="string"||r instanceof Blob?n.append(t,r):n.append(t,JSON.stringify(r));},H={bodySerializer:n=>{let t=new FormData;return Object.entries(n).forEach(([r,o])=>{o!=null&&(Array.isArray(o)?o.forEach(a=>j(t,r,a)):j(t,r,o));}),t}},I={bodySerializer:n=>JSON.stringify(n)},q=(n,t,r)=>{typeof r=="string"?n.append(t,r):n.append(t,JSON.stringify(r));},W={bodySerializer:n=>{let t=new URLSearchParams;return Object.entries(n).forEach(([r,o])=>{o!=null&&(Array.isArray(o)?o.forEach(a=>q(t,r,a)):q(t,r,o));}),t}},N=R({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),B={"Content-Type":"application/json"},z=(n={})=>({...I,baseUrl:"",headers:B,parseAs:"auto",querySerializer:N,...n});var L=(n={})=>{let t=O(z(),n),r=()=>({...t}),o=e=>(t=O(t,e),r()),a=e=>P({baseUrl:e.baseUrl??"",path:e.path,query:e.query,querySerializer:typeof e.querySerializer=="function"?e.querySerializer:R(e.querySerializer),url:e.url}),c=E(),i=async e=>{let s={...t,...e,fetch:e.fetch??t.fetch??globalThis.fetch,headers:x(t.headers,e.headers)};s.body&&s.bodySerializer&&(s.body=s.bodySerializer(s.body)),s.body||s.headers.delete("Content-Type");let u=a(s),g={redirect:"follow",...s},f=new Request(u,g);for(let p of c.request._fns)f=await p(f,s);let T=s.fetch,l=await T(f);for(let p of c.response._fns)l=await p(l,f,s);let m={request:f,response:l};if(l.ok){if(l.status===204||l.headers.get("Content-Length")==="0")return {data:{},...m};if(s.parseAs==="stream")return {data:l.body,...m};let p=(s.parseAs==="auto"?w(l.headers.get("Content-Type")):s.parseAs)??"json",b=await l[p]();return p==="json"&&s.responseTransformer&&(b=await s.responseTransformer(b)),{data:b,...m}}let h=await l.text();try{h=JSON.parse(h);}catch{}let d=h;for(let p of c.error._fns)d=await p(h,l,f,s);if(d=d||{},s.throwOnError)throw d;return {error:d,...m}};return {buildUrl:a,connect:e=>i({...e,method:"CONNECT"}),delete:e=>i({...e,method:"DELETE"}),get:e=>i({...e,method:"GET"}),getConfig:r,head:e=>i({...e,method:"HEAD"}),interceptors:c,options:e=>i({...e,method:"OPTIONS"}),patch:e=>i({...e,method:"PATCH"}),post:e=>i({...e,method:"POST"}),put:e=>i({...e,method:"PUT"}),request:i,setConfig:o,trace:e=>i({...e,method:"TRACE"})}}; | ||
export { L as createClient, q as createConfig, H as formDataBodySerializer, I as jsonBodySerializer, W as urlSearchParamsBodySerializer }; | ||
export { L as createClient, z as createConfig, H as formDataBodySerializer, I as jsonBodySerializer, W as urlSearchParamsBodySerializer }; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@hey-api/client-fetch", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "🚀 Fetch API client for `@hey-api/openapi-ts` codegen.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://heyapi.dev/", |
@@ -50,4 +50,3 @@ import type { Client, Config, RequestOptions } from './types'; | ||
const request: Client['request'] = async (options) => { | ||
// @ts-expect-error | ||
const opts: RequestOptions = { | ||
const opts = { | ||
..._config, | ||
@@ -174,3 +173,3 @@ ...options, | ||
OptionsLegacyParser, | ||
RequestOptionsBase, | ||
RequestOptions, | ||
RequestResult, | ||
@@ -177,0 +176,0 @@ } from './types'; |
111
src/types.ts
@@ -18,13 +18,2 @@ import type { | ||
/** | ||
* Any body that you want to add to your request. | ||
* | ||
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body} | ||
*/ | ||
body?: | ||
| RequestInit['body'] | ||
| Record<string, unknown> | ||
| Array<Record<string, unknown>> | ||
| Array<unknown> | ||
| number; | ||
/** | ||
* A function for serializing request body parameter. By default, | ||
@@ -102,6 +91,23 @@ * {@link JSON.stringify()} will be used. | ||
export interface RequestOptionsBase< | ||
ThrowOnError extends boolean, | ||
export interface RequestOptions< | ||
ThrowOnError extends boolean = boolean, | ||
Url extends string = string, | ||
> extends Config<ThrowOnError> { | ||
/** | ||
* Any body that you want to add to your request. | ||
* | ||
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body} | ||
*/ | ||
body?: | ||
| RequestInit['body'] | ||
| Record<string, unknown> | ||
| Array<Record<string, unknown>> | ||
| Array<unknown> | ||
| number; | ||
/** | ||
* You can provide a client instance returned by `createClient()` instead of | ||
* individual options. This might be also useful if you want to implement a | ||
* custom client. | ||
*/ | ||
client?: Client; | ||
path?: Record<string, unknown>; | ||
@@ -137,3 +143,3 @@ query?: Record<string, unknown>; | ||
>( | ||
options: Omit<RequestOptionsBase<ThrowOnError>, 'method'>, | ||
options: Omit<RequestOptions<ThrowOnError>, 'method'>, | ||
) => RequestResult<Data, TError, ThrowOnError>; | ||
@@ -146,4 +152,4 @@ | ||
>( | ||
options: Omit<RequestOptionsBase<ThrowOnError>, 'method'> & | ||
Pick<Required<RequestOptionsBase<ThrowOnError>>, 'method'>, | ||
options: Omit<RequestOptions<ThrowOnError>, 'method'> & | ||
Pick<Required<RequestOptions<ThrowOnError>>, 'method'>, | ||
) => RequestResult<Data, TError, ThrowOnError>; | ||
@@ -160,3 +166,12 @@ | ||
*/ | ||
buildUrl: <T extends { url: string }>(options: T & Options<T>) => string; | ||
buildUrl: < | ||
Data extends { | ||
body?: unknown; | ||
path?: Record<string, unknown>; | ||
query?: Record<string, unknown>; | ||
url: string; | ||
}, | ||
>( | ||
options: Pick<Data, 'url'> & Options<Data>, | ||
) => string; | ||
connect: MethodFn; | ||
@@ -177,47 +192,29 @@ delete: MethodFn; | ||
export type RequestOptions = RequestOptionsBase<false> & | ||
Config<false> & { | ||
headers: Headers; | ||
}; | ||
interface DataShape { | ||
body?: unknown; | ||
headers?: unknown; | ||
path?: unknown; | ||
query?: unknown; | ||
url: string; | ||
} | ||
type OptionsBase<ThrowOnError extends boolean> = Omit< | ||
RequestOptionsBase<ThrowOnError>, | ||
'url' | ||
> & { | ||
/** | ||
* You can provide a client instance returned by `createClient()` instead of | ||
* individual options. This might be also useful if you want to implement a | ||
* custom client. | ||
*/ | ||
client?: Client; | ||
}; | ||
export type Options< | ||
T extends { url: string } = { url: string }, | ||
Data extends DataShape = DataShape, | ||
ThrowOnError extends boolean = boolean, | ||
> = T extends { body?: any } | ||
? T extends { headers?: any } | ||
? OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'headers'> & Omit<T, 'url'> | ||
: OmitKeys<OptionsBase<ThrowOnError>, 'body'> & | ||
Omit<T, 'url'> & | ||
Pick<OptionsBase<ThrowOnError>, 'headers'> | ||
: T extends { headers?: any } | ||
? OmitKeys<OptionsBase<ThrowOnError>, 'headers'> & | ||
Omit<T, 'url'> & | ||
Pick<OptionsBase<ThrowOnError>, 'body'> | ||
: OptionsBase<ThrowOnError> & Omit<T, 'url'>; | ||
> = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & | ||
Omit<Data, 'url'>; | ||
export type OptionsLegacyParser< | ||
T = unknown, | ||
Data = unknown, | ||
ThrowOnError extends boolean = boolean, | ||
> = T extends { body?: any } | ||
? T extends { headers?: any } | ||
? OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'headers'> & T | ||
: OmitKeys<OptionsBase<ThrowOnError>, 'body'> & | ||
T & | ||
Pick<OptionsBase<ThrowOnError>, 'headers'> | ||
: T extends { headers?: any } | ||
? OmitKeys<OptionsBase<ThrowOnError>, 'headers'> & | ||
T & | ||
Pick<OptionsBase<ThrowOnError>, 'body'> | ||
: OptionsBase<ThrowOnError> & T; | ||
> = Data extends { body?: any } | ||
? Data extends { headers?: any } | ||
? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & Data | ||
: OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> & | ||
Data & | ||
Pick<RequestOptions<ThrowOnError>, 'headers'> | ||
: Data extends { headers?: any } | ||
? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> & | ||
Data & | ||
Pick<RequestOptions<ThrowOnError>, 'body'> | ||
: OmitKeys<RequestOptions<ThrowOnError>, 'url'> & Data; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
120275
1104