@complex-suite/request-axios
Advanced tools
+5
-0
@@ -10,2 +10,7 @@ <!-- | ||
| ### 2.0.16 | ||
| - fix(parseError): `$parseError` 参数类型改为 `unknown`,新增非对象守卫 `!responseError || typeof responseError !== 'object'` 统一返回 `{ type:'internal' }`,避免 null/字符串/Error 等非 axios 错误二次抛错 | ||
| - fix(request): `_cloneData` 改用 `deepCloneData` 深拷贝替代 `{...data}` 浅拷贝,FormData/嵌套对象均完全独立,重试不再累积 token | ||
| - fix(request): `$request` 中 params 从浅拷贝 `{ ...requestConfig.params }` 改为 `deepCloneData(requestConfig.params)` 深拷贝,与 data 处理一致,防止 format/拦截器修改嵌套 params 污染重试 | ||
| ### 2.0.15 | ||
@@ -12,0 +17,0 @@ - fix(parseError): `$parseError` 新增 `code === 'ECONNABORTED' || code === 'ERR_TIMEOUT'` → `type: 'timeout'`,`code === 'ERR_NETWORK'` → `type: 'network'`,`failType` 类型同步扩展。 |
+17
-32
| import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios' | ||
| import { BaseRequest } from '@complex-suite/request' | ||
| import type { RequestConfig, requestTrigger } from '@complex-suite/request' | ||
| import { deepCloneData } from '@complex-suite/utils' | ||
| import type { AxiosRequestInitOption } from './src/types' | ||
@@ -14,26 +15,2 @@ | ||
| } | ||
| /** | ||
| * 拷贝 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) { | ||
@@ -48,4 +25,4 @@ // 拷贝 headers/data/params,避免 axios 拦截器修改污染原 requestConfig | ||
| headers: { ...requestConfig.headers }, | ||
| data: this._cloneData(requestConfig.data), | ||
| params: { ...requestConfig.params }, | ||
| data: deepCloneData(requestConfig.data), | ||
| params: deepCloneData(requestConfig.params), | ||
| responseType: requestConfig.responseType, | ||
@@ -59,6 +36,14 @@ ...requestConfig.local | ||
| } | ||
| $parseError(responseError: { response?: { status: number }, code?: string }) { | ||
| $parseError(responseError: unknown) { | ||
| // 非对象异常(null/字符串/普通 Error 等)统一按内部错误处理 | ||
| if (!responseError || typeof responseError !== 'object') { | ||
| return { | ||
| type: 'internal' as const, | ||
| data: responseError | ||
| } as const | ||
| } | ||
| const err = responseError as { response?: { status: number }, code?: string } | ||
| // 识别请求取消(ERR_CANCELED),返回 type: 'cancel',因 $fail.message.cancel 为 undefined, | ||
| // _showFail 收到 msg=undefined 且默认无 content 时不会弹出错误提示,实现取消请求静默 | ||
| if (responseError.code === 'ERR_CANCELED') { | ||
| if (err.code === 'ERR_CANCELED') { | ||
| return { | ||
@@ -69,3 +54,3 @@ type: 'cancel' as const, | ||
| } | ||
| if (responseError.code === 'ECONNABORTED' || responseError.code === 'ERR_TIMEOUT') { | ||
| if (err.code === 'ECONNABORTED' || err.code === 'ERR_TIMEOUT') { | ||
| return { | ||
@@ -76,3 +61,3 @@ type: 'timeout' as const, | ||
| } | ||
| if (responseError.code === 'ERR_NETWORK') { | ||
| if (err.code === 'ERR_NETWORK') { | ||
| return { | ||
@@ -83,6 +68,6 @@ type: 'network' as const, | ||
| } | ||
| if (responseError.response) { | ||
| if (err.response) { | ||
| return { | ||
| type: 'server', | ||
| msg: this.status[responseError.response.status]?.(), | ||
| msg: this.status[err.response.status]?.(), | ||
| data: responseError | ||
@@ -89,0 +74,0 @@ } as const |
+2
-2
| { | ||
| "name": "@complex-suite/request-axios", | ||
| "version": "2.0.15", | ||
| "version": "2.0.16", | ||
| "description": "a complex request from axios", | ||
@@ -16,3 +16,3 @@ "type": "module", | ||
| "axios": "^1.15.0", | ||
| "@complex-suite/request": "2.0.15" | ||
| "@complex-suite/request": "2.0.16" | ||
| }, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
13261
-0.83%140
-9.68%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed