-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to return response
in interceptors
#224
Comments
The handlers can be async and I believe you can modify the |
I didn't know that, I will try and response in this thread later, thanks for supporting @danielroe 🙇 |
I confirm that modifying the |
Hi! Thanks! |
@happykust sure! something likes this const api = async (url: string) => {
const resp = await $fetch(url, {
onResponseError: async (context) => {
// Do something
const res: any = await api(url);
context.response = res;
},
});
return resp;
}; Please note that this only work when you modify the |
Hello, |
PR welcome to improve docs 👍 |
@danielroe It seems some thing's wrong in new version, modify the |
I have the exact same scenario. I am refreshing tokens on 401 and I want to catch the error in that handler. Unfortunately even though the refresh is successful and I re-run the original request, the first request throws the error and Nuxt displays an error page. Here's a code snippet: export const apiClient = ofetch.create({
onRequest: ({ options }) => {
options.headers = authHeaders()
},
onResponseError: async ({ request, response, options }) => {
const authStore = useAuthStore()
const refreshToken = localStorage.getItem(REFRESH_TOKEN_KEY)
// refresh tokens & re-attempt request
if (response.status === StatusCodes.UNAUTHORIZED) {
if (
response._data.code === 'invalid_access_token' &&
refreshToken
) {
await authStore.refreshTokens()
response = await apiClient(request, {
...options,
headers: authHeaders(),
})
} else {
return authStore.onLogout()
}
}
},
}) |
@hymair I was faced the same problem as well and I finally figured it out. There are some points I can tell you: Firstly, if you want to retry an 401 response and turn the request from Secondly, the Finally, you don't override the original response by assigning it a new value, javascript does not work like that response = await apiClient(request, { ... }) This mean you are re-assigning a new value for the async onResponse(context) {
context.response = await apiClient(request, { ... })
} However, I believe there is some other properties in the async onResponse(context) {
context.response = await apiClient(request, {
onResponse(ctx) {
Object.assign(context, ctx);
}
})
} Btw, I already write an authentication module for Nuxt 3: https://github.com/trandaison/nuxt-3-auth. It support refresh token too, just give it a try if possible 🙏 You also find the code where I process the refresh access token logic here |
I close this issue, see this comment #224 (comment) to solve the problem. |
Describe the feature
Currently, we already have interceptors, however it's just an synchronous callback on request/response/error...
Is there any way to return a response in response interceptors? This is an example of axios
Use case:
I want to catch error 401 in
onResponseError
then refresh access token, then retry the api call and return the new response from retry request.Currently, the callback calls but the first request still returns error.
Additional information
The text was updated successfully, but these errors were encountered: