File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -360,7 +360,8 @@ export class SpotifyClient {
360360 async request < T > (
361361 method : string ,
362362 path : string ,
363- options : { params ?: Record < string , string | number > ; body ?: unknown } = { }
363+ options : { params ?: Record < string , string | number > ; body ?: unknown } = { } ,
364+ retries = 3
364365 ) : Promise < T > {
365366 const token = await this . getValidAccessToken ( ) ;
366367
@@ -384,6 +385,12 @@ export class SpotifyClient {
384385 ...( options . body ? { body : JSON . stringify ( options . body ) } : { } ) ,
385386 } ) ;
386387
388+ if ( res . status === 429 && retries > 0 ) {
389+ const retryAfter = Number ( res . headers . get ( "Retry-After" ) ?? 1 ) ;
390+ await new Promise ( resolve => setTimeout ( resolve , retryAfter * 1000 ) ) ;
391+ return this . request < T > ( method , path , options , retries - 1 ) ;
392+ }
393+
387394 if ( res . status === 204 ) return undefined as unknown as T ;
388395
389396 const data = await res . json ( ) . catch ( ( ) => null ) ;
You can’t perform that action at this time.
0 commit comments