From e974e4de886969f8339968071e7d6c034b163fc0 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Thu, 18 Jan 2024 09:25:26 +0100 Subject: [PATCH] fix: update the retry function to check the response status correctly --- src/index.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index fb9d1398..62afc638 100755 --- a/src/index.ts +++ b/src/index.ts @@ -592,18 +592,6 @@ class Storyblok { return new Promise(async (resolve, reject) => { try { const res = await this.throttle('get', url, params) - if (res.status === 429) { - retries = retries ? retries + 1 : 0 - - if (retries < this.maxRetries) { - console.log(`Hit rate limit. Retrying in ${retries} seconds.`) - await this.helpers.delay(1000 * retries) - return this.cacheResponse(url, params, retries) - .then(resolve) - .catch(reject) - } - } - if (res.status !== 200) { return reject(res) } @@ -640,6 +628,17 @@ class Storyblok { return resolve(response) } catch (error: Error | any) { + if (error.response && error.status === 429) { + retries = retries ? retries + 1 : 0 + + if (retries < this.maxRetries) { + console.log(`Hit rate limit. Retrying in ${retries} seconds.`) + await this.helpers.delay(1000 * retries) + return this.cacheResponse(url, params, retries) + .then(resolve) + .catch(reject) + } + } reject(error) } })