From cf2e46476dea98777bf12fdcfc20e527de266575 Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Wed, 5 Apr 2023 16:35:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=86=99=20AxiosError=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/adapter.ts | 20 ++++++++++---------- src/axios.ts | 2 +- src/core/createError.ts | 14 +++++++------- src/core/request.ts | 22 +++++++++++----------- test/core/createError.test.ts | 12 +++++++----- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/adapter.ts b/src/adapter.ts index c6b95cc..33324b2 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -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, diff --git a/src/axios.ts b/src/axios.ts index 0d9ff11..7867542 100644 --- a/src/axios.ts +++ b/src/axios.ts @@ -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; } diff --git a/src/core/createError.ts b/src/core/createError.ts index 92fd77c..5bd67e6 100644 --- a/src/core/createError.ts +++ b/src/core/createError.ts @@ -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; } diff --git a/src/core/request.ts b/src/core/request.ts index 79dd41d..3484a91 100644 --- a/src/core/request.ts +++ b/src/core/request.ts @@ -5,7 +5,6 @@ import { AxiosAdapterRequestMethod, AxiosAdapterResponse, AxiosAdapterResponseError, - AxiosAdapterTask, } from '../adapter'; import { AxiosProgressCallback, @@ -56,9 +55,7 @@ export function request( fail, }; - const adapterTask = config.adapter!(adapterConfig) as - | AxiosAdapterTask - | undefined; + const adapterTask = config.adapter!(adapterConfig); function success(_: AxiosAdapterResponse): void { const response = _ as AxiosResponse; @@ -75,15 +72,18 @@ export function request( } 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)) { diff --git a/test/core/createError.test.ts b/test/core/createError.test.ts index 8944b30..2b6c24c 100644 --- a/test/core/createError.test.ts +++ b/test/core/createError.test.ts @@ -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(); }); });