Skip to content
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

feat: auto retry for arbitrary HTTP status code #109

Closed
Nightfly-student opened this issue Jul 23, 2022 · 3 comments
Closed

feat: auto retry for arbitrary HTTP status code #109

Nightfly-student opened this issue Jul 23, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@Nightfly-student
Copy link

Nightfly-student commented Jul 23, 2022

Hi,

I am trying to auto retry if there was an error. I am using JWT with refresh tokens and want to refresh the token if the server sends an error.

const apiFetch = $fetch.create({
  retry: 1,
  keepalive: true,
  async onRequest({ request, options }) {
    var token = useCookie<tokenObject>("token").value;
    if (token && token.access_token) {
      options.headers = { Authorization: "Bearer " + token.access_token };
    }
  },
  async onResponseError({ request, response, options }) {
    if (response.status === 403) {
      const config = useRuntimeConfig();

      const check = new Promise((resolve, reject) => {
        try {
          apiFetch(`${config.DOMAIN}/api/refresh`, {
            method: "POST",
            body: {
              refreshToken: useCookie<tokenObject>("token").value.refresh_token,
            },
          }).then((res) => {
            const token = res;
            useCookie<tokenObject>("token").value = token;
            options.retry = 1;
            resolve(true);
          });
        } catch (err) {
          reject();
        }
      });
      console.log("hi");
      console.log(options);
    }
  },
});

The code to catch the request

    async logout() {
      const config = useRuntimeConfig();
      await apiFetch<dataObject>(`${config.DOMAIN}/api/users/logout`, {
        retry: 3,
        method: "POST",
        body: {
          token: useCookie<tokenObject>("token").value.refresh_token,
        },
      }).then(() => {
        useCookie("user").value = undefined;
        useCookie("token").value = undefined;
        this.user = null;
        this.token = null;
        this.isAuthenticated = false;
        this.isAdmin = false;
        this.isMod = false;
        this.isAffiliate = false;
        const router = useRouter();
        router.push("/");
      });
    },

example of call. both have retry both don't retry.

Let me know :)

@amihhs
Copy link
Contributor

amihhs commented Jul 28, 2022

If your status code is 403, it really won't execute.
see:https://github.com/unjs/ohmyfetch/blob/69ba2647d0ab1bced8b8faeda0fe01321c504e51/src/fetch.ts#L48

@nozomuikuta
Copy link
Member

As @amihhs already answered, currently it is statically defined when ohfetch automatically retries.

I will modify this issue to one for enhancement.

@nozomuikuta nozomuikuta added the enhancement New feature or request label Jan 11, 2023
@nozomuikuta nozomuikuta changed the title [Interceptor] Cannot auto retry after onResponseError feat: auto retry for arbitrary HTTP status code Jan 11, 2023
@pi0 pi0 closed this as completed in c1f075f Aug 23, 2023
@pi0
Copy link
Member

pi0 commented Aug 23, 2023

Thanks for the idea! Next version will support retryStatusCodes option in order to allow overriding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants