UNPKG

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