feat: if there is a rate limit, wait two seconds and try again

This commit is contained in:
Nevo David 2024-11-27 17:09:47 +07:00
parent e4ac191aad
commit e3bd1df158
1 changed files with 8 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import { timer } from '@gitroom/helpers/utils/timer';
export class RefreshToken {
constructor(
public identifier: string,
@ -18,7 +20,7 @@ export class NotEnoughScopes {
}
export abstract class SocialAbstract {
async fetch(url: string, options: RequestInit = {}, identifier = '') {
async fetch(url: string, options: RequestInit = {}, identifier = ''): Promise<Response> {
const request = await fetch(url, options);
if (request.status === 200 || request.status === 201) {
@ -33,6 +35,11 @@ export abstract class SocialAbstract {
json = '{}';
}
if (json.includes('rate_limit_exceeded') || json.includes('Rate limit')) {
await timer(2000);
return this.fetch(url, options, identifier);
}
if (request.status === 401 || json.includes('OAuthException')) {
throw new RefreshToken(identifier, json, options.body!);
}