import * as _axios from 'axios'; import { Method, AxiosRequestConfig, AxiosResponse as AxiosResponse$1, InternalAxiosRequestConfig, AxiosError, AxiosInstance, CreateAxiosDefaults } from 'axios'; export * from 'axios'; interface CacheOptions { /** * 是否允许使用缓存。默认:`false` */ enable?: boolean; /** * 缓存存活时间(ms),默认:`10 * 60 *1000`(10分钟)。 * * @see CacheSlot.defaultMaxAge */ maxAge?: number; /** * 允许缓存的请求方法,默认:`['get']` * @see CacheSlot.defaultAllowedMethods */ allowedMethods?: `${Lowercase}`[]; /** * 作为缓存的依赖,你可以过滤掉无关的属性,容易命中缓存。 * 允许直接更改formatConfig对象,不会影响请求结果。 */ format?(formatConfig: CacheFormatConfig): object | string; /** * 允许使用缓存的请求,执行该方法再次确认。 */ validate?(config: AxiosRequestConfig): boolean; } type CacheMap = Partial<{ [K: string]: { time: number; response: AxiosResponse$1; }; }>; type FormatKeys$1 = (typeof CacheSlot)['formatKeys'][number]; type CacheFormatConfig = Required>; declare class CacheSlot { protected readonly options: boolean | CacheOptions; static defaultMaxAge: number; static formatKeys: readonly ["baseURL", "url", "method", "params", "data", "headers"]; static defaultAllowedMethods: NonNullable; protected cacheMap: CacheMap; constructor(options?: boolean | CacheOptions); hit(config: InternalAxiosRequestConfig, newCache: (config: InternalAxiosRequestConfig) => Promise): Promise; clear(filter?: (config: CacheFormatConfig) => boolean): void; protected static getFormatConfig(config: AxiosRequestConfig): CacheFormatConfig; } interface FocaAxiosPromise extends Promise { /** * 返回axios原始的response格式 * @see AxiosResponse */ toRaw: () => Promise>; } type RetryOptions = { /** * 失败的请求是否允许重试,默认:`true` */ enable?: boolean; /** * 最大重试次数,默认:`3`次 * @see RetrySlot.MAX_ATTEMPT */ maxAttempt?: number; /** * 每次重试间隔。如果碰到报文`Retry-After`,则需到达报文指定的时间后才能重试。默认:`500`ms * @see setTimeout() * @see RetrySlot.GAP */ gap?: number; /** * 允许重试的请求方法,默认:`['get', 'head', 'put', 'patch', 'delete']` * @see RetrySlot.ALLOWED_METHODS */ allowedMethods?: `${Lowercase}`[]; /** * 允许重试的http状态码区间,默认:`[[100, 199], 429, [500, 599]]` * @see RetrySlot.ALLOWED_HTTP_STATUS */ allowedHttpStatus?: (number | [number, number])[]; /** * 允许使用重试的请求,执行该方法再次确认 */ validate?(config: AxiosRequestConfig): boolean; } & ({ resolveUnauthorized?: undefined; onAuthorized?: undefined; } | { /** * 重试前更新令牌 * * 当检测到401 unauthorized状态码,如果该函数返回true, * 则忽略 **allowedMethods** 和 **allowedHttpStatus** 的判断并继续重试。 * * 注意:函数内的请求如果使用到GET等请求,建议在单个请求种手动关闭retry功能,以免出现死循环 * * ```typescript * axios.create({ * retry: { * async resolveUnauthorized() { * const result = await axios.post('/refresh/token', {...}); * // 存储令牌,即将在 onAuthorized 中用到 * saveToken(result.token); * // 代表已经解决授权问题,允许继续重试 * return true; * } * } * }); * ``` */ resolveUnauthorized: (err: AxiosError) => Promise; /** * 修改即将重试的config,比如替换令牌 * * ```typescript * axios.create({ * retry: { * onAuthorized(config) { * const token = getToken(); * config.headers.Authorization = `Bearer ${token}`; * } * } * }) * ``` */ onAuthorized: (config: InternalAxiosRequestConfig) => void | Promise; }); interface ThrottleOptions { /** * 是否允许共享,默认:`true` */ enable?: boolean; /** * 允许共享的方法,默认:`['get', 'head', 'put', 'patch', 'delete']` * @see ThrottleSlot.defaultAllowedMethods */ allowedMethods?: `${Lowercase}`[]; /** * 作为共享的依赖,你可以过滤掉无关的属性,容易命中共享中的请求。 * * 允许直接更改formatConfig对象,不会影响请求结果。 */ format?: (formatConfig: ThrottleFormatConfig) => object | string; /** * 对于过滤后初步允许共享的请求,执行该方法再次确认。 */ validate?(config: AxiosRequestConfig): boolean; } type FormatKeys = (typeof ThrottleSlot)['formatKeys'][number]; type ThrottleFormatConfig = Required>; declare class ThrottleSlot { protected readonly options?: (boolean | ThrottleOptions) | undefined; static formatKeys: readonly ["baseURL", "url", "method", "params", "data", "headers", "timeout", "maxContentLength", "maxBodyLength", "xsrfCookieName", "xsrfHeaderName"]; static defaultAllowedMethods: NonNullable; protected readonly threads: Partial<{ [K: string]: Promise; }>; constructor(options?: (boolean | ThrottleOptions) | undefined); hit(config: InternalAxiosRequestConfig, newThread: (config: InternalAxiosRequestConfig) => Promise): Promise; protected static getFormatConfig(config: AxiosRequestConfig): ThrottleFormatConfig; } declare module 'axios' { interface InternalAxiosRequestConfig { timestamp: number; } interface AxiosStatic { _: AxiosStatic['create']; } interface AxiosRequestConfig { /** * 相同请求共享。 */ throttle?: boolean | ThrottleOptions; /** * 失败后的重试。 */ retry?: boolean | RetryOptions; /** * 缓存响应成功的数据。建议默认关闭,仅在不需要增删改的请求上做缓存。 */ cache?: boolean | CacheOptions; /** * 一些接口偏向于把错误码放到响应数据中,httpStatus总是设置成200。 * 你需要实现该方法以返回正确的status,这样axios才会判定为是异常的请求。 * * 另一方面,只有异常的请求才会触发重试,所以需要重试的非标准接口必须实现该方法。 * * @see Axios.defaults.validateStatus */ getHttpStatus?: (response: AxiosResponse) => number; } interface AxiosInstance extends Axios { request: (config?: AxiosRequestConfig) => FocaAxiosPromise; get: (url: string, config?: AxiosRequestConfig) => FocaAxiosPromise; delete: (url: string, config?: AxiosRequestConfig) => FocaAxiosPromise; head: (url: string, config?: AxiosRequestConfig) => FocaAxiosPromise; options: (url: string, config?: AxiosRequestConfig) => FocaAxiosPromise; post: (url: string, data?: D, config?: AxiosRequestConfig) => FocaAxiosPromise; put: (url: string, data?: D, config?: AxiosRequestConfig) => FocaAxiosPromise; patch: (url: string, data?: D, config?: AxiosRequestConfig) => FocaAxiosPromise; /** * 清除全部请求缓存。 * 如果只需要清除特定的缓存,可以传入过滤函数 */ clearCache: (filter?: (config: CacheFormatConfig) => boolean) => void; } interface AxiosAdapter { __prev_adapter__: any; } } /** * axios增强适配器 */ declare const enhance: (instance: T, options?: CreateAxiosDefaults) => T; declare const axios: _axios.AxiosStatic; export { axios, axios as default, enhance };