Skip to content

Commit 3177aaa

Browse files
committed
fix: 🐛 implement retry after on requests for spotify
1 parent 3c01c18 commit 3177aaa

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/util/spotify.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)