Improve auth refreshing to make it work better

This commit is contained in:
Oliver Akins 2022-10-12 22:03:28 -06:00
parent 7632a7931d
commit dbd8a486ae
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -8,7 +8,6 @@ export class TwitchAuth {
private token_type: string; private token_type: string;
private scope: string; private scope: string;
private expires_in: number; private expires_in: number;
private invalidated: boolean = false;
private _bid: string | undefined; private _bid: string | undefined;
constructor(token_data: TokenData) { constructor(token_data: TokenData) {
@ -113,11 +112,7 @@ export class TwitchAuth {
* @param conf The Axios RequestConfig, without method, url, or baseURL * @param conf The Axios RequestConfig, without method, url, or baseURL
* @returns The data returned from the Twitch API * @returns The data returned from the Twitch API
*/ */
public async request<T>(method: Method, url: string, conf:any={}): Promise<T> { public async request<T>(method: Method, url: string, conf:any={}, attempt=0): Promise<T> {
if (this.invalidated) {
throw new Error("Can't make a request with an invalidated token");
};
try { try {
let headers = { let headers = {
@ -148,6 +143,9 @@ export class TwitchAuth {
switch (err.response.status) { switch (err.response.status) {
case 403: case 403:
this.refresh(); this.refresh();
if (attempt < 3) {
this.request(method, url, conf, ++attempt);
};
break; break;
default: default:
log.error(err); log.error(err);