From 1152853ce2ba8e1543540d8407521687b53e5ae7 Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Sun, 7 May 2023 23:16:28 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E4=BF=AE=E6=AD=A3=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/ISSUE_TEMPLATE/feature_request.yml | 2 +- docs/pages/basics/download.md | 6 ++-- package.json | 4 +-- src/adpater/createAdapter.ts | 2 +- src/adpater/getDefaultAdapter.ts | 2 +- src/core/Axios.ts | 2 +- src/core/MiddlewareManager.ts | 30 ++++++++++++++++--- src/core/mergeConfig.ts | 2 +- src/helpers/buildURL.ts | 2 +- src/helpers/deepMerge.ts | 2 +- src/helpers/transformURL.ts | 3 +- src/helpers/{isTypes.ts => types.ts} | 0 src/request/dispatchRequest.ts | 2 +- src/request/request.ts | 16 +++++----- src/request/transformData.ts | 2 +- .../{isTypes.test.ts => types.test.ts} | 4 +-- 16 files changed, 52 insertions(+), 29 deletions(-) rename src/helpers/{isTypes.ts => types.ts} (100%) rename test/helpers/{isTypes.test.ts => types.test.ts} (95%) diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index cf7e04f..795e139 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -17,7 +17,7 @@ body: id: suggested-solution attributes: label: 建议的解决方案 - description: '在模块 [xy] 中,我们可以提供以下实现......' + description: '我们可以提供以下实现......' validations: required: true - type: textarea diff --git a/docs/pages/basics/download.md b/docs/pages/basics/download.md index 18a809c..ac9ba90 100644 --- a/docs/pages/basics/download.md +++ b/docs/pages/basics/download.md @@ -26,7 +26,7 @@ axios('https://api.com/test', { const { // 临时文件路径 (本地路径)。没传入 filePath 指定文件存储路径时会返回,下载后的文件会存储到一个临时文件 tempFilePath, - } = response; + } = response.data; }) .catch((error) => { // 失败之后做些什么 @@ -50,7 +50,7 @@ axios('https://api.com/test', { const { // 指定文件下载后存储的路径 (本地路径) filePath, - } = response; + } = response.data; }) .catch((error) => { // 失败之后做些什么 @@ -86,7 +86,7 @@ axios('https://api.com/test', { const { // 指定文件下载后存储的路径 (本地路径) filePath, - } = response; + } = response.data; }) .catch((error) => { // 失败之后做些什么 diff --git a/package.json b/package.json index de2f99e..e1cd317 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "docs:preview": "pnpm -C docs preview", "docs:deploy": "esno scripts/docs.deploy.ts", "start": "esno scripts/start.ts", - "tidy": "prettier **/*.{js,json} --write && prettier **/*.ts?(x) --parser=typescript --write" + "tidy-up": "prettier **/*.{js,json} --write & prettier **/*.ts --parser=typescript --write" }, "devDependencies": { "@commitlint/cli": "^17.4.4", @@ -94,7 +94,7 @@ "*.{js,json}": [ "prettier --write" ], - "*.ts?(x)": [ + "*.ts": [ "eslint", "prettier --parser=typescript --write" ] diff --git a/src/adpater/createAdapter.ts b/src/adpater/createAdapter.ts index ccdf4af..679df6c 100644 --- a/src/adpater/createAdapter.ts +++ b/src/adpater/createAdapter.ts @@ -1,4 +1,4 @@ -import { isFunction, isPlainObject } from '../helpers/isTypes'; +import { isFunction, isPlainObject } from '../helpers/types'; import { assert } from '../helpers/error'; import { origIgnore } from '../helpers/ignore'; import { diff --git a/src/adpater/getDefaultAdapter.ts b/src/adpater/getDefaultAdapter.ts index 8344261..174991e 100644 --- a/src/adpater/getDefaultAdapter.ts +++ b/src/adpater/getDefaultAdapter.ts @@ -1,4 +1,4 @@ -import { isFunction, isPlainObject } from '../helpers/isTypes'; +import { isFunction, isPlainObject } from '../helpers/types'; import { AxiosAdapterPlatform, createAdapter } from './createAdapter'; /** diff --git a/src/core/Axios.ts b/src/core/Axios.ts index c932a5b..afc91cb 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -3,7 +3,7 @@ import { WITH_DATA_METHODS, WITH_PARAMS_METHODS, } from '../constants/methods'; -import { isString } from '../helpers/isTypes'; +import { isString } from '../helpers/types'; import { dispatchRequest } from '../request/dispatchRequest'; import { CancelToken } from '../request/cancel'; import { AxiosTransformer } from '../request/transformData'; diff --git a/src/core/MiddlewareManager.ts b/src/core/MiddlewareManager.ts index dc2acc9..bc077f7 100644 --- a/src/core/MiddlewareManager.ts +++ b/src/core/MiddlewareManager.ts @@ -1,5 +1,5 @@ import { assert } from '../helpers/error'; -import { isFunction } from '../helpers/isTypes'; +import { isFunction } from '../helpers/types'; import { AxiosRequestConfig, AxiosResponse } from './Axios'; export interface MiddlewareNext { @@ -41,7 +41,29 @@ export default class MiddlewareManager { /** * 注册中间件 * - * @param middleware 中间件 + * 示例1:注册一个中间件 + * ```ts + * axios.use(async function middleware(ctx, next) { + * console.log(ctx.req); + * await next(); + * console.log(ctx.res); + * }); + * ``` + * + * 示例2:链式注册多个中间件 + * ```ts + * axios + * .use(async function middleware1(ctx, next) { + * console.log(ctx.req); + * await next(); + * console.log(ctx.res); + * }) + * .use(async function middleware2(ctx, next) { + * console.log(ctx.req); + * await next(); + * console.log(ctx.res); + * }); + * ``` */ use(middleware: MiddlewareCallback) { assert(isFunction(middleware), 'middleware 不是一个 function'); @@ -59,7 +81,7 @@ export default class MiddlewareManager { } /** - * 中间件执行器 + * 运行中间件 * * @param ctx 中间件上下文 * @param respond 目标函数 @@ -73,7 +95,7 @@ export default class MiddlewareManager { } /** - * 强化中间件执行器 + * 强化运行中间件 * * @param enhancer 强化器 */ diff --git a/src/core/mergeConfig.ts b/src/core/mergeConfig.ts index 6456daa..1f7961f 100644 --- a/src/core/mergeConfig.ts +++ b/src/core/mergeConfig.ts @@ -1,4 +1,4 @@ -import { isUndefined, isPlainObject } from '../helpers/isTypes'; +import { isUndefined, isPlainObject } from '../helpers/types'; import { deepMerge } from '../helpers/deepMerge'; import { AxiosRequestConfig } from './Axios'; diff --git a/src/helpers/buildURL.ts b/src/helpers/buildURL.ts index caac4a8..63a565d 100644 --- a/src/helpers/buildURL.ts +++ b/src/helpers/buildURL.ts @@ -1,4 +1,4 @@ -import { isArray, isDate, isNull, isPlainObject, isUndefined } from './isTypes'; +import { isArray, isDate, isNull, isPlainObject, isUndefined } from './types'; export function buildURL( url = '', diff --git a/src/helpers/deepMerge.ts b/src/helpers/deepMerge.ts index 0e55338..b9a3a50 100644 --- a/src/helpers/deepMerge.ts +++ b/src/helpers/deepMerge.ts @@ -1,4 +1,4 @@ -import { isPlainObject } from './isTypes'; +import { isPlainObject } from './types'; export function deepMerge(...objs: (T | undefined)[]): T { const result: AnyObject = {}; diff --git a/src/helpers/transformURL.ts b/src/helpers/transformURL.ts index cee051b..35c43c4 100644 --- a/src/helpers/transformURL.ts +++ b/src/helpers/transformURL.ts @@ -1,5 +1,5 @@ import { AxiosRequestConfig } from '../core/Axios'; -import { isPlainObject } from './isTypes'; +import { isPlainObject } from './types'; import { buildURL } from './buildURL'; import { combineURL } from './combineURL'; import { dynamicURL } from './dynamicURL'; @@ -10,6 +10,5 @@ export function transformURL(config: AxiosRequestConfig) { config.params, isPlainObject(config.data) ? config.data : {}, ); - return buildURL(fullPath, config.params, config.paramsSerializer); } diff --git a/src/helpers/isTypes.ts b/src/helpers/types.ts similarity index 100% rename from src/helpers/isTypes.ts rename to src/helpers/types.ts diff --git a/src/request/dispatchRequest.ts b/src/request/dispatchRequest.ts index ff17d75..fca2431 100644 --- a/src/request/dispatchRequest.ts +++ b/src/request/dispatchRequest.ts @@ -1,5 +1,5 @@ import { WITH_DATA_RE } from '../constants/methods'; -import { isFunction, isString } from '../helpers/isTypes'; +import { isFunction, isString } from '../helpers/types'; import { assert } from '../helpers/error'; import { AxiosRequestConfig, AxiosResponse } from '../core/Axios'; import { Cancel, isCancel, isCancelToken } from './cancel'; diff --git a/src/request/request.ts b/src/request/request.ts index b2e5701..124e32a 100644 --- a/src/request/request.ts +++ b/src/request/request.ts @@ -1,4 +1,4 @@ -import { isFunction, isPlainObject } from '../helpers/isTypes'; +import { isFunction, isPlainObject } from '../helpers/types'; import { transformURL } from '../helpers/transformURL'; import { AxiosRequestConfig, @@ -91,7 +91,9 @@ export function request(config: AxiosRequestConfig) { if (isPlainObject(adapterTask)) { tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate); - adapterTask?.abort?.(); + if (isFunction(adapterTask.abort)) { + adapterTask.abort(); + } } reject(reason); @@ -102,19 +104,19 @@ export function request(config: AxiosRequestConfig) { function tryToggleProgressUpdate( config: AxiosAdapterRequestConfig, - progress?: (cb: (event: AnyObject) => void) => void, + toggle?: (cb: (event: AnyObject) => void) => void, ) { - const { type, onUploadProgress, onDownloadProgress } = config; - if (isFunction(progress)) { + if (isFunction(toggle)) { + const { type, onUploadProgress, onDownloadProgress } = config; switch (type) { case 'upload': if (isFunction(onUploadProgress)) { - progress(onUploadProgress); + toggle(onUploadProgress); } break; case 'download': if (isFunction(onDownloadProgress)) { - progress(onDownloadProgress); + toggle(onDownloadProgress); } break; } diff --git a/src/request/transformData.ts b/src/request/transformData.ts index ed677e2..a08b86f 100644 --- a/src/request/transformData.ts +++ b/src/request/transformData.ts @@ -1,4 +1,4 @@ -import { isArray, isFunction } from '../helpers/isTypes'; +import { isArray, isFunction } from '../helpers/types'; export interface AxiosTransformCallback { ( diff --git a/test/helpers/isTypes.test.ts b/test/helpers/types.test.ts similarity index 95% rename from test/helpers/isTypes.test.ts rename to test/helpers/types.test.ts index f0c008d..69ddcaf 100644 --- a/test/helpers/isTypes.test.ts +++ b/test/helpers/types.test.ts @@ -7,9 +7,9 @@ import { isNull, isUndefined, isString, -} from '@/helpers/isTypes'; +} from '@/helpers/types'; -describe('src/helpers/isTypes.ts', () => { +describe('src/helpers/types.ts', () => { test('应该能判断是数组', () => { expect(isArray(new Array(1))).toBeTruthy(); expect(isArray([])).toBeTruthy();