🎨 优化
parent
7b20ad9661
commit
796b80370a
32
src/axios.ts
32
src/axios.ts
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-15 12:45:18
|
* @Date: 2020-04-15 12:45:18
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 16:07:56
|
* @LastEditTime: 2020-04-23 10:44:18
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, Data, AxiosResponse, AxiosBaseInstance, AxiosInstance } from './types';
|
import { AxiosRequestConfig, Data, AxiosResponse, AxiosBaseInstance, AxiosInstance } from './types';
|
||||||
import Axios from './core/Axios';
|
import Axios from './core/Axios';
|
||||||
|
@ -21,16 +21,6 @@ function createInstance(config: AxiosRequestConfig): AxiosInstance {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支持重载的 axios 函数
|
* 支持重载的 axios 函数
|
||||||
*
|
|
||||||
* @调用方式一
|
|
||||||
*
|
|
||||||
* @param url 调用方式一: 请求配置
|
|
||||||
* @param config 调用方式一: 空
|
|
||||||
*
|
|
||||||
* @调用方式二
|
|
||||||
*
|
|
||||||
* @param url 调用方式二: 请求地址
|
|
||||||
* @param config 调用方式二: 额外配置
|
|
||||||
*/
|
*/
|
||||||
function axios<T extends Data>(
|
function axios<T extends Data>(
|
||||||
url: AxiosRequestConfig | string,
|
url: AxiosRequestConfig | string,
|
||||||
|
@ -59,7 +49,7 @@ function createInstance(config: AxiosRequestConfig): AxiosInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Axios 实例增强
|
* Axios 实例拓展
|
||||||
*/
|
*/
|
||||||
const axios = createInstance(defaults);
|
const axios = createInstance(defaults);
|
||||||
|
|
||||||
|
@ -74,23 +64,7 @@ axios.Axios = Axios;
|
||||||
// 添加 CancelToken 类
|
// 添加 CancelToken 类
|
||||||
axios.CancelToken = CancelToken;
|
axios.CancelToken = CancelToken;
|
||||||
|
|
||||||
// 添加 判断取消请求 方法
|
// 添加 检查错误是否来自取消请求 方法
|
||||||
axios.isCancel = isCancel;
|
axios.isCancel = isCancel;
|
||||||
|
|
||||||
export default axios;
|
export default axios;
|
||||||
|
|
||||||
// axios
|
|
||||||
// .extractData<{}>(
|
|
||||||
// axios.get(
|
|
||||||
// '/test',
|
|
||||||
// {
|
|
||||||
// id: 1,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// headers: { aaa: 'aaa' },
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
// .then((data) => {
|
|
||||||
// console.log(data);
|
|
||||||
// });
|
|
||||||
|
|
|
@ -2,18 +2,18 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 21:14:53
|
* @Date: 2020-04-13 21:14:53
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-14 13:41:45
|
* @LastEditTime: 2020-04-22 17:38:43
|
||||||
*/
|
*/
|
||||||
import { Cancel } from '../types';
|
import { Cancel } from '../types';
|
||||||
|
|
||||||
export default class CancelStatic implements Cancel {
|
export default class CancelStatic implements Cancel {
|
||||||
message?: string;
|
public message?: string;
|
||||||
|
|
||||||
constructor(message?: string) {
|
constructor(message?: string) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
public toString() {
|
||||||
const message = this.message ? `: ${this.message}` : '';
|
const message = this.message ? `: ${this.message}` : '';
|
||||||
|
|
||||||
return `Cancel${message}`;
|
return `Cancel${message}`;
|
||||||
|
|
|
@ -2,15 +2,18 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 20:00:08
|
* @Date: 2020-04-13 20:00:08
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-15 17:29:07
|
* @LastEditTime: 2020-04-22 17:39:44
|
||||||
*/
|
*/
|
||||||
import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types';
|
import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types';
|
||||||
import Cancel from './Cancel';
|
import Cancel from './Cancel';
|
||||||
|
|
||||||
export default class CancelTokenStatic implements CancelToken {
|
export default class CancelTokenStatic implements CancelToken {
|
||||||
reason?: Cancel;
|
/**
|
||||||
|
* 取消请求
|
||||||
|
*/
|
||||||
|
private _reason?: Cancel;
|
||||||
|
|
||||||
listener: Promise<Cancel>;
|
public listener: Promise<Cancel>;
|
||||||
|
|
||||||
constructor(executor: CancelExecutor) {
|
constructor(executor: CancelExecutor) {
|
||||||
let action!: CancelAction;
|
let action!: CancelAction;
|
||||||
|
@ -18,22 +21,22 @@ export default class CancelTokenStatic implements CancelToken {
|
||||||
this.listener = new Promise<Cancel>((resolve) => {
|
this.listener = new Promise<Cancel>((resolve) => {
|
||||||
action = (message) => {
|
action = (message) => {
|
||||||
// 防止重复取消
|
// 防止重复取消
|
||||||
if (this.reason) {
|
if (this._reason) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.reason = new Cancel(message);
|
this._reason = new Cancel(message);
|
||||||
|
|
||||||
resolve(this.reason);
|
resolve(this._reason);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
executor(action);
|
executor(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
throwIfRequested(): void {
|
public throwIfRequested(): void {
|
||||||
if (this.reason) {
|
if (this._reason) {
|
||||||
throw this.reason;
|
throw this._reason;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 18:00:27
|
* @Date: 2020-04-13 18:00:27
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 15:59:22
|
* @LastEditTime: 2020-04-23 10:12:56
|
||||||
*/
|
*/
|
||||||
import { Method, Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types';
|
import { Method, Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types';
|
||||||
import buildURL from '../helpers/buildURL';
|
import buildURL from '../helpers/buildURL';
|
||||||
|
@ -21,6 +21,9 @@ export default class AxiosStatic implements Axios {
|
||||||
*/
|
*/
|
||||||
public interceptors: Interceptors;
|
public interceptors: Interceptors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param config 自定义默认配置
|
||||||
|
*/
|
||||||
constructor(config: AxiosRequestConfig = {}) {
|
constructor(config: AxiosRequestConfig = {}) {
|
||||||
this.defaults = config;
|
this.defaults = config;
|
||||||
this.interceptors = {
|
this.interceptors = {
|
||||||
|
@ -46,7 +49,9 @@ export default class AxiosStatic implements Axios {
|
||||||
* @param config Axios 请求配置
|
* @param config Axios 请求配置
|
||||||
*/
|
*/
|
||||||
public request<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public request<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||||
let promiseRequest = Promise.resolve(mergeConfig(this.defaults, config));
|
const requestConfig = mergeConfig(this.defaults, config);
|
||||||
|
|
||||||
|
let promiseRequest = Promise.resolve(requestConfig);
|
||||||
|
|
||||||
// 执行请求拦截器
|
// 执行请求拦截器
|
||||||
this.interceptors.request.forEach(function executor({ resolved, rejected }) {
|
this.interceptors.request.forEach(function executor({ resolved, rejected }) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-15 17:50:50
|
* @Date: 2020-04-15 17:50:50
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-19 13:35:47
|
* @LastEditTime: 2020-04-23 09:16:23
|
||||||
*/
|
*/
|
||||||
import {
|
import {
|
||||||
InterceptorResolved,
|
InterceptorResolved,
|
||||||
|
@ -19,16 +19,16 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
|
||||||
/**
|
/**
|
||||||
* 生成拦截器 id
|
* 生成拦截器 id
|
||||||
*/
|
*/
|
||||||
private id: number;
|
private _id: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拦截器集合
|
* 拦截器集合
|
||||||
*/
|
*/
|
||||||
private interceptors: Record<number, Interceptor<T>>;
|
private _interceptors: Record<number, Interceptor<T>>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.id = 0;
|
this._id = 0;
|
||||||
this.interceptors = {};
|
this._interceptors = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,13 +37,13 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
|
||||||
* @param resolved 成功的回调函数
|
* @param resolved 成功的回调函数
|
||||||
* @param rejected 失败的回调函数
|
* @param rejected 失败的回调函数
|
||||||
*/
|
*/
|
||||||
public use(resolved: InterceptorResolved<T>, rejected: InterceptorRejected = (err: any) => Promise.reject(err)) {
|
public use(resolved: InterceptorResolved<T>, rejected?: InterceptorRejected) {
|
||||||
this.interceptors[++this.id] = {
|
this._interceptors[++this._id] = {
|
||||||
resolved,
|
resolved,
|
||||||
rejected,
|
rejected,
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.id;
|
return this._id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,7 +52,7 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
|
||||||
* @param id 拦截器 id
|
* @param id 拦截器 id
|
||||||
*/
|
*/
|
||||||
public eject(id: number): void {
|
public eject(id: number): void {
|
||||||
delete this.interceptors[id];
|
delete this._interceptors[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,7 +62,7 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
|
||||||
* @param reverse 是否倒序遍历
|
* @param reverse 是否倒序遍历
|
||||||
*/
|
*/
|
||||||
public forEach(executor: InterceptorExecutor<T>, reverse?: 'reverse'): void {
|
public forEach(executor: InterceptorExecutor<T>, reverse?: 'reverse'): void {
|
||||||
let interceptors: Interceptor<T>[] = [...Object.values(this.interceptors)];
|
let interceptors: Interceptor<T>[] = Object.values(this._interceptors);
|
||||||
|
|
||||||
if (reverse === 'reverse') {
|
if (reverse === 'reverse') {
|
||||||
interceptors = interceptors.reverse();
|
interceptors = interceptors.reverse();
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-14 22:23:39
|
* @Date: 2020-04-14 22:23:39
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-20 13:23:04
|
* @LastEditTime: 2020-04-23 10:34:18
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, RequestConfig, AxiosResponse } from '../types';
|
import { AxiosRequestConfig, RequestConfig, AxiosResponse } from '../types';
|
||||||
|
|
||||||
|
@ -30,6 +30,12 @@ class AxiosError extends Error {
|
||||||
*/
|
*/
|
||||||
public response?: AxiosResponse;
|
public response?: AxiosResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param message 错误信息
|
||||||
|
* @param config Axios 请求配置
|
||||||
|
* @param request 通用请求配置
|
||||||
|
* @param response Axios 响应体
|
||||||
|
*/
|
||||||
constructor(message: string, config: AxiosRequestConfig, request: RequestConfig, response?: AxiosResponse) {
|
constructor(message: string, config: AxiosRequestConfig, request: RequestConfig, response?: AxiosResponse) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 18:01:16
|
* @Date: 2020-04-13 18:01:16
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-21 11:44:53
|
* @LastEditTime: 2020-04-23 09:31:00
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
||||||
import isCancel from '../cancel/isCancel';
|
import isCancel from '../cancel/isCancel';
|
||||||
|
@ -50,7 +50,7 @@ export default function dispatchRequest(config: AxiosRequestConfig): Promise<Axi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return typeof config.errorHandler === 'function' ? config.errorHandler(reason) : Promise.reject(reason);
|
return config.errorHandler !== undefined ? config.errorHandler(reason) : Promise.reject(reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
return request(config).then(onResolved, onRejected);
|
return request(config).then(onResolved, onRejected);
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
/*
|
|
||||||
* @Author: early-autumn
|
|
||||||
* @Date: 2020-04-22 15:59:25
|
|
||||||
* @LastEditors: early-autumn
|
|
||||||
* @LastEditTime: 2020-04-22 15:59:42
|
|
||||||
*/
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-15 22:09:38
|
* @Date: 2020-04-15 22:09:38
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-21 19:21:38
|
* @LastEditTime: 2020-04-23 09:11:41
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig } from './types';
|
import { AxiosRequestConfig } from './types';
|
||||||
import adaptive from './adaptive';
|
import adaptive from './adaptive';
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-14 23:22:52
|
* @Date: 2020-04-14 23:22:52
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 16:42:48
|
* @LastEditTime: 2020-04-23 10:31:27
|
||||||
*/
|
*/
|
||||||
import axios from './axios';
|
import axios from './axios';
|
||||||
|
|
||||||
|
|
54
src/types.ts
54
src/types.ts
|
@ -2,15 +2,13 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 15:23:53
|
* @Date: 2020-04-13 15:23:53
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 16:08:07
|
* @LastEditTime: 2020-04-23 10:43:58
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import axios from './axios';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 任意值对象
|
* 任意值对象
|
||||||
*/
|
*/
|
||||||
export declare type AnyObject = Record<string, any>;
|
export declare type AnyObject<T extends any = any> = Record<string, T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求方法
|
* 请求方法
|
||||||
|
@ -44,52 +42,52 @@ export declare interface Headers {
|
||||||
/**
|
/**
|
||||||
* 通用配置
|
* 通用配置
|
||||||
*/
|
*/
|
||||||
common?: Record<string, string>;
|
common?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* options 请求专用
|
* options 请求专用
|
||||||
*/
|
*/
|
||||||
options?: Record<string, string>;
|
options?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get 请求专用
|
* get 请求专用
|
||||||
*/
|
*/
|
||||||
get?: Record<string, string>;
|
get?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* head 请求专用
|
* head 请求专用
|
||||||
*/
|
*/
|
||||||
head?: Record<string, string>;
|
head?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* post 请求专用
|
* post 请求专用
|
||||||
*/
|
*/
|
||||||
post?: Record<string, string>;
|
post?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* put 请求专用
|
* put 请求专用
|
||||||
*/
|
*/
|
||||||
put?: Record<string, string>;
|
put?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* delete 请求专用
|
* delete 请求专用
|
||||||
*/
|
*/
|
||||||
delete?: Record<string, string>;
|
delete?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* trace 请求专用
|
* trace 请求专用
|
||||||
*/
|
*/
|
||||||
trace?: Record<string, string>;
|
trace?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* connect 请求专用
|
* connect 请求专用
|
||||||
*/
|
*/
|
||||||
connect?: Record<string, string>;
|
connect?: AnyObject<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义配置
|
* 自定义配置
|
||||||
*/
|
*/
|
||||||
[x: string]: Record<string, string> | string | undefined;
|
[x: string]: AnyObject<string> | string | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -406,7 +404,7 @@ export declare interface InterceptorResolved<T = any> {
|
||||||
* 拦截器失败的回调函数
|
* 拦截器失败的回调函数
|
||||||
*/
|
*/
|
||||||
export declare interface InterceptorRejected {
|
export declare interface InterceptorRejected {
|
||||||
(error: any): Promise<any> | any;
|
(error: any): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -417,10 +415,11 @@ export declare interface Interceptor<T = any> {
|
||||||
* 拦截器成功的回调函数
|
* 拦截器成功的回调函数
|
||||||
*/
|
*/
|
||||||
resolved: InterceptorResolved<T>;
|
resolved: InterceptorResolved<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拦截器失败的回调函数
|
* 拦截器失败的回调函数
|
||||||
*/
|
*/
|
||||||
rejected: InterceptorRejected;
|
rejected?: InterceptorRejected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -575,7 +574,7 @@ export declare interface Axios {
|
||||||
* Axios 类接口
|
* Axios 类接口
|
||||||
*/
|
*/
|
||||||
export declare interface AxiosConstructor {
|
export declare interface AxiosConstructor {
|
||||||
new (config: AxiosRequestConfig): Axios;
|
new (config?: AxiosRequestConfig): Axios;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -643,11 +642,6 @@ export declare interface CancelExecutor {
|
||||||
* 取消令牌
|
* 取消令牌
|
||||||
*/
|
*/
|
||||||
export declare interface CancelToken {
|
export declare interface CancelToken {
|
||||||
/**
|
|
||||||
* 取消请求
|
|
||||||
*/
|
|
||||||
reason?: Cancel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消时被触发
|
* 取消时被触发
|
||||||
*/
|
*/
|
||||||
|
@ -695,12 +689,14 @@ export declare interface CancelTokenConstructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Axios 实例基础增强
|
* Axios 实例基础拓展
|
||||||
*
|
*
|
||||||
* * 支持两种函数调用方式
|
* * 支持两种函数调用方式
|
||||||
*/
|
*/
|
||||||
export declare interface AxiosBaseInstance extends Axios {
|
export declare interface AxiosBaseInstance extends Axios {
|
||||||
/**
|
/**
|
||||||
|
* 发送 HTTP 请求
|
||||||
|
*
|
||||||
* 调用方式一
|
* 调用方式一
|
||||||
*
|
*
|
||||||
* @param config 请求配置
|
* @param config 请求配置
|
||||||
|
@ -708,6 +704,8 @@ export declare interface AxiosBaseInstance extends Axios {
|
||||||
<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 发送 HTTP 请求
|
||||||
|
*
|
||||||
* 调用方式二
|
* 调用方式二
|
||||||
*
|
*
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
|
@ -717,7 +715,7 @@ export declare interface AxiosBaseInstance extends Axios {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Axios 实例增强
|
* Axios 实例拓展
|
||||||
*
|
*
|
||||||
* * 支持两种函数调用方式
|
* * 支持两种函数调用方式
|
||||||
*
|
*
|
||||||
|
@ -725,9 +723,9 @@ export declare interface AxiosBaseInstance extends Axios {
|
||||||
*/
|
*/
|
||||||
export declare interface AxiosInstance extends AxiosBaseInstance {
|
export declare interface AxiosInstance extends AxiosBaseInstance {
|
||||||
/**
|
/**
|
||||||
* 创建 Axios 实例基础增强
|
* 创建 Axios 实例基础拓展
|
||||||
*
|
*
|
||||||
* @param config 全局配置
|
* @param config 自定义默认配置
|
||||||
*/
|
*/
|
||||||
create(config?: AxiosRequestConfig): AxiosBaseInstance;
|
create(config?: AxiosRequestConfig): AxiosBaseInstance;
|
||||||
|
|
||||||
|
@ -742,11 +740,9 @@ export declare interface AxiosInstance extends AxiosBaseInstance {
|
||||||
CancelToken: CancelTokenConstructor;
|
CancelToken: CancelTokenConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查一个错误是不是取消错误
|
* 检查错误是否来自取消请求
|
||||||
*
|
*
|
||||||
* @param value 判断的值
|
* @param value 判断的值
|
||||||
*/
|
*/
|
||||||
isCancel: (value: any) => boolean;
|
isCancel: (value: any) => boolean;
|
||||||
|
|
||||||
// extractData: <T extends Data>(response: Promise<AxiosResponse<T>>) => Promise<T>;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-20 15:40:44
|
* @Date: 2020-04-20 15:40:44
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-20 22:05:48
|
* @LastEditTime: 2020-04-23 09:29:17
|
||||||
*/
|
*/
|
||||||
import InterceptorManager from '../../src/core/InterceptorManager';
|
import InterceptorManager from '../../src/core/InterceptorManager';
|
||||||
|
|
||||||
|
@ -54,8 +54,14 @@ describe('测试 src/core/InterceptorManager.ts', () => {
|
||||||
it('异常', () => {
|
it('异常', () => {
|
||||||
const interceptor = new InterceptorManager();
|
const interceptor = new InterceptorManager();
|
||||||
|
|
||||||
interceptor.use(() => undefined);
|
interceptor.use(
|
||||||
|
() => undefined,
|
||||||
|
(error: any) => {
|
||||||
|
expect(error).toBe('error');
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
interceptor.forEach(({ rejected }) => rejected('error'));
|
interceptor.forEach(({ rejected }) => rejected?.('error'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue