Enhance request handling with token refresh and retries
All checks were successful
SteamWarCI Build successful
All checks were successful
SteamWarCI Build successful
This commit is contained in:
@ -146,12 +146,12 @@ export class AuthV2Repo {
|
|||||||
this.setLoginState(response);
|
this.setLoginState(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
async request(url: string, params: RequestInit = {}) {
|
async request(url: string, params: RequestInit = {}, retryCount: number = 0) {
|
||||||
if (this.accessToken !== undefined && this.accessTokenExpires !== undefined && this.accessTokenExpires.isBefore(dayjs().add(10, "seconds"))) {
|
if (this.accessToken !== undefined && this.accessTokenExpires !== undefined && this.accessTokenExpires.isBefore(dayjs().add(10, "seconds"))) {
|
||||||
await this.refresh();
|
await this.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.requestWithToken(this.accessToken ?? "", url, params);
|
return this.requestWithToken(this.accessToken ?? "", url, params, retryCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async requestWithToken(token: string, url: string, params: RequestInit = {}, retryCount: number = 0): Promise<Response> {
|
private async requestWithToken(token: string, url: string, params: RequestInit = {}, retryCount: number = 0): Promise<Response> {
|
||||||
@ -165,11 +165,14 @@ export class AuthV2Repo {
|
|||||||
"Content-Type": "application/json", ...params.headers,
|
"Content-Type": "application/json", ...params.headers,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then(value => {
|
.then(async value => {
|
||||||
if (value.status === 401 && url !== "/auth") {
|
if (value.status === 401 && url !== "/auth") {
|
||||||
this.refresh();
|
try {
|
||||||
|
await this.refresh();
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
} catch (_e) { /* empty */ }
|
||||||
|
|
||||||
return this.requestWithToken(token, url, params, retryCount + 1);
|
return this.request(url, params, retryCount + 1);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user