unjs / unjs/ofetch

Ability to return a new response from `onResponseError`

Open
#160 8 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
5.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the onResponseError hook and the fetcher calls shown in the issue, then trace how the refreshed request result reaches addProductToCart and useLazyAsyncData. Done should mean the 401 refresh path returns a usable response or error to the original submit flow, so its existing success handling can run.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.