Skip to content

Commit d702524

Browse files
committed
optimize(projects): optimize response code comparison
1 parent 6bb66a0 commit d702524

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/service/request/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
3535
},
3636
async onBackendFail(response, instance) {
3737
const authStore = useAuthStore();
38+
const responseCode = String(response.data.code);
3839

3940
function handleLogout() {
4041
authStore.resetStore();
@@ -49,14 +50,14 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
4950

5051
// when the backend response code is in `logoutCodes`, it means the user will be logged out and redirected to login page
5152
const logoutCodes = import.meta.env.VITE_SERVICE_LOGOUT_CODES?.split(',') || [];
52-
if (logoutCodes.includes(response.data.code)) {
53+
if (logoutCodes.includes(responseCode)) {
5354
handleLogout();
5455
return null;
5556
}
5657

5758
// when the backend response code is in `modalLogoutCodes`, it means the user will be logged out by displaying a modal
5859
const modalLogoutCodes = import.meta.env.VITE_SERVICE_MODAL_LOGOUT_CODES?.split(',') || [];
59-
if (modalLogoutCodes.includes(response.data.code) && !request.state.errMsgStack?.includes(response.data.msg)) {
60+
if (modalLogoutCodes.includes(responseCode) && !request.state.errMsgStack?.includes(responseCode)) {
6061
request.state.errMsgStack = [...(request.state.errMsgStack || []), response.data.msg];
6162

6263
// prevent the user from refreshing the page
@@ -81,7 +82,7 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
8182
// when the backend response code is in `expiredTokenCodes`, it means the token is expired, and refresh token
8283
// the api `refreshToken` can not return error code in `expiredTokenCodes`, otherwise it will be a dead loop, should return `logoutCodes` or `modalLogoutCodes`
8384
const expiredTokenCodes = import.meta.env.VITE_SERVICE_EXPIRED_TOKEN_CODES?.split(',') || [];
84-
if (expiredTokenCodes.includes(response.data.code) && !request.state.isRefreshingToken) {
85+
if (expiredTokenCodes.includes(responseCode) && !request.state.isRefreshingToken) {
8586
request.state.isRefreshingToken = true;
8687

8788
const refreshConfig = await handleRefreshToken(response.config);
@@ -107,7 +108,7 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
107108
// get backend error message and code
108109
if (error.code === BACKEND_ERROR_CODE) {
109110
message = error.response?.data?.msg || message;
110-
backendErrorCode = error.response?.data?.code || '';
111+
backendErrorCode = String(error.response?.data?.code) || '';
111112
}
112113

113114
// the error message is displayed in the modal

0 commit comments

Comments
 (0)