From e3bd1df158d272190ae90aef48cea6ee17142da2 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Wed, 27 Nov 2024 17:09:47 +0700 Subject: [PATCH] feat: if there is a rate limit, wait two seconds and try again --- .../nestjs-libraries/src/integrations/social.abstract.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts index b30497f6..e9fe0480 100644 --- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts +++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts @@ -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 { 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!); }