Skip to content

Commit 48bbf32

Browse files
committed
refactor(axios): 优化未授权验证插件
1 parent abc4ffe commit 48bbf32

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

.changeset/four-pans-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ethan-utils/axios": patch
3+
---
4+
5+
优化未授权验证插件

packages/axios/plugins/unauthorized.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ export interface UnauthorizedOptions {
66
*/
77
onUnauthorized?: () => void;
88
/**
9-
* 触发未授权回调的响应数据中的 code 值,默认为 401
9+
* 触发未授权回调的响应数据中的 code 值,默认为 1001
1010
*/
1111
unauthorizedCodes?: number | number[];
1212
}
1313

1414
/**
1515
* 未授权处理插件
16-
* 支持检测 HTTP 状态码 401 和响应数据中的自定义 code 值
16+
* 支持检测 HTTP 状态码 401 和 403 以及响应数据中的自定义 code 值
1717
*/
1818
export function unauthorizedPlugin(
1919
api: AxiosInstance,
2020
options: UnauthorizedOptions = {},
2121
) {
22-
const { onUnauthorized, unauthorizedCodes = 401 } = options;
22+
const { onUnauthorized, unauthorizedCodes = 1001 } = options;
2323

2424
if (!onUnauthorized) {
2525
return;
@@ -40,9 +40,9 @@ export function unauthorizedPlugin(
4040
return response;
4141
}, undefined);
4242

43-
// 错误响应拦截器 - 检查 HTTP 状态码 401
43+
// 错误响应拦截器 - 检查 HTTP 状态码 401 或 403
4444
api.interceptors.response.use(undefined, (error: AxiosError) => {
45-
if (error.response?.status === 401) {
45+
if (error.response?.status === 401 || error.response?.status === 403) {
4646
onUnauthorized();
4747
}
4848
return Promise.reject(error);

0 commit comments

Comments
 (0)