Skip to content

Commit

Permalink
Merge pull request #811 from storyblok/fix/retries-loop-bug
Browse files Browse the repository at this point in the history
Fix(SHAPE-2460):retries loop bug
  • Loading branch information
gustavomelki authored Apr 19, 2024
2 parents b6abea2 + 4b3d4d9 commit 5cdb067
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Version = ObjectValues<typeof VERSION>
class Storyblok {
private client: SbFetch
private maxRetries: number
private retriesDelay: number
private throttle: ThrottleFn
private accessToken: string
private cache: ISbCache
Expand Down Expand Up @@ -137,7 +138,8 @@ class Storyblok {
this.setComponentResolver(config.componentResolver)
}

this.maxRetries = config.maxRetries || 5
this.maxRetries = config.maxRetries || 10
this.retriesDelay = 300
this.throttle = throttledQueue(this.throttledRequest, rateLimit, 1000)
this.accessToken = config.accessToken || ''
this.relations = {} as RelationsType
Expand Down Expand Up @@ -585,10 +587,6 @@ class Storyblok {
params: ISbStoriesParams,
retries?: number
): Promise<ISbResult> {
if (typeof retries === 'undefined' || !retries) {
retries = 0
}

const cacheKey = this.helpers.stringify({ url: url, params: params })
const provider = this.cacheProvider()

Expand Down Expand Up @@ -643,11 +641,11 @@ class Storyblok {
return resolve(response)
} catch (error: Error | any) {
if (error.response && error.status === 429) {
retries = retries ? retries + 1 : 0
retries = typeof retries === 'undefined' ? 0 : retries + 1

if (retries < this.maxRetries) {
console.log(`Hit rate limit. Retrying in ${retries} seconds.`)
await this.helpers.delay(1000 * retries)
console.log(`Hit rate limit. Retrying in ${this.retriesDelay/1000} seconds.`)
await this.helpers.delay(this.retriesDelay)
return this.cacheResponse(url, params, retries)
.then(resolve)
.catch(reject)
Expand Down

0 comments on commit 5cdb067

Please sign in to comment.