@hey-api/client-axios
Advanced tools
Comparing version
@@ -33,4 +33,2 @@ import { CreateAxiosDefaults, AxiosStatic, AxiosResponse, AxiosError, AxiosInstance } from 'axios'; | ||
/** | ||
* **This feature works only with the [experimental parser](https://heyapi.dev/openapi-ts/configuration#parser)** | ||
* | ||
* Auth token or a function returning auth token. The resolved value will be | ||
@@ -82,4 +80,2 @@ * added to the request payload as defined by its `security` array. | ||
/** | ||
* **This feature works only with the [experimental parser](https://heyapi.dev/openapi-ts/configuration#parser)** | ||
* | ||
* A function validating response data. This is useful if you want to ensure | ||
@@ -125,3 +121,3 @@ * the response conforms to the desired shape, so it can be safely passed to | ||
} | ||
type RequestResult<Data = unknown, TError = unknown, ThrowOnError extends boolean = boolean> = ThrowOnError extends true ? Promise<AxiosResponse<Data>> : Promise<(AxiosResponse<Data> & { | ||
type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean> = ThrowOnError extends true ? Promise<AxiosResponse<TData>> : Promise<(AxiosResponse<TData> & { | ||
error: undefined; | ||
@@ -132,4 +128,4 @@ }) | (AxiosError<TError> & { | ||
})>; | ||
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>; | ||
type MethodFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<ThrowOnError>, 'method'>) => RequestResult<TData, TError, ThrowOnError>; | ||
type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<ThrowOnError>, 'method'> & Pick<Required<RequestOptions<ThrowOnError>>, 'method'>) => RequestResult<TData, TError, ThrowOnError>; | ||
interface Client { | ||
@@ -139,3 +135,3 @@ /** | ||
*/ | ||
buildUrl: <Data extends { | ||
buildUrl: <TData extends { | ||
body?: unknown; | ||
@@ -145,3 +141,3 @@ path?: Record<string, unknown>; | ||
url: string; | ||
}>(options: Pick<Data, 'url'> & Omit<Options<Data>, 'axios'>) => string; | ||
}>(options: Pick<TData, 'url'> & Omit<Options<TData>, 'axios'>) => string; | ||
delete: MethodFn; | ||
@@ -166,10 +162,10 @@ get: MethodFn; | ||
} | ||
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 { | ||
type Options<TData extends DataShape = DataShape, ThrowOnError extends boolean = boolean> = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & Omit<TData, 'url'>; | ||
type OptionsLegacyParser<TData = unknown, ThrowOnError extends boolean = boolean> = TData extends { | ||
body?: any; | ||
} ? Data extends { | ||
} ? TData extends { | ||
headers?: any; | ||
} ? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & Data : OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> & Data & Pick<RequestOptions<ThrowOnError>, 'headers'> : Data extends { | ||
} ? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & TData : OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> & TData & Pick<RequestOptions<ThrowOnError>, 'headers'> : TData extends { | ||
headers?: any; | ||
} ? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> & Data & Pick<RequestOptions<ThrowOnError>, 'body'> : OmitKeys<RequestOptions<ThrowOnError>, 'url'> & Data; | ||
} ? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> & TData & Pick<RequestOptions<ThrowOnError>, 'body'> : OmitKeys<RequestOptions<ThrowOnError>, 'url'> & TData; | ||
@@ -176,0 +172,0 @@ declare const createClient: (config: Config) => Client; |
@@ -1,7 +0,2 @@ | ||
import V from 'axios'; | ||
var R=/\{[^{}]+\}/g,f=({allowReserved:t,name:e,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 `${e}=${t?r:encodeURIComponent(r)}`},C=t=>{switch(t){case"label":return ".";case"matrix":return ";";case"simple":return ",";default:return "&"}},P=t=>{switch(t){case"form":return ",";case"pipeDelimited":return "|";case"spaceDelimited":return "%20";default:return ","}},q=t=>{switch(t){case"label":return ".";case"matrix":return ";";case"simple":return ",";default:return "&"}},O=({allowReserved:t,explode:e,name:r,style:n,value:i})=>{if(!e){let a=(t?i:i.map(o=>encodeURIComponent(o))).join(P(n));switch(n){case"label":return `.${a}`;case"matrix":return `;${r}=${a}`;case"simple":return a;default:return `${r}=${a}`}}let s=C(n),c=i.map(a=>n==="label"||n==="simple"?t?a:encodeURIComponent(a):f({allowReserved:t,name:r,value:a})).join(s);return n==="label"||n==="matrix"?s+c:c},A=({allowReserved:t,explode:e,name:r,style:n,value:i})=>{if(n!=="deepObject"&&!e){let a=[];Object.entries(i).forEach(([u,d])=>{a=[...a,u,t?d:encodeURIComponent(d)];});let o=a.join(",");switch(n){case"form":return `${r}=${o}`;case"label":return `.${o}`;case"matrix":return `;${r}=${o}`;default:return o}}let s=q(n),c=Object.entries(i).map(([a,o])=>f({allowReserved:t,name:n==="deepObject"?`${r}[${a}]`:a,value:o})).join(s);return n==="label"||n==="matrix"?s+c:c},$=({path:t,url:e})=>{let r=e,n=e.match(R);if(n)for(let i of n){let s=false,c=i.substring(1,i.length-1),a="simple";c.endsWith("*")&&(s=true,c=c.substring(0,c.length-1)),c.startsWith(".")?(c=c.substring(1),a="label"):c.startsWith(";")&&(c=c.substring(1),a="matrix");let o=t[c];if(o==null)continue;if(Array.isArray(o)){r=r.replace(i,O({explode:s,name:c,style:a,value:o}));continue}if(typeof o=="object"){r=r.replace(i,A({explode:s,name:c,style:a,value:o}));continue}if(a==="matrix"){r=r.replace(i,`;${f({name:c,value:o})}`);continue}let u=encodeURIComponent(a==="label"?`.${o}`:o);r=r.replace(i,u);}return r},T=({allowReserved:t,array:e,object:r}={})=>i=>{let s=[];if(i&&typeof i=="object")for(let c in i){let a=i[c];if(a!=null){if(Array.isArray(a)){s=[...s,O({allowReserved:t,explode:true,name:c,style:"form",value:a,...e})];continue}if(typeof a=="object"){s=[...s,A({allowReserved:t,explode:true,name:c,style:"deepObject",value:a,...r})];continue}s=[...s,f({allowReserved:t,name:c,value:a})];}}return s.join("&")},k=async(t,e)=>{let r=typeof e=="function"?await e(t):e;if(r)return t.scheme==="bearer"?`Bearer ${r}`:t.scheme==="basic"?`Basic ${btoa(r)}`:r},j=async({security:t,...e})=>{for(let r of t){let n=await k(r,e.auth);if(!n)continue;let i=r.name??"Authorization";switch(r.in){case"query":e.query||(e.query={}),e.query[i]=n;break;case"header":default:e.headers[i]=n;break}return}},h=t=>U({path:t.path,query:t.paramsSerializer?undefined:t.query,querySerializer:typeof t.querySerializer=="function"?t.querySerializer:T(t.querySerializer),url:t.url}),U=({path:t,query:e,querySerializer:r,url:n})=>{let s=n.startsWith("/")?n:`/${n}`;t&&(s=$({path:t,url:s}));let c=e?r(e):"";return c.startsWith("?")&&(c=c.substring(1)),c&&(s+=`?${c}`),s},x=(t,e,r)=>{typeof r=="string"||r instanceof Blob?t.append(e,r):t.append(e,JSON.stringify(r));},g=(t,e)=>{let r={...t,...e};return r.headers=y(t.headers,e.headers),r},E=["common","delete","get","head","patch","post","put"],y=(...t)=>{let e={};for(let r of t){if(!r||typeof r!="object")continue;let n=Object.entries(r);for(let[i,s]of n)if(E.includes(i)&&typeof s=="object")e[i]={...e[i],...s};else if(s===null)delete e[i];else if(Array.isArray(s))for(let c of s)e[i]=[...e[i]??[],c];else s!==undefined&&(e[i]=typeof s=="object"?JSON.stringify(s):s);}return e},B={bodySerializer:t=>{let e=new FormData;return Object.entries(t).forEach(([r,n])=>{n!=null&&(Array.isArray(n)?n.forEach(i=>x(e,r,i)):x(e,r,n));}),e}},D={bodySerializer:t=>JSON.stringify(t)},z=(t,e,r)=>{typeof r=="string"?t.append(e,r):t.append(e,JSON.stringify(r));},H={bodySerializer:t=>{let e=new URLSearchParams;return Object.entries(t).forEach(([r,n])=>{n!=null&&(Array.isArray(n)?n.forEach(i=>z(e,r,i)):z(e,r,n));}),e}},S=(t={})=>({baseURL:"",...t});var Q=t=>{let e=g(S(),t),{auth:r,...n}=e,i=V.create(n),s=()=>({...e}),c=o=>(e=g(e,o),i.defaults={...i.defaults,...e,headers:y(i.defaults.headers,e.headers)},s()),a=async o=>{let u={...e,...o,axios:o.axios??e.axios??i,headers:y(e.headers,o.headers)};u.security&&await j({...u,security:u.security}),u.body&&u.bodySerializer&&(u.body=u.bodySerializer(u.body));let d=h(u);try{let m=u.axios,{auth:l,...w}=u,b=await m({...w,data:u.body,headers:u.headers,params:u.paramsSerializer?u.query:void 0,url:d}),{data:p}=b;return u.responseType==="json"&&(u.responseValidator&&await u.responseValidator(p),u.responseTransformer&&(p=await u.responseTransformer(p))),{...b,data:p??{}}}catch(m){let l=m;if(u.throwOnError)throw l;return l.error=l.response?.data??{},l}};return {buildUrl:h,delete:o=>a({...o,method:"delete"}),get:o=>a({...o,method:"get"}),getConfig:s,head:o=>a({...o,method:"head"}),instance:i,options:o=>a({...o,method:"options"}),patch:o=>a({...o,method:"patch"}),post:o=>a({...o,method:"post"}),put:o=>a({...o,method:"put"}),request:a,setConfig:c}}; | ||
export { Q as createClient, S as createConfig, B as formDataBodySerializer, D as jsonBodySerializer, H as urlSearchParamsBodySerializer }; | ||
//# sourceMappingURL=index.js.map | ||
import V from'axios';var R=/\{[^{}]+\}/g,f=({allowReserved:t,name:e,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 `${e}=${t?r:encodeURIComponent(r)}`},C=t=>{switch(t){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},P=t=>{switch(t){case "form":return ",";case "pipeDelimited":return "|";case "spaceDelimited":return "%20";default:return ","}},q=t=>{switch(t){case "label":return ".";case "matrix":return ";";case "simple":return ",";default:return "&"}},O=({allowReserved:t,explode:e,name:r,style:n,value:i})=>{if(!e){let a=(t?i:i.map(o=>encodeURIComponent(o))).join(P(n));switch(n){case "label":return `.${a}`;case "matrix":return `;${r}=${a}`;case "simple":return a;default:return `${r}=${a}`}}let s=C(n),c=i.map(a=>n==="label"||n==="simple"?t?a:encodeURIComponent(a):f({allowReserved:t,name:r,value:a})).join(s);return n==="label"||n==="matrix"?s+c:c},A=({allowReserved:t,explode:e,name:r,style:n,value:i})=>{if(n!=="deepObject"&&!e){let a=[];Object.entries(i).forEach(([u,d])=>{a=[...a,u,t?d:encodeURIComponent(d)];});let o=a.join(",");switch(n){case "form":return `${r}=${o}`;case "label":return `.${o}`;case "matrix":return `;${r}=${o}`;default:return o}}let s=q(n),c=Object.entries(i).map(([a,o])=>f({allowReserved:t,name:n==="deepObject"?`${r}[${a}]`:a,value:o})).join(s);return n==="label"||n==="matrix"?s+c:c},$=({path:t,url:e})=>{let r=e,n=e.match(R);if(n)for(let i of n){let s=false,c=i.substring(1,i.length-1),a="simple";c.endsWith("*")&&(s=true,c=c.substring(0,c.length-1)),c.startsWith(".")?(c=c.substring(1),a="label"):c.startsWith(";")&&(c=c.substring(1),a="matrix");let o=t[c];if(o==null)continue;if(Array.isArray(o)){r=r.replace(i,O({explode:s,name:c,style:a,value:o}));continue}if(typeof o=="object"){r=r.replace(i,A({explode:s,name:c,style:a,value:o}));continue}if(a==="matrix"){r=r.replace(i,`;${f({name:c,value:o})}`);continue}let u=encodeURIComponent(a==="label"?`.${o}`:o);r=r.replace(i,u);}return r},T=({allowReserved:t,array:e,object:r}={})=>i=>{let s=[];if(i&&typeof i=="object")for(let c in i){let a=i[c];if(a!=null){if(Array.isArray(a)){s=[...s,O({allowReserved:t,explode:true,name:c,style:"form",value:a,...e})];continue}if(typeof a=="object"){s=[...s,A({allowReserved:t,explode:true,name:c,style:"deepObject",value:a,...r})];continue}s=[...s,f({allowReserved:t,name:c,value:a})];}}return s.join("&")},k=async(t,e)=>{let r=typeof e=="function"?await e(t):e;if(r)return t.scheme==="bearer"?`Bearer ${r}`:t.scheme==="basic"?`Basic ${btoa(r)}`:r},j=async({security:t,...e})=>{for(let r of t){let n=await k(r,e.auth);if(!n)continue;let i=r.name??"Authorization";switch(r.in){case "query":e.query||(e.query={}),e.query[i]=n;break;case "header":default:e.headers[i]=n;break}return}},h=t=>U({path:t.path,query:t.paramsSerializer?undefined:t.query,querySerializer:typeof t.querySerializer=="function"?t.querySerializer:T(t.querySerializer),url:t.url}),U=({path:t,query:e,querySerializer:r,url:n})=>{let s=n.startsWith("/")?n:`/${n}`;t&&(s=$({path:t,url:s}));let c=e?r(e):"";return c.startsWith("?")&&(c=c.substring(1)),c&&(s+=`?${c}`),s},x=(t,e,r)=>{typeof r=="string"||r instanceof Blob?t.append(e,r):t.append(e,JSON.stringify(r));},g=(t,e)=>{let r={...t,...e};return r.headers=y(t.headers,e.headers),r},E=["common","delete","get","head","patch","post","put"],y=(...t)=>{let e={};for(let r of t){if(!r||typeof r!="object")continue;let n=Object.entries(r);for(let[i,s]of n)if(E.includes(i)&&typeof s=="object")e[i]={...e[i],...s};else if(s===null)delete e[i];else if(Array.isArray(s))for(let c of s)e[i]=[...e[i]??[],c];else s!==undefined&&(e[i]=typeof s=="object"?JSON.stringify(s):s);}return e},B={bodySerializer:t=>{let e=new FormData;return Object.entries(t).forEach(([r,n])=>{n!=null&&(Array.isArray(n)?n.forEach(i=>x(e,r,i)):x(e,r,n));}),e}},D={bodySerializer:t=>JSON.stringify(t)},z=(t,e,r)=>{typeof r=="string"?t.append(e,r):t.append(e,JSON.stringify(r));},H={bodySerializer:t=>{let e=new URLSearchParams;return Object.entries(t).forEach(([r,n])=>{n!=null&&(Array.isArray(n)?n.forEach(i=>z(e,r,i)):z(e,r,n));}),e}},S=(t={})=>({baseURL:"",...t});var Q=t=>{let e=g(S(),t),{auth:r,...n}=e,i=V.create(n),s=()=>({...e}),c=o=>(e=g(e,o),i.defaults={...i.defaults,...e,headers:y(i.defaults.headers,e.headers)},s()),a=async o=>{let u={...e,...o,axios:o.axios??e.axios??i,headers:y(e.headers,o.headers)};u.security&&await j({...u,security:u.security}),u.body&&u.bodySerializer&&(u.body=u.bodySerializer(u.body));let d=h(u);try{let m=u.axios,{auth:l,...w}=u,b=await m({...w,data:u.body,headers:u.headers,params:u.paramsSerializer?u.query:void 0,url:d}),{data:p}=b;return u.responseType==="json"&&(u.responseValidator&&await u.responseValidator(p),u.responseTransformer&&(p=await u.responseTransformer(p))),{...b,data:p??{}}}catch(m){let l=m;if(u.throwOnError)throw l;return l.error=l.response?.data??{},l}};return {buildUrl:h,delete:o=>a({...o,method:"delete"}),get:o=>a({...o,method:"get"}),getConfig:s,head:o=>a({...o,method:"head"}),instance:i,options:o=>a({...o,method:"options"}),patch:o=>a({...o,method:"patch"}),post:o=>a({...o,method:"post"}),put:o=>a({...o,method:"put"}),request:a,setConfig:c}};export{Q as createClient,S as createConfig,B as formDataBodySerializer,D as jsonBodySerializer,H as urlSearchParamsBodySerializer};//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@hey-api/client-axios", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "🚀 Axios client for `@hey-api/openapi-ts` codegen.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://heyapi.dev/", |
@@ -20,4 +20,2 @@ import type { | ||
/** | ||
* **This feature works only with the [experimental parser](https://heyapi.dev/openapi-ts/configuration#parser)** | ||
* | ||
* Auth token or a function returning auth token. The resolved value will be | ||
@@ -89,4 +87,2 @@ * added to the request payload as defined by its `security` array. | ||
/** | ||
* **This feature works only with the [experimental parser](https://heyapi.dev/openapi-ts/configuration#parser)** | ||
* | ||
* A function validating response data. This is useful if you want to ensure | ||
@@ -140,9 +136,9 @@ * the response conforms to the desired shape, so it can be safely passed to | ||
export type RequestResult< | ||
Data = unknown, | ||
TData = unknown, | ||
TError = unknown, | ||
ThrowOnError extends boolean = boolean, | ||
> = ThrowOnError extends true | ||
? Promise<AxiosResponse<Data>> | ||
? Promise<AxiosResponse<TData>> | ||
: Promise< | ||
| (AxiosResponse<Data> & { error: undefined }) | ||
| (AxiosResponse<TData> & { error: undefined }) | ||
| (AxiosError<TError> & { data: undefined; error: TError }) | ||
@@ -152,3 +148,3 @@ >; | ||
type MethodFn = < | ||
Data = unknown, | ||
TData = unknown, | ||
TError = unknown, | ||
@@ -158,6 +154,6 @@ ThrowOnError extends boolean = false, | ||
options: Omit<RequestOptions<ThrowOnError>, 'method'>, | ||
) => RequestResult<Data, TError, ThrowOnError>; | ||
) => RequestResult<TData, TError, ThrowOnError>; | ||
type RequestFn = < | ||
Data = unknown, | ||
TData = unknown, | ||
TError = unknown, | ||
@@ -168,3 +164,3 @@ ThrowOnError extends boolean = false, | ||
Pick<Required<RequestOptions<ThrowOnError>>, 'method'>, | ||
) => RequestResult<Data, TError, ThrowOnError>; | ||
) => RequestResult<TData, TError, ThrowOnError>; | ||
@@ -176,3 +172,3 @@ export interface Client { | ||
buildUrl: < | ||
Data extends { | ||
TData extends { | ||
body?: unknown; | ||
@@ -184,3 +180,3 @@ path?: Record<string, unknown>; | ||
>( | ||
options: Pick<Data, 'url'> & Omit<Options<Data>, 'axios'>, | ||
options: Pick<TData, 'url'> & Omit<Options<TData>, 'axios'>, | ||
) => string; | ||
@@ -209,20 +205,20 @@ delete: MethodFn; | ||
export type Options< | ||
Data extends DataShape = DataShape, | ||
TData extends DataShape = DataShape, | ||
ThrowOnError extends boolean = boolean, | ||
> = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & | ||
Omit<Data, 'url'>; | ||
Omit<TData, 'url'>; | ||
export type OptionsLegacyParser< | ||
Data = unknown, | ||
TData = unknown, | ||
ThrowOnError extends boolean = boolean, | ||
> = Data extends { body?: any } | ||
? Data extends { headers?: any } | ||
? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & Data | ||
> = TData extends { body?: any } | ||
? TData extends { headers?: any } | ||
? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & TData | ||
: OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> & | ||
Data & | ||
TData & | ||
Pick<RequestOptions<ThrowOnError>, 'headers'> | ||
: Data extends { headers?: any } | ||
: TData extends { headers?: any } | ||
? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> & | ||
Data & | ||
TData & | ||
Pick<RequestOptions<ThrowOnError>, 'body'> | ||
: OmitKeys<RequestOptions<ThrowOnError>, 'url'> & Data; | ||
: OmitKeys<RequestOptions<ThrowOnError>, 'url'> & TData; |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
114817
-0.62%1200
-1.64%