Skip to content

Commit

Permalink
Fixing isAxiosError typings to take generics
Browse files Browse the repository at this point in the history
  • Loading branch information
latipun7 committed Oct 30, 2021
1 parent c5fe05b commit 33c0d1d
Showing 1 changed file with 70 additions and 31 deletions.
101 changes: 70 additions & 31 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export type AxiosRequestHeaders = Record<string, string>;

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

export interface AxiosRequestTransformer {
Expand Down Expand Up @@ -34,24 +34,34 @@ 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'
| 'blob'
| 'document'
| 'json'
| 'text'
| 'stream';
| "arraybuffer"
| "blob"
| "document"
| "json"
| "text"
| "stream";

export interface TransitionalOptions {
silentJSONParsing?: boolean;
Expand Down Expand Up @@ -108,11 +118,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 @@ -130,8 +141,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 @@ -162,7 +172,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 @@ -174,14 +187,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 @@ -198,7 +237,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>(payload: any): payload is AxiosError<T>;
}

declare const axios: AxiosStatic;
Expand Down

0 comments on commit 33c0d1d

Please sign in to comment.