Skip to content

Commit

Permalink
Merge pull request #72 from cashy-at/fix/request-throttling
Browse files Browse the repository at this point in the history
Fix request throttling
  • Loading branch information
alvarosabu authored May 23, 2024
2 parents 8a3c914 + dfc0dcd commit 505356a
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,42 @@ export default {
spaceId: null,
region: '',

getClient () {
const { region } = creds.get()
getClient: (function () {
let client, accessToken, oauthToken, region, credsRegion

return function getClient() {
const { region: _credsRegion } = creds.get()

// cache the client if the params are the same
// this is needed so request throttling works properly
if (
client
&& accessToken === this.accessToken
&& oauthToken === this.oauthToken
&& region === this.region
&& credsRegion === _credsRegion) {
return client
}

try {
return new Storyblok({
accessToken: this.accessToken,
oauthToken: this.oauthToken,
region: this.region,
headers: {
...DEFAULT_AGENT
}
}, this.apiSwitcher(region))
} catch (error) {
throw new Error(error)
accessToken = this.accessToken
oauthToken = this.oauthToken
region = this.region
credsRegion = _credsRegion

try {
return client = new Storyblok({
accessToken,
oauthToken,
region,
headers: {
...DEFAULT_AGENT
}
}, this.apiSwitcher(credsRegion))
} catch (error) {
throw new Error(error)
}
}
},
})(),

getPath (path) {
if (this.spaceId) {
Expand Down

0 comments on commit 505356a

Please sign in to comment.