@complex-suite/request-axios
Advanced tools
+14
-0
@@ -0,2 +1,16 @@ | ||
| <!-- | ||
| 版本规则: | ||
| - 顶部 `### current` 记录当前未发版的改动(可能为空)。 | ||
| - 版本升级发版时,将 `current` 更替为本次发版版本号(如 `### 2.0.12`),并在其上方新建一个空的 `### current`。 | ||
| - 这样每次发版后,顶部始终保留一个 `### current`(可能为空)用于记录后续改动。 | ||
| --> | ||
| ### current | ||
| ### 2.0.15 | ||
| - fix(parseError): `$parseError` 新增 `code === 'ECONNABORTED' || code === 'ERR_TIMEOUT'` → `type: 'timeout'`,`code === 'ERR_NETWORK'` → `type: 'network'`,`failType` 类型同步扩展。 | ||
| - fix(parseError): `$parseError` 识别 `code === 'ERR_CANCELED'` 返回 `type: 'cancel'`,同步扩展 `failType` 和 `$fail`,cancel 时 msg=undefined 不弹错误提示。取消请求不再弹出错误提示。 | ||
| - fix(request): `$request` 深拷贝 headers/params(浅拷贝)和 data(FormData 逐条 append 到新实例,普通对象浅拷贝),遵守基类深拷贝契约,避免 login/refresh 重试时污染原 requestConfig。`_cloneData` 对 undefined/null/字符串/数字/布尔值等非对象类型直接返回原值,避免展开导致 GET 请求误发 `{}` body 或字符串 body 被拆为字符索引对象。 | ||
| - chore(import): 移除 `index.ts` 中未使用的 `RequestInitOption` 导入(`types/index.ts` 有自己的独立导入)。 | ||
| ### 2.0.11 | ||
@@ -3,0 +17,0 @@ - chore: 小版本升级,保持与套件其他子包版本同步。 |
+53
-5
| import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios' | ||
| import { BaseRequest } from '@complex-suite/request' | ||
| import type { RequestConfig, RequestInitOption, requestTrigger } from '@complex-suite/request' | ||
| import type { RequestConfig, requestTrigger } from '@complex-suite/request' | ||
| import type { AxiosRequestInitOption } from './src/types' | ||
@@ -14,9 +14,37 @@ | ||
| } | ||
| /** | ||
| * 拷贝 data:FormData 逐条 append 到新实例,普通对象浅拷贝 | ||
| * 避免重试(login/refresh)时 axios 拦截器或 format 函数修改污染原 requestConfig.data | ||
| * 注意:undefined/null/字符串/数字/布尔值等非对象类型直接返回原值,不能展开 | ||
| * - undefined/null:{ ...undefined } → {},会导致无 body 的请求误发 {} 作为 body | ||
| * - 字符串:{ ...'hello' } → { 0:'h', 1:'e', ... },破坏原始 body 格式 | ||
| */ | ||
| protected _cloneData(data: RequestConfig<R, L>['data']) { | ||
| if (data === undefined || data === null) { | ||
| return data | ||
| } | ||
| if (data instanceof FormData) { | ||
| const fd = new FormData() | ||
| data.forEach((value: FormDataEntryValue, key: string) => { | ||
| fd.append(key, value) | ||
| }) | ||
| return fd | ||
| } | ||
| if (typeof data === 'object') { | ||
| return { ...(data as Record<PropertyKey, unknown>) } | ||
| } | ||
| // 字符串/数字/布尔值等原始类型直接返回,不可展开 | ||
| return data | ||
| } | ||
| $request(requestConfig: RequestConfig<R, L>, trigger?: requestTrigger) { | ||
| // 拷贝 headers/data/params,避免 axios 拦截器修改污染原 requestConfig | ||
| // 重试(login/refresh)时复用同一个 requestConfig,引用共享会导致: | ||
| // 1. headers 累积重复(如 Authorization 被拦截器多次添加) | ||
| // 2. FormData 被 format 函数多次 append | ||
| const axiosRequestConfig = { | ||
| url: requestConfig.url, | ||
| method: requestConfig.method, | ||
| headers: requestConfig.headers, | ||
| data: requestConfig.data, | ||
| params: requestConfig.params, | ||
| headers: { ...requestConfig.headers }, | ||
| data: this._cloneData(requestConfig.data), | ||
| params: { ...requestConfig.params }, | ||
| responseType: requestConfig.responseType, | ||
@@ -30,3 +58,23 @@ ...requestConfig.local | ||
| } | ||
| $parseError(responseError: { response?: { status: number } }) { | ||
| $parseError(responseError: { response?: { status: number }, code?: string }) { | ||
| // 识别请求取消(ERR_CANCELED),返回 type: 'cancel',因 $fail.message.cancel 为 undefined, | ||
| // _showFail 收到 msg=undefined 且默认无 content 时不会弹出错误提示,实现取消请求静默 | ||
| if (responseError.code === 'ERR_CANCELED') { | ||
| return { | ||
| type: 'cancel' as const, | ||
| data: responseError | ||
| } as const | ||
| } | ||
| if (responseError.code === 'ECONNABORTED' || responseError.code === 'ERR_TIMEOUT') { | ||
| return { | ||
| type: 'timeout' as const, | ||
| data: responseError | ||
| } as const | ||
| } | ||
| if (responseError.code === 'ERR_NETWORK') { | ||
| return { | ||
| type: 'network' as const, | ||
| data: responseError | ||
| } as const | ||
| } | ||
| if (responseError.response) { | ||
@@ -33,0 +81,0 @@ return { |
+2
-2
| { | ||
| "name": "@complex-suite/request-axios", | ||
| "version": "2.0.14", | ||
| "version": "2.0.15", | ||
| "description": "a complex request from axios", | ||
@@ -16,3 +16,3 @@ "type": "module", | ||
| "axios": "^1.15.0", | ||
| "@complex-suite/request": "2.0.14" | ||
| "@complex-suite/request": "2.0.15" | ||
| }, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
13372
35.11%155
44.86%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed