ofetch
ofetch copied to clipboard
Ability to return a new response from `onResponseError`
hello, on submit, if the tokens are valid, everything works correctly and Swal displays a message, if the token is not valid, a refresh fires, the form submit fires again, but Swal no longer displays a message. How to deal with it?
async onResponseError(ctx: FetchContext): Promise<any> {
if (ctx.response.status === 401) {
const { error } = await this.refreshToken();
if (!error) {
// eslint-disable-next-line no-return-await
return await this.fetcher(ctx.request, ctx.options);
}
}
}
repository
async addProductToCart(body: any, filter: any = {}): Promise<any> {
return this.fetcher(this.urlGenerator.addProductToCart(), {
method: 'POST',
body,
params: filter
});
}
my function
const onSubmit = handleSubmit(async () => {
const {
error,
pending
} = await useLazyAsyncData(
'updateUserProfile',
() => httpRepository.addProductToCart(form),
{
initialCache: false
}
);
isLoading.value = pending.value;
if (!error.value) {
Swal.fire({
toast: true,
position: 'top-right',
text: 'Данные успешно обновлены!',
icon: 'success',
timer: 3000,
showConfirmButton: false
});
}
});
I feel this issue is potentially related to and/or duplicated of #114. But, we appreciate if you provide a minimal reproduction by which everyone can see what you see.
@maksimdrosdov Hi, do you solve this issue, I also meeting this problem right now
Any update on that?
hey, did u solve the issue?
I believe this is related to the fact that we don't look at what onResponseError can return: https://github.com/unjs/ofetch/blob/411904d3bf8c5c2f8009adf1e24af2a9fd7f249d/src/fetch.ts#L283
This means that we ignore what you can return.
I think we should change the behavior in order to transform the response based on it, similar to Axios.
I believe this issue is same as #224.
Have you tried to modify the context.response in onResponseError yet? It should work.
https://github.com/unjs/ofetch/issues/224#issuecomment-1592183119
I tried to modify the context.response in onResponseError - It doesn't work for me, I get the same result as when there is no onResponseError handler.
nuxt 3.8.2 Still facing this problem :(
@cgnz0r Can you create a codesandbox to reproduce the issue?