UNPKG

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