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 httpAgent?: any;
335 httpsAgent?: any;
336 proxy?: AxiosProxyConfig | false;
337 cancelToken?: CancelToken;
338 decompress?: boolean;
339 transitional?: TransitionalOptions;
340 signal?: GenericAbortSignal;
341 insecureHTTPParser?: boolean;
342 env?: {
343 FormData?: new (...args: any[]) => object;
344 };
345 formSerializer?: FormSerializerOptions;
346}
347
348// Alias
349export type RawAxiosRequestConfig<D = any> = AxiosRequestConfig<D>;
350
351export interface InternalAxiosRequestConfig<D = any> extends AxiosRequestConfig<D> {
352 headers: AxiosRequestHeaders;
353}
354
355export interface HeadersDefaults {
356 common: RawAxiosRequestHeaders;
357 delete: RawAxiosRequestHeaders;
358 get: RawAxiosRequestHeaders;
359 head: RawAxiosRequestHeaders;
360 post: RawAxiosRequestHeaders;
361 put: RawAxiosRequestHeaders;
362 patch: RawAxiosRequestHeaders;
363 options?: RawAxiosRequestHeaders;
364 purge?: RawAxiosRequestHeaders;
365 link?: RawAxiosRequestHeaders;
366 unlink?: RawAxiosRequestHeaders;
367}
368
369export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
370 headers: HeadersDefaults;
371}
372
373export interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
374 headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
375}
376
377export interface AxiosResponse<T = any, D = any> {
378 data: T;
379 status: number;
380 statusText: string;
381 headers: RawAxiosResponseHeaders | AxiosResponseHeaders;
382 config: InternalAxiosRequestConfig<D>;
383 request?: any;
384}
385
386export class AxiosError<T = unknown, D = any> extends Error {
387 constructor(
388 message?: string,
389 code?: string,
390 config?: InternalAxiosRequestConfig<D>,
391 request?: any,
392 response?: AxiosResponse<T, D>
393 );
394
395 config?: InternalAxiosRequestConfig<D>;
396 code?: string;
397 request?: any;
398 response?: AxiosResponse<T, D>;
399 isAxiosError: boolean;
400 status?: number;
401 toJSON: () => object;
402 cause?: Error;
403 static from<T = unknown, D = any>(
404 error: Error | unknown,
405 code?: string,
406 config?: InternalAxiosRequestConfig<D>,
407 request?: any,
408 response?: AxiosResponse<T, D>,
409 customProps?: object,
410): AxiosError<T, D>;
411 static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
412 static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
413 static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
414 static readonly ERR_NETWORK = "ERR_NETWORK";
415 static readonly ERR_DEPRECATED = "ERR_DEPRECATED";
416 static readonly ERR_BAD_RESPONSE = "ERR_BAD_RESPONSE";
417 static readonly ERR_BAD_REQUEST = "ERR_BAD_REQUEST";
418 static readonly ERR_NOT_SUPPORT = "ERR_NOT_SUPPORT";
419 static readonly ERR_INVALID_URL = "ERR_INVALID_URL";
420 static readonly ERR_CANCELED = "ERR_CANCELED";
421 static readonly ECONNABORTED = "ECONNABORTED";
422 static readonly ETIMEDOUT = "ETIMEDOUT";
423}
424
425export class CanceledError<T> extends AxiosError<T> {
426}
427
428export type AxiosPromise<T = any> = Promise<AxiosResponse<T>>;
429
430export interface CancelStatic {
431 new (message?: string): Cancel;
432}
433
434export interface Cancel {
435 message: string | undefined;
436}
437
438export interface Canceler {
439 (message?: string, config?: AxiosRequestConfig, request?: any): void;
440}
441
442export interface CancelTokenStatic {
443 new (executor: (cancel: Canceler) => void): CancelToken;
444 source(): CancelTokenSource;
445}
446
447export interface CancelToken {
448 promise: Promise<Cancel>;
449 reason?: Cancel;
450 throwIfRequested(): void;
451}
452
453export interface CancelTokenSource {
454 token: CancelToken;
455 cancel: Canceler;
456}
457
458export interface AxiosInterceptorOptions {
459 synchronous?: boolean;
460 runWhen?: (config: InternalAxiosRequestConfig) => boolean;
461}
462
463export interface AxiosInterceptorManager<V> {
464 use(onFulfilled?: ((value: V) => V | Promise<V>) | null, onRejected?: ((error: any) => any) | null, options?: AxiosInterceptorOptions): number;
465 eject(id: number): void;
466 clear(): void;
467}
468
469export class Axios {
470 constructor(config?: AxiosRequestConfig);
471 defaults: AxiosDefaults;
472 interceptors: {
473 request: AxiosInterceptorManager<InternalAxiosRequestConfig>;
474 response: AxiosInterceptorManager<AxiosResponse>;
475 };
476 getUri(config?: AxiosRequestConfig): string;
477 request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
478 get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
479 delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
480 head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
481 options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
482 post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
483 put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
484 patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
485 postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
486 putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
487 patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
488}
489
490export interface AxiosInstance extends Axios {
491 <T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
492 <T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
493
494 defaults: Omit<AxiosDefaults, 'headers'> & {
495 headers: HeadersDefaults & {
496 [key: string]: AxiosHeaderValue
497 }
498 };
499}
500
501export interface GenericFormData {
502 append(name: string, value: any, options?: any): any;
503}
504
505export interface GenericHTMLFormElement {
506 name: string;
507 method: string;
508 submit(): void;
509}
510
511export function toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
512
513export function formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
514
515export function isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
516
517export function spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
518
519export function isCancel(value: any): value is Cancel;
520
521export function all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
522
523export interface AxiosStatic extends AxiosInstance {
524 create(config?: CreateAxiosDefaults): AxiosInstance;
525 Cancel: CancelStatic;
526 CancelToken: CancelTokenStatic;
527 Axios: typeof Axios;
528 AxiosError: typeof AxiosError;
529 HttpStatusCode: typeof HttpStatusCode;
530 readonly VERSION: string;
531 isCancel: typeof isCancel;
532 all: typeof all;
533 spread: typeof spread;
534 isAxiosError: typeof isAxiosError;
535 toFormData: typeof toFormData;
536 formToJSON: typeof formToJSON;
537 CanceledError: typeof CanceledError;
538 AxiosHeaders: typeof AxiosHeaders;
539}
540
541declare const axios: AxiosStatic;
542
543export default axios;