🎨 优化

pull/1/head
early-autumn 2020-04-23 10:46:45 +08:00
parent 7b20ad9661
commit 796b80370a
12 changed files with 80 additions and 96 deletions

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-15 12:45:18
* @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 Axios from './core/Axios';
@ -21,16 +21,6 @@ function createInstance(config: AxiosRequestConfig): AxiosInstance {
/**
* axios
*
* @
*
* @param url :
* @param config :
*
* @
*
* @param url :
* @param config :
*/
function axios<T extends Data>(
url: AxiosRequestConfig | string,
@ -59,7 +49,7 @@ function createInstance(config: AxiosRequestConfig): AxiosInstance {
}
/**
* Axios
* Axios
*/
const axios = createInstance(defaults);
@ -74,23 +64,7 @@ axios.Axios = Axios;
// 添加 CancelToken 类
axios.CancelToken = CancelToken;
// 添加 判断取消请求 方法
// 添加 检查错误是否来自取消请求 方法
axios.isCancel = isCancel;
export default axios;
// axios
// .extractData<{}>(
// axios.get(
// '/test',
// {
// id: 1,
// },
// {
// headers: { aaa: 'aaa' },
// }
// )
// )
// .then((data) => {
// console.log(data);
// });

View File

@ -2,18 +2,18 @@
* @Author: early-autumn
* @Date: 2020-04-13 21:14:53
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 13:41:45
* @LastEditTime: 2020-04-22 17:38:43
*/
import { Cancel } from '../types';
export default class CancelStatic implements Cancel {
message?: string;
public message?: string;
constructor(message?: string) {
this.message = message;
}
toString() {
public toString() {
const message = this.message ? `: ${this.message}` : '';
return `Cancel${message}`;

View File

@ -2,15 +2,18 @@
* @Author: early-autumn
* @Date: 2020-04-13 20:00:08
* @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 Cancel from './Cancel';
export default class CancelTokenStatic implements CancelToken {
reason?: Cancel;
/**
*
*/
private _reason?: Cancel;
listener: Promise<Cancel>;
public listener: Promise<Cancel>;
constructor(executor: CancelExecutor) {
let action!: CancelAction;
@ -18,22 +21,22 @@ export default class CancelTokenStatic implements CancelToken {
this.listener = new Promise<Cancel>((resolve) => {
action = (message) => {
// 防止重复取消
if (this.reason) {
if (this._reason) {
return;
}
this.reason = new Cancel(message);
this._reason = new Cancel(message);
resolve(this.reason);
resolve(this._reason);
};
});
executor(action);
}
throwIfRequested(): void {
if (this.reason) {
throw this.reason;
public throwIfRequested(): void {
if (this._reason) {
throw this._reason;
}
}

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-13 18:00:27
* @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 buildURL from '../helpers/buildURL';
@ -21,6 +21,9 @@ export default class AxiosStatic implements Axios {
*/
public interceptors: Interceptors;
/**
* @param config
*/
constructor(config: AxiosRequestConfig = {}) {
this.defaults = config;
this.interceptors = {
@ -46,7 +49,9 @@ export default class AxiosStatic implements Axios {
* @param config Axios
*/
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 }) {

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-15 17:50:50
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-19 13:35:47
* @LastEditTime: 2020-04-23 09:16:23
*/
import {
InterceptorResolved,
@ -19,16 +19,16 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
/**
* id
*/
private id: number;
private _id: number;
/**
*
*/
private interceptors: Record<number, Interceptor<T>>;
private _interceptors: Record<number, Interceptor<T>>;
constructor() {
this.id = 0;
this.interceptors = {};
this._id = 0;
this._interceptors = {};
}
/**
@ -37,13 +37,13 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
* @param resolved
* @param rejected
*/
public use(resolved: InterceptorResolved<T>, rejected: InterceptorRejected = (err: any) => Promise.reject(err)) {
this.interceptors[++this.id] = {
public use(resolved: InterceptorResolved<T>, rejected?: InterceptorRejected) {
this._interceptors[++this._id] = {
resolved,
rejected,
};
return this.id;
return this._id;
}
/**
@ -52,7 +52,7 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
* @param id id
*/
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
*/
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') {
interceptors = interceptors.reverse();

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-14 22:23:39
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-20 13:23:04
* @LastEditTime: 2020-04-23 10:34:18
*/
import { AxiosRequestConfig, RequestConfig, AxiosResponse } from '../types';
@ -30,6 +30,12 @@ class AxiosError extends Error {
*/
public response?: AxiosResponse;
/**
* @param message
* @param config Axios
* @param request
* @param response Axios
*/
constructor(message: string, config: AxiosRequestConfig, request: RequestConfig, response?: AxiosResponse) {
super(message);

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-13 18:01:16
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-21 11:44:53
* @LastEditTime: 2020-04-23 09:31:00
*/
import { AxiosRequestConfig, AxiosResponse } from '../types';
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);

View File

@ -1,6 +0,0 @@
/*
* @Author: early-autumn
* @Date: 2020-04-22 15:59:25
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-22 15:59:42
*/

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-15 22:09:38
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-21 19:21:38
* @LastEditTime: 2020-04-23 09:11:41
*/
import { AxiosRequestConfig } from './types';
import adaptive from './adaptive';

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-14 23:22:52
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-22 16:42:48
* @LastEditTime: 2020-04-23 10:31:27
*/
import axios from './axios';

View File

@ -2,15 +2,13 @@
* @Author: early-autumn
* @Date: 2020-04-13 15:23:53
* @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?: Record<string, string>;
options?: AnyObject<string>;
/**
* get
*/
get?: Record<string, string>;
get?: AnyObject<string>;
/**
* head
*/
head?: Record<string, string>;
head?: AnyObject<string>;
/**
* post
*/
post?: Record<string, string>;
post?: AnyObject<string>;
/**
* put
*/
put?: Record<string, string>;
put?: AnyObject<string>;
/**
* delete
*/
delete?: Record<string, string>;
delete?: AnyObject<string>;
/**
* trace
*/
trace?: Record<string, string>;
trace?: AnyObject<string>;
/**
* 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 {
(error: any): Promise<any> | any;
(error: any): any;
}
/**
@ -417,10 +415,11 @@ export declare interface Interceptor<T = any> {
*
*/
resolved: InterceptorResolved<T>;
/**
*
*/
rejected: InterceptorRejected;
rejected?: InterceptorRejected;
}
/**
@ -575,7 +574,7 @@ export declare interface Axios {
* Axios
*/
export declare interface AxiosConstructor {
new (config: AxiosRequestConfig): Axios;
new (config?: AxiosRequestConfig): Axios;
}
/**
@ -643,11 +642,6 @@ export declare interface CancelExecutor {
*
*/
export declare interface CancelToken {
/**
*
*/
reason?: Cancel;
/**
*
*/
@ -695,12 +689,14 @@ export declare interface CancelTokenConstructor {
}
/**
* Axios
* Axios
*
* *
*/
export declare interface AxiosBaseInstance extends Axios {
/**
* HTTP
*
*
*
* @param config
@ -708,6 +704,8 @@ export declare interface AxiosBaseInstance extends Axios {
<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
/**
* HTTP
*
*
*
* @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 {
/**
* Axios
* Axios
*
* @param config
* @param config
*/
create(config?: AxiosRequestConfig): AxiosBaseInstance;
@ -742,11 +740,9 @@ export declare interface AxiosInstance extends AxiosBaseInstance {
CancelToken: CancelTokenConstructor;
/**
*
*
*
* @param value
*/
isCancel: (value: any) => boolean;
// extractData: <T extends Data>(response: Promise<AxiosResponse<T>>) => Promise<T>;
}

View File

@ -2,7 +2,7 @@
* @Author: early-autumn
* @Date: 2020-04-20 15:40:44
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-20 22:05:48
* @LastEditTime: 2020-04-23 09:29:17
*/
import InterceptorManager from '../../src/core/InterceptorManager';
@ -54,8 +54,14 @@ describe('测试 src/core/InterceptorManager.ts', () => {
it('异常', () => {
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'));
});
});