Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing isAxiosError typings to take generics #3816

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
127 changes: 89 additions & 38 deletions index.d.ts
Expand Up @@ -3,7 +3,7 @@
export type AxiosRequestHeaders = Record<string, string | number | boolean>;

export type AxiosResponseHeaders = Record<string, string> & {
"set-cookie"?: string[]
'set-cookie'?: string[];
};

export interface AxiosRequestTransformer {
Expand Down Expand Up @@ -34,16 +34,26 @@ export interface AxiosProxyConfig {
}

export type Method =
| 'get' | 'GET'
| 'delete' | 'DELETE'
| 'head' | 'HEAD'
| 'options' | 'OPTIONS'
| 'post' | 'POST'
| 'put' | 'PUT'
| 'patch' | 'PATCH'
| 'purge' | 'PURGE'
| 'link' | 'LINK'
| 'unlink' | 'UNLINK';
| 'get'
| 'GET'
| 'delete'
| 'DELETE'
| 'head'
| 'HEAD'
| 'options'
| 'OPTIONS'
| 'post'
| 'POST'
| 'put'
| 'PUT'
| 'patch'
| 'PATCH'
| 'purge'
| 'PURGE'
| 'link'
| 'LINK'
| 'unlink'
| 'UNLINK';

export type ResponseType =
| 'arraybuffer'
Expand All @@ -53,19 +63,31 @@ export type ResponseType =
| 'text'
| 'stream';

export type responseEncoding =
| 'ascii' | 'ASCII'
| 'ansi' | 'ANSI'
| 'binary' | 'BINARY'
| 'base64' | 'BASE64'
| 'base64url' | 'BASE64URL'
| 'hex' | 'HEX'
| 'latin1' | 'LATIN1'
| 'ucs-2' | 'UCS-2'
| 'ucs2' | 'UCS2'
| 'utf-8' | 'UTF-8'
| 'utf8' | 'UTF8'
| 'utf16le' | 'UTF16LE';
export type responseEncoding =
| 'ascii'
| 'ASCII'
| 'ansi'
| 'ANSI'
| 'binary'
| 'BINARY'
| 'base64'
| 'BASE64'
| 'base64url'
| 'BASE64URL'
| 'hex'
| 'HEX'
| 'latin1'
| 'LATIN1'
| 'ucs-2'
| 'UCS-2'
| 'ucs2'
| 'UCS2'
| 'utf-8'
| 'UTF-8'
| 'utf8'
| 'UTF8'
| 'utf16le'
| 'UTF16LE';

export interface TransitionalOptions {
silentJSONParsing?: boolean;
Expand Down Expand Up @@ -126,11 +148,12 @@ export interface HeadersDefaults {
unlink?: AxiosRequestHeaders;
}

export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
export interface AxiosDefaults<D = any>
extends Omit<AxiosRequestConfig<D>, 'headers'> {
headers: HeadersDefaults;
}

export interface AxiosResponse<T = any, D = any> {
export interface AxiosResponse<T = any, D = any> {
data: T;
status: number;
statusText: string;
Expand All @@ -148,8 +171,7 @@ export interface AxiosError<T = any, D = any> extends Error {
toJSON: () => object;
}

export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
}
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {}

export interface CancelStatic {
new (message?: string): Cancel;
Expand Down Expand Up @@ -180,7 +202,10 @@ export interface CancelTokenSource {
}

export interface AxiosInterceptorManager<V> {
use<T = V>(onFulfilled?: (value: V) => T | Promise<T>, onRejected?: (error: any) => any): number;
use<T = V>(
onFulfilled?: (value: V) => T | Promise<T>,
onRejected?: (error: any) => any
): number;
eject(id: number): void;
}

Expand All @@ -192,14 +217,40 @@ export class Axios {
response: AxiosInterceptorManager<AxiosResponse>;
};
getUri(config?: AxiosRequestConfig): string;
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
request<T = any, R = AxiosResponse<T>, D = any>(
config: AxiosRequestConfig<D>
): Promise<R>;
get<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: AxiosRequestConfig<D>
): Promise<R>;
delete<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: AxiosRequestConfig<D>
): Promise<R>;
head<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: AxiosRequestConfig<D>
): Promise<R>;
options<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: AxiosRequestConfig<D>
): Promise<R>;
post<T = any, R = AxiosResponse<T>, D = any>(
url: string,
data?: D,
config?: AxiosRequestConfig<D>
): Promise<R>;
put<T = any, R = AxiosResponse<T>, D = any>(
url: string,
data?: D,
config?: AxiosRequestConfig<D>
): Promise<R>;
patch<T = any, R = AxiosResponse<T>, D = any>(
url: string,
data?: D,
config?: AxiosRequestConfig<D>
): Promise<R>;
}

export interface AxiosInstance extends Axios {
Expand All @@ -216,7 +267,7 @@ export interface AxiosStatic extends AxiosInstance {
isCancel(value: any): boolean;
all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
isAxiosError(payload: any): payload is AxiosError;
isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
}

declare const axios: AxiosStatic;
Expand Down