当accessToken
它变坏时,interceptor
它会捕获错误,更新令牌并重复请求。重复请求,但没有数据写入store
。如何实施?
(response: AxiosResponse) => {
return response
},
async (error: unknown) => {
if (isAxiosError(error)) {
if (error.response?.status === StatusCode.Unauthorized) {
if (error.config?.url === Endpoints.updateToken) {
return Promise.reject(error)
} else {
try {
const params = error.config || undefined
await authStore.updateToken(params)
} catch (updateError) {
return Promise.reject(updateError)
}
}
}
}
return Promise.reject(error)
}
)
async updateToken(
params: InternalAxiosRequestConfig | undefined
) {
try {
const newToken = await authApi.updateToken()
setToLocalStorage(StorageKeys.AccessToken, newToken)
if (params) {
params.headers.Authorization = `Bearer ${newToken}`
const repeatedRequest = await instance.request(params)
// ???
}
} catch (error) {
return Promise.reject(error)
}
}
我尝试重复的不是请求,而是操作,从 axios error.config 获取数据,但带有路径参数的请求让我困惑"/api/v1/posts/{postId}/comments/{commentId}/answers/{answerId}/likes"