ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

Ability to return a new response from `onResponseError`

Open maksimdrosdov opened this issue 3 years ago • 8 comments

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
      });
    }
  });

maksimdrosdov avatar Nov 12 '22 07:11 maksimdrosdov

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.

nozomuikuta avatar Jan 12 '23 05:01 nozomuikuta

@maksimdrosdov Hi, do you solve this issue, I also meeting this problem right now

linshujuan avatar Jul 14 '23 13:07 linshujuan

Any update on that?

TusharRoy23 avatar Jul 26 '23 07:07 TusharRoy23

hey, did u solve the issue?

dselivanovvv avatar Aug 01 '23 19:08 dselivanovvv

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.

atinux avatar Sep 06 '23 16:09 atinux

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

trandaison avatar Dec 17 '23 15:12 trandaison

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 avatar Dec 18 '23 07:12 cgnz0r

@cgnz0r Can you create a codesandbox to reproduce the issue?

trandaison avatar Dec 19 '23 01:12 trandaison