refactor: 重写 AxiosError 类型

pull/41/head
zjx0905 2023-04-05 16:35:25 +08:00
parent f1c0f348be
commit cf2e46476d
5 changed files with 36 additions and 34 deletions

View File

@ -104,15 +104,15 @@ export interface AxiosAdapterDownloadOptions extends AxiosAdapterBaseOptions {
}
export interface AxiosAdapterRequest {
(config: AxiosAdapterBaseOptions): AxiosAdapterTask | void;
(config: AxiosAdapterBaseOptions): AxiosAdapterTask;
}
export interface AxiosAdapterUpload {
(config: AxiosAdapterUploadOptions): AxiosAdapterTask | void;
(config: AxiosAdapterUploadOptions): AxiosAdapterTask;
}
export interface AxiosAdapterDownload {
(config: AxiosAdapterDownloadOptions): AxiosAdapterTask | void;
(config: AxiosAdapterDownloadOptions): AxiosAdapterTask;
}
export interface AxiosPlatform {
@ -121,14 +121,14 @@ export interface AxiosPlatform {
download: AxiosAdapterDownload;
}
export interface AxiosAdapterTask {
export type AxiosAdapterTask = {
abort?(): void;
onProgressUpdate?(callback: AxiosProgressCallback): void;
offProgressUpdate?(callback: AxiosProgressCallback): void;
}
} | void;
export interface AxiosAdapter {
(config: AxiosAdapterRequestConfig): AxiosAdapterTask | void;
(config: AxiosAdapterRequestConfig): AxiosAdapterTask;
}
export function getAdapterDefault(): AxiosAdapter | undefined {
@ -171,7 +171,7 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
assert(isFunction(platform.upload), 'upload 不是一个 function');
assert(isFunction(platform.download), 'download 不是一个 function');
function adapter(config: AxiosAdapterRequestConfig): AxiosAdapterTask | void {
function adapter(config: AxiosAdapterRequestConfig): AxiosAdapterTask {
const baseOptions = transformOptions(config);
switch (config.type) {
@ -189,14 +189,14 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
function callRequest(
request: AxiosAdapterRequest,
baseOptions: AxiosAdapterBaseOptions,
): AxiosAdapterTask | void {
): AxiosAdapterTask {
return request(baseOptions);
}
function callUpload(
upload: AxiosAdapterUpload,
baseOptions: AxiosAdapterBaseOptions,
): AxiosAdapterTask | void {
): AxiosAdapterTask {
const { fileName, filePath, fileType, ...formData } =
baseOptions.data as AxiosRequestFormData;
const options = {
@ -214,7 +214,7 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
function callDownload(
download: AxiosAdapterDownload,
baseOptions: AxiosAdapterBaseOptions,
): AxiosAdapterTask | void {
): AxiosAdapterTask {
const options = {
...baseOptions,
filePath: baseOptions.params?.filePath,

View File

@ -11,7 +11,7 @@ import { mergeConfig } from './core/mergeConfig';
import { AxiosAdapter, createAdapter, AxiosPlatform } from './adapter';
import defaults from './defaults';
export interface AxiosInstanceDefaults extends AxiosRequestHeaders {
export interface AxiosInstanceDefaults extends AxiosRequestConfig {
headers: Required<AxiosRequestHeaders>;
}

View File

@ -7,15 +7,15 @@ export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
class AxiosError extends Error {
public config: AxiosRequestConfig;
public request?: AxiosAdapterTask;
public request: AxiosAdapterTask;
public response?: AxiosErrorResponse;
public response: AxiosErrorResponse;
public constructor(
message: string,
config: AxiosRequestConfig,
request?: AxiosAdapterTask,
response?: AxiosErrorResponse,
response: AxiosErrorResponse,
request: AxiosAdapterTask,
) {
super(message);
@ -30,10 +30,10 @@ class AxiosError extends Error {
export function createError(
message: string,
config: AxiosRequestConfig,
request?: AxiosAdapterTask,
response?: AxiosErrorResponse,
response: AxiosErrorResponse,
request: AxiosAdapterTask,
): AxiosError {
const axiosError = new AxiosError(message, config, request, response);
const axiosError = new AxiosError(message, config, response, request);
cleanStack(axiosError);
return axiosError;
}

View File

@ -5,7 +5,6 @@ import {
AxiosAdapterRequestMethod,
AxiosAdapterResponse,
AxiosAdapterResponseError,
AxiosAdapterTask,
} from '../adapter';
import {
AxiosProgressCallback,
@ -56,9 +55,7 @@ export function request<TData = unknown>(
fail,
};
const adapterTask = config.adapter!(adapterConfig) as
| AxiosAdapterTask
| undefined;
const adapterTask = config.adapter!(adapterConfig);
function success(_: AxiosAdapterResponse<TData>): void {
const response = _ as AxiosResponse<TData>;
@ -75,15 +72,18 @@ export function request<TData = unknown>(
}
function fail(_: AxiosAdapterResponseError): void {
const error = _ as AxiosResponseError;
error.isFail = true;
error.config = config;
error.request = adapterTask;
catchError('网络错误', error);
const responseError = _ as AxiosResponseError;
responseError.isFail = true;
responseError.config = config;
responseError.request = adapterTask;
catchError('网络错误', responseError);
}
function catchError(message: string, response?: AxiosErrorResponse): void {
reject(createError(message, config, adapterTask, response));
function catchError(
message: string,
errorResponse: AxiosErrorResponse,
): void {
reject(createError(message, config, errorResponse, adapterTask));
}
if (isPlainObject(adapterTask)) {

View File

@ -5,28 +5,30 @@ import { createError, isAxiosError } from 'src/core/createError';
describe('src/core/createError.ts', () => {
test('应该支持空参数', () => {
const c = {};
const err = createError('error', c);
const r = {} as any;
const err = createError('error', c, r);
expect(err.message).toBe('error');
expect(err.config).toBe(c);
expect(err.response).toBe(r);
expect(checkStack(err)).toBeTruthy();
});
test('应该支持传入更多参数', () => {
const c = {};
const req = {};
const res = {};
const err = createError('error', c, req, res as any);
const req = {} as any;
const err = createError('error', c, res as any, req);
expect(err.message).toBe('error');
expect(err.config).toBe(c);
expect(err.request).toBe(req);
expect(err.response).toBe(res);
expect(err.request).toBe(req);
});
test('应该正确判断 AxiosError', () => {
expect(isAxiosError(0)).toBeFalsy();
expect(isAxiosError(new Error())).toBeFalsy();
expect(isAxiosError(createError('error', {}))).toBeTruthy();
expect(isAxiosError(createError('error', {}, {} as any))).toBeTruthy();
});
});