UNPKG

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