We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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 :)
The text was updated successfully, but these errors were encountered:
If your status code is 403, it really won't execute. see:https://github.com/unjs/ohmyfetch/blob/69ba2647d0ab1bced8b8faeda0fe01321c504e51/src/fetch.ts#L48
Sorry, something went wrong.
As @amihhs already answered, currently it is statically defined when ohfetch automatically retries.
I will modify this issue to one for enhancement.
c1f075f
Thanks for the idea! Next version will support retryStatusCodes option in order to allow overriding.
retryStatusCodes
No branches or pull requests
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.
The code to catch the request
example of call. both have retry both don't retry.
Let me know :)
The text was updated successfully, but these errors were encountered: