Skip to content

Commit d3a816d

Browse files
committed
feat(fetch): update typescript typings with fetch types
1 parent 5844b29 commit d3a816d

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

index.d.cts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ type MethodsHeaders = {
55
[Key in axios.Method as Lowercase<Key>]: AxiosHeaders;
66
};
77

8+
type FetchOptions = object;
9+
10+
type FetchURL = URL;
11+
12+
type FetchRequest = Request;
13+
14+
type FetchInput = FetchURL | FetchRequest | string;
15+
16+
type AxiosRequestFetchInputs = FetchURL | FetchRequest | string;
17+
818
interface CommonHeaders {
919
common: AxiosHeaders;
1020
}
@@ -110,17 +120,17 @@ declare class Axios {
110120
response: axios.AxiosInterceptorManager<axios.AxiosResponse>;
111121
};
112122
getUri(config?: axios.AxiosRequestConfig): string;
113-
request<T = any, R = axios.AxiosResponse<T>, D = any>(config: axios.AxiosRequestConfig<D>): Promise<R>;
114-
get<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
115-
delete<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
116-
head<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
117-
options<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
118-
post<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
119-
put<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
120-
patch<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
121-
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
122-
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
123-
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
123+
request<T = any, R = axios.AxiosResponse<T>, D = any>(url: axios.AxiosRequestConfig<D> | AxiosRequestFetchInputs): Promise<R>;
124+
get<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: axios.AxiosRequestConfig<D>): Promise<R>;
125+
delete<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: axios.AxiosRequestConfig<D>): Promise<R>;
126+
head<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: axios.AxiosRequestConfig<D>): Promise<R>;
127+
options<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: axios.AxiosRequestConfig<D>): Promise<R>;
128+
post<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
129+
put<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
130+
patch<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
131+
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
132+
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
133+
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
124134
}
125135

126136
declare enum HttpStatusCode {
@@ -338,12 +348,15 @@ declare namespace axios {
338348

339349
type Milliseconds = number;
340350

341-
type AxiosAdapterName = 'xhr' | 'http' | string;
351+
type AxiosAdapterName = 'fetch' | 'xhr' | 'http' | string;
342352

343353
type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
344354

355+
type Fetcher = (input: FetchInput, init?: FetchOptions) => Promise<Response>;
356+
345357
interface AxiosRequestConfig<D = any> {
346-
url?: string;
358+
url?: string | FetchURL;
359+
parsedUrl?: FetchURL;
347360
method?: Method | string;
348361
baseURL?: string;
349362
transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
@@ -356,6 +369,8 @@ declare namespace axios {
356369
timeoutErrorMessage?: string;
357370
withCredentials?: boolean;
358371
adapter?: AxiosAdapterConfig | AxiosAdapterConfig[];
372+
fetcher?: Fetcher;
373+
fetchOptions?: FetchOptions;
359374
auth?: AxiosBasicCredentials;
360375
responseType?: ResponseType;
361376
responseEncoding?: responseEncoding | string;
@@ -478,7 +493,7 @@ declare namespace axios {
478493
}
479494

480495
interface AxiosStatic extends AxiosInstance {
481-
create(config?: CreateAxiosDefaults): AxiosInstance;
496+
create(configOrUrlOrRequest?: CreateAxiosDefaults | AxiosRequestFetchInputs, config?: CreateAxiosDefaults): AxiosInstance;
482497
Cancel: CancelStatic;
483498
CancelToken: CancelTokenStatic;
484499
Axios: typeof Axios;

index.d.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ type MethodsHeaders = {
66
[Key in Method as Lowercase<Key>]: AxiosHeaders;
77
};
88

9+
type FetchOptions = object;
10+
11+
type FetchURL = URL;
12+
13+
type FetchRequest = Request;
14+
15+
type FetchInput = FetchURL | FetchRequest | string;
16+
917
interface CommonHeaders {
1018
common: AxiosHeaders;
1119
}
@@ -279,12 +287,15 @@ export interface AxiosProgressEvent {
279287

280288
type Milliseconds = number;
281289

282-
type AxiosAdapterName = 'xhr' | 'http' | string;
290+
type AxiosAdapterName = 'fetch' | 'xhr' | 'http' | string;
283291

284292
type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
285293

294+
type Fetcher = (input: FetchInput, init?: FetchOptions) => Promise<Response>;
295+
286296
export interface AxiosRequestConfig<D = any> {
287-
url?: string;
297+
url?: string | FetchURL;
298+
parsedUrl?: FetchURL;
288299
method?: Method | string;
289300
baseURL?: string;
290301
transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
@@ -297,6 +308,8 @@ export interface AxiosRequestConfig<D = any> {
297308
timeoutErrorMessage?: string;
298309
withCredentials?: boolean;
299310
adapter?: AxiosAdapterConfig | AxiosAdapterConfig[];
311+
fetcher?: Fetcher;
312+
fetchOptions?: FetchOptions;
300313
auth?: AxiosBasicCredentials;
301314
responseType?: ResponseType;
302315
responseEncoding?: responseEncoding | string;
@@ -439,6 +452,8 @@ export interface AxiosInterceptorManager<V> {
439452
clear(): void;
440453
}
441454

455+
type AxiosRequestFetchInputs = FetchURL | FetchRequest | string;
456+
442457
export class Axios {
443458
constructor(config?: AxiosRequestConfig);
444459
defaults: AxiosDefaults;
@@ -447,17 +462,17 @@ export class Axios {
447462
response: AxiosInterceptorManager<AxiosResponse>;
448463
};
449464
getUri(config?: AxiosRequestConfig): string;
450-
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
451-
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
452-
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
453-
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
454-
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
455-
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
456-
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
457-
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
458-
postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
459-
putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
460-
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
465+
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D> | AxiosRequestFetchInputs): Promise<R>;
466+
get<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: AxiosRequestConfig<D>): Promise<R>;
467+
delete<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: AxiosRequestConfig<D>): Promise<R>;
468+
head<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: AxiosRequestConfig<D>): Promise<R>;
469+
options<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, config?: AxiosRequestConfig<D>): Promise<R>;
470+
post<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
471+
put<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
472+
patch<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
473+
postForm<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
474+
putForm<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
475+
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: AxiosRequestFetchInputs, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
461476
}
462477

463478
export interface AxiosInstance extends Axios {
@@ -494,7 +509,7 @@ export function isCancel(value: any): value is Cancel;
494509
export function all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
495510

496511
export interface AxiosStatic extends AxiosInstance {
497-
create(config?: CreateAxiosDefaults): AxiosInstance;
512+
create(configOrUrlOrRequest?: CreateAxiosDefaults | AxiosRequestFetchInputs, config?: CreateAxiosDefaults): AxiosInstance;
498513
Cancel: CancelStatic;
499514
CancelToken: CancelTokenStatic;
500515
Axios: typeof Axios;

0 commit comments

Comments
 (0)