UNPKG

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