UNPKG

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