UNPKG

14.9 kBTypeScriptView Raw
1// TypeScript Version: 4.7
2type AxiosHeaderValue = AxiosHeaders | string | string[] | number | boolean | null;
3type RawAxiosHeaders = Record<string, AxiosHeaderValue>;
4
5type MethodsHeaders = {
6 [Key in Method as Lowercase<Key>]: AxiosHeaders;
7};
8
9interface CommonHeaders {
10 common: AxiosHeaders;
11}
12
13type AxiosHeaderMatcher = (this: AxiosHeaders, value: string, name: string, headers: RawAxiosHeaders) => boolean;
14
15type AxiosHeaderSetter = (value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher) => AxiosHeaders;
16
17type AxiosHeaderGetter = ((parser?: RegExp) => RegExpExecArray | null) |
18 ((matcher?: AxiosHeaderMatcher) => AxiosHeaderValue);
19
20type AxiosHeaderTester = (matcher?: AxiosHeaderMatcher) => boolean;
21
22export class AxiosHeaders {
23 constructor(
24 headers?: RawAxiosHeaders | AxiosHeaders
25 );
26
27 set(headerName?: string, value?: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
28 set(headers?: RawAxiosHeaders | AxiosHeaders, rewrite?: boolean): AxiosHeaders;
29
30 get(headerName: string, parser: RegExp): RegExpExecArray | null;
31 get(headerName: string, matcher?: true | AxiosHeaderMatcher): AxiosHeaderValue;
32
33 has(header: string, matcher?: true | AxiosHeaderMatcher): boolean;
34
35 delete(header: string | string[], matcher?: AxiosHeaderMatcher): boolean;
36
37 clear(): boolean;
38
39 normalize(format: boolean): AxiosHeaders;
40
41 concat(...targets: Array<AxiosHeaders | RawAxiosHeaders | string>): AxiosHeaders;
42
43 toJSON(asStrings?: boolean): RawAxiosHeaders;
44
45 static from(thing?: AxiosHeaders | RawAxiosHeaders | string): AxiosHeaders;
46
47 static accessor(header: string | string[]): AxiosHeaders;
48
49 static concat(...targets: Array<AxiosHeaders | RawAxiosHeaders | string>): AxiosHeaders;
50
51 setContentType: AxiosHeaderSetter;
52 getContentType: AxiosHeaderGetter;
53 hasContentType: AxiosHeaderTester;
54
55 setContentLength: AxiosHeaderSetter;
56 getContentLength: AxiosHeaderGetter;
57 hasContentLength: AxiosHeaderTester;
58
59 setAccept: AxiosHeaderSetter;
60 getAccept: AxiosHeaderGetter;
61 hasAccept: AxiosHeaderTester;
62
63 setUserAgent: AxiosHeaderSetter;
64 getUserAgent: AxiosHeaderGetter;
65 hasUserAgent: AxiosHeaderTester;
66
67 setContentEncoding: AxiosHeaderSetter;
68 getContentEncoding: AxiosHeaderGetter;
69 hasContentEncoding: AxiosHeaderTester;
70}
71
72export type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>;
73
74export type AxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders> & AxiosHeaders;
75
76export type RawAxiosResponseHeaders = Partial<Record<string, string> & {
77 "set-cookie"?: string[]
78}>;
79
80export type AxiosResponseHeaders = RawAxiosResponseHeaders & AxiosHeaders;
81
82export interface AxiosRequestTransformer {
83 (this: AxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any;
84}
85
86export interface AxiosResponseTransformer {
87 (this: AxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any;
88}
89
90export interface AxiosAdapter {
91 (config: AxiosRequestConfig): AxiosPromise;
92}
93
94export interface AxiosBasicCredentials {
95 username: string;
96 password: string;
97}
98
99export interface AxiosProxyConfig {
100 host: string;
101 port: number;
102 auth?: {
103 username: string;
104 password: string;
105 };
106 protocol?: string;
107}
108
109export enum HttpStatusCode {
110 Continue = 100,
111 SwitchingProtocols = 101,
112 Processing = 102,
113 EarlyHints = 103,
114 Ok = 200,
115 Created = 201,
116 Accepted = 202,
117 NonAuthoritativeInformation = 203,
118 NoContent = 204,
119 ResetContent = 205,
120 PartialContent = 206,
121 MultiStatus = 207,
122 AlreadyReported = 208,
123 ImUsed = 226,
124 MultipleChoices = 300,
125 MovedPermanently = 301,
126 Found = 302,
127 SeeOther = 303,
128 NotModified = 304,
129 UseProxy = 305,
130 Unused = 306,
131 TemporaryRedirect = 307,
132 PermanentRedirect = 308,
133 BadRequest = 400,
134 Unauthorized = 401,
135 PaymentRequired = 402,
136 Forbidden = 403,
137 NotFound = 404,
138 MethodNotAllowed = 405,
139 NotAcceptable = 406,
140 ProxyAuthenticationRequired = 407,
141 RequestTimeout = 408,
142 Conflict = 409,
143 Gone = 410,
144 LengthRequired = 411,
145 PreconditionFailed = 412,
146 PayloadTooLarge = 413,
147 UriTooLong = 414,
148 UnsupportedMediaType = 415,
149 RangeNotSatisfiable = 416,
150 ExpectationFailed = 417,
151 ImATeapot = 418,
152 MisdirectedRequest = 421,
153 UnprocessableEntity = 422,
154 Locked = 423,
155 FailedDependency = 424,
156 TooEarly = 425,
157 UpgradeRequired = 426,
158 PreconditionRequired = 428,
159 TooManyRequests = 429,
160 RequestHeaderFieldsTooLarge = 431,
161 UnavailableForLegalReasons = 451,
162 InternalServerError = 500,
163 NotImplemented = 501,
164 BadGateway = 502,
165 ServiceUnavailable = 503,
166 GatewayTimeout = 504,
167 HttpVersionNotSupported = 505,
168 VariantAlsoNegotiates = 506,
169 InsufficientStorage = 507,
170 LoopDetected = 508,
171 NotExtended = 510,
172 NetworkAuthenticationRequired = 511,
173}
174
175export type Method =
176 | 'get' | 'GET'
177 | 'delete' | 'DELETE'
178 | 'head' | 'HEAD'
179 | 'options' | 'OPTIONS'
180 | 'post' | 'POST'
181 | 'put' | 'PUT'
182 | 'patch' | 'PATCH'
183 | 'purge' | 'PURGE'
184 | 'link' | 'LINK'
185 | 'unlink' | 'UNLINK';
186
187export type ResponseType =
188 | 'arraybuffer'
189 | 'blob'
190 | 'document'
191 | 'json'
192 | 'text'
193 | 'stream';
194
195export type responseEncoding =
196 | 'ascii' | 'ASCII'
197 | 'ansi' | 'ANSI'
198 | 'binary' | 'BINARY'
199 | 'base64' | 'BASE64'
200 | 'base64url' | 'BASE64URL'
201 | 'hex' | 'HEX'
202 | 'latin1' | 'LATIN1'
203 | 'ucs-2' | 'UCS-2'
204 | 'ucs2' | 'UCS2'
205 | 'utf-8' | 'UTF-8'
206 | 'utf8' | 'UTF8'
207 | 'utf16le' | 'UTF16LE';
208
209export interface TransitionalOptions {
210 silentJSONParsing?: boolean;
211 forcedJSONParsing?: boolean;
212 clarifyTimeoutError?: boolean;
213}
214
215export interface GenericAbortSignal {
216 readonly aborted: boolean;
217 onabort?: ((...args: any) => any) | null;
218 addEventListener?: (...args: any) => any;
219 removeEventListener?: (...args: any) => any;
220}
221
222export interface FormDataVisitorHelpers {
223 defaultVisitor: SerializerVisitor;
224 convertValue: (value: any) => any;
225 isVisitable: (value: any) => boolean;
226}
227
228export interface SerializerVisitor {
229 (
230 this: GenericFormData,
231 value: any,
232 key: string | number,
233 path: null | Array<string | number>,
234 helpers: FormDataVisitorHelpers
235 ): boolean;
236}
237
238export interface SerializerOptions {
239 visitor?: SerializerVisitor;
240 dots?: boolean;
241 metaTokens?: boolean;
242 indexes?: boolean | null;
243}
244
245// tslint:disable-next-line
246export interface FormSerializerOptions extends SerializerOptions {
247}
248
249export interface ParamEncoder {
250 (value: any, defaultEncoder: (value: any) => any): any;
251}
252
253export interface CustomParamsSerializer {
254 (params: Record<string, any>, options?: ParamsSerializerOptions): string;
255}
256
257export interface ParamsSerializerOptions extends SerializerOptions {
258 encode?: ParamEncoder;
259 serialize?: CustomParamsSerializer;
260}
261
262type MaxUploadRate = number;
263
264type MaxDownloadRate = number;
265
266export interface AxiosProgressEvent {
267 loaded: number;
268 total?: number;
269 progress?: number;
270 bytes: number;
271 rate?: number;
272 estimated?: number;
273 upload?: boolean;
274 download?: boolean;
275 event?: ProgressEvent;
276}
277
278type Milliseconds = number;
279
280type AxiosAdapterName = 'xhr' | 'http' | string;
281
282type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
283
284export interface AxiosRequestConfig<D = any> {
285 url?: string;
286 method?: Method | string;
287 baseURL?: string;
288 transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
289 transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
290 headers?: RawAxiosRequestHeaders;
291 params?: any;
292 paramsSerializer?: ParamsSerializerOptions;
293 data?: D;
294 timeout?: Milliseconds;
295 timeoutErrorMessage?: string;
296 withCredentials?: boolean;
297 adapter?: AxiosAdapterConfig | AxiosAdapterConfig[];
298 auth?: AxiosBasicCredentials;
299 responseType?: ResponseType;
300 responseEncoding?: responseEncoding | string;
301 xsrfCookieName?: string;
302 xsrfHeaderName?: string;
303 onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
304 onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
305 maxContentLength?: number;
306 validateStatus?: ((status: number) => boolean) | null;
307 maxBodyLength?: number;
308 maxRedirects?: number;
309 maxRate?: number | [MaxUploadRate, MaxDownloadRate];
310 beforeRedirect?: (options: Record<string, any>, responseDetails: {headers: Record<string, string>}) => void;
311 socketPath?: string | null;
312 httpAgent?: any;
313 httpsAgent?: any;
314 proxy?: AxiosProxyConfig | false;
315 cancelToken?: CancelToken;
316 decompress?: boolean;
317 transitional?: TransitionalOptions;
318 signal?: GenericAbortSignal;
319 insecureHTTPParser?: boolean;
320 env?: {
321 FormData?: new (...args: any[]) => object;
322 };
323 formSerializer?: FormSerializerOptions;
324}
325
326export interface HeadersDefaults {
327 common: RawAxiosRequestHeaders;
328 delete: RawAxiosRequestHeaders;
329 get: RawAxiosRequestHeaders;
330 head: RawAxiosRequestHeaders;
331 post: RawAxiosRequestHeaders;
332 put: RawAxiosRequestHeaders;
333 patch: RawAxiosRequestHeaders;
334 options?: RawAxiosRequestHeaders;
335 purge?: RawAxiosRequestHeaders;
336 link?: RawAxiosRequestHeaders;
337 unlink?: RawAxiosRequestHeaders;
338}
339
340export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
341 headers: HeadersDefaults;
342}
343
344export interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
345 headers?: RawAxiosRequestHeaders | Partial<HeadersDefaults>;
346}
347
348export interface AxiosResponse<T = any, D = any> {
349 data: T;
350 status: number;
351 statusText: string;
352 headers: RawAxiosResponseHeaders | AxiosResponseHeaders;
353 config: AxiosRequestConfig<D>;
354 request?: any;
355}
356
357export class AxiosError<T = unknown, D = any> extends Error {
358 constructor(
359 message?: string,
360 code?: string,
361 config?: AxiosRequestConfig<D>,
362 request?: any,
363 response?: AxiosResponse<T, D>
364 );
365
366 config?: AxiosRequestConfig<D>;
367 code?: string;
368 request?: any;
369 response?: AxiosResponse<T, D>;
370 isAxiosError: boolean;
371 status?: number;
372 toJSON: () => object;
373 cause?: Error;
374 static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
375 static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
376 static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
377 static readonly ERR_NETWORK = "ERR_NETWORK";
378 static readonly ERR_DEPRECATED = "ERR_DEPRECATED";
379 static readonly ERR_BAD_RESPONSE = "ERR_BAD_RESPONSE";
380 static readonly ERR_BAD_REQUEST = "ERR_BAD_REQUEST";
381 static readonly ERR_NOT_SUPPORT = "ERR_NOT_SUPPORT";
382 static readonly ERR_INVALID_URL = "ERR_INVALID_URL";
383 static readonly ERR_CANCELED = "ERR_CANCELED";
384 static readonly ECONNABORTED = "ECONNABORTED";
385 static readonly ETIMEDOUT = "ETIMEDOUT";
386}
387
388export class CanceledError<T> extends AxiosError<T> {
389}
390
391export type AxiosPromise<T = any> = Promise<AxiosResponse<T>>;
392
393export interface CancelStatic {
394 new (message?: string): Cancel;
395}
396
397export interface Cancel {
398 message: string | undefined;
399}
400
401export interface Canceler {
402 (message?: string, config?: AxiosRequestConfig, request?: any): void;
403}
404
405export interface CancelTokenStatic {
406 new (executor: (cancel: Canceler) => void): CancelToken;
407 source(): CancelTokenSource;
408}
409
410export interface CancelToken {
411 promise: Promise<Cancel>;
412 reason?: Cancel;
413 throwIfRequested(): void;
414}
415
416export interface CancelTokenSource {
417 token: CancelToken;
418 cancel: Canceler;
419}
420
421export interface AxiosInterceptorOptions {
422 synchronous?: boolean;
423 runWhen?: (config: AxiosRequestConfig) => boolean;
424}
425
426export interface AxiosInterceptorManager<V> {
427 use(onFulfilled?: ((value: V) => V | Promise<V>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions): number;
428 eject(id: number): void;
429 clear(): void;
430}
431
432export class Axios {
433 constructor(config?: AxiosRequestConfig);
434 defaults: AxiosDefaults;
435 interceptors: {
436 request: AxiosInterceptorManager<AxiosRequestConfig>;
437 response: AxiosInterceptorManager<AxiosResponse>;
438 };
439 getUri(config?: AxiosRequestConfig): string;
440 request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
441 get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
442 delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
443 head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
444 options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
445 post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
446 put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
447 patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
448 postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
449 putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
450 patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
451}
452
453export interface AxiosInstance extends Axios {
454 <T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
455 <T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
456
457 defaults: Omit<AxiosDefaults, 'headers'> & {
458 headers: HeadersDefaults & {
459 [key: string]: AxiosHeaderValue
460 }
461 };
462}
463
464export interface GenericFormData {
465 append(name: string, value: any, options?: any): any;
466}
467
468export interface GenericHTMLFormElement {
469 name: string;
470 method: string;
471 submit(): void;
472}
473
474export function toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
475
476export function formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
477
478export function isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
479
480export function spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
481
482export function isCancel(value: any): value is Cancel;
483
484export function all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
485
486export interface AxiosStatic extends AxiosInstance {
487 create(config?: CreateAxiosDefaults): AxiosInstance;
488 Cancel: CancelStatic;
489 CancelToken: CancelTokenStatic;
490 Axios: typeof Axios;
491 AxiosError: typeof AxiosError;
492 readonly VERSION: string;
493 isCancel: typeof isCancel;
494 all: typeof all;
495 spread: typeof spread;
496 isAxiosError: typeof isAxiosError;
497 toFormData: typeof toFormData;
498 formToJSON: typeof formToJSON;
499 CanceledError: typeof CanceledError;
500 AxiosHeaders: typeof AxiosHeaders;
501}
502
503declare const axios: AxiosStatic;
504
505export default axios;