UNPKG

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