Skip to content

Commit

Permalink
feat: if there is a rate limit, wait two seconds and try again
Browse files Browse the repository at this point in the history
  • Loading branch information
nevo-david committed Nov 27, 2024
1 parent 7f27f53 commit b01aab0
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { timer } from '@gitroom/helpers/utils/timer';

export class RefreshToken {
constructor(
public identifier: string,
Expand All @@ -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) {
Expand All @@ -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!);
}
Expand Down

0 comments on commit b01aab0

Please sign in to comment.