Add retry mechanism and limit for token requests
All checks were successful
SteamWarCI Build successful

This commit is contained in:
2025-03-01 11:27:06 +01:00
parent 23e10eef0f
commit f03867b9a7

View File

@ -18,7 +18,6 @@
*/ */
import {readable, writable} from "svelte/store"; import {readable, writable} from "svelte/store";
import {tokenStore} from "@repo/repo.ts";
import dayjs, {type Dayjs} from "dayjs"; import dayjs, {type Dayjs} from "dayjs";
import {type AuthToken, AuthTokenSchema} from "@type/auth.ts"; import {type AuthToken, AuthTokenSchema} from "@type/auth.ts";
@ -152,7 +151,11 @@ export class AuthV2Repo {
return this.requestWithToken(this.accessToken ?? "", url, params); 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, return fetch(`${import.meta.env.PUBLIC_API_SERVER}${url}`, {...params,
headers: { headers: {
...(token !== "" ? {"Authorization": "Bearer " + (token)} : {}), ...(token !== "" ? {"Authorization": "Bearer " + (token)} : {}),
@ -161,7 +164,9 @@ export class AuthV2Repo {
}) })
.then(value => { .then(value => {
if (value.status === 401) { if (value.status === 401) {
tokenStore.set(""); this.refresh();
return this.requestWithToken(token, url, params, retryCount + 1);
} }
return value; return value;
}); });