Add retry mechanism and limit for token requests
All checks were successful
SteamWarCI Build successful
All checks were successful
SteamWarCI Build successful
This commit is contained in:
@ -18,7 +18,6 @@
|
||||
*/
|
||||
|
||||
import {readable, writable} from "svelte/store";
|
||||
import {tokenStore} from "@repo/repo.ts";
|
||||
import dayjs, {type Dayjs} from "dayjs";
|
||||
import {type AuthToken, AuthTokenSchema} from "@type/auth.ts";
|
||||
|
||||
@ -152,7 +151,11 @@ export class AuthV2Repo {
|
||||
return this.requestWithToken(this.accessToken ?? "", url, params);
|
||||
}
|
||||
|
||||
private async requestWithToken(token: string, url: string, params: RequestInit = {}) {
|
||||
private async requestWithToken(token: string, url: string, params: RequestInit = {}, retryCount: number = 0): Promise<Response> {
|
||||
if (retryCount >= 3) {
|
||||
throw new Error("Too many retries");
|
||||
}
|
||||
|
||||
return fetch(`${import.meta.env.PUBLIC_API_SERVER}${url}`, {...params,
|
||||
headers: {
|
||||
...(token !== "" ? {"Authorization": "Bearer " + (token)} : {}),
|
||||
@ -161,7 +164,9 @@ export class AuthV2Repo {
|
||||
})
|
||||
.then(value => {
|
||||
if (value.status === 401) {
|
||||
tokenStore.set("");
|
||||
this.refresh();
|
||||
|
||||
return this.requestWithToken(token, url, params, retryCount + 1);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user