Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit dfc0dcd

Browse files
committed
fix: request throttling
1 parent f968524 commit dfc0dcd

1 file changed

Lines changed: 34 additions & 14 deletions

File tree

src/utils/api.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,42 @@ module.exports = {
1313
spaceId: null,
1414
region: '',
1515

16-
getClient () {
17-
const { region } = creds.get()
16+
getClient: (function () {
17+
let client, accessToken, oauthToken, region, credsRegion
18+
19+
return function getClient() {
20+
const { region: _credsRegion } = creds.get()
21+
22+
// cache the client if the params are the same
23+
// this is needed so request throttling works properly
24+
if (
25+
client
26+
&& accessToken === this.accessToken
27+
&& oauthToken === this.oauthToken
28+
&& region === this.region
29+
&& credsRegion === _credsRegion) {
30+
return client
31+
}
1832

19-
try {
20-
return new Storyblok({
21-
accessToken: this.accessToken,
22-
oauthToken: this.oauthToken,
23-
region: this.region,
24-
headers: {
25-
...DEFAULT_AGENT
26-
}
27-
}, this.apiSwitcher(region))
28-
} catch (error) {
29-
throw new Error(error)
33+
accessToken = this.accessToken
34+
oauthToken = this.oauthToken
35+
region = this.region
36+
credsRegion = _credsRegion
37+
38+
try {
39+
return client = new Storyblok({
40+
accessToken,
41+
oauthToken,
42+
region,
43+
headers: {
44+
...DEFAULT_AGENT
45+
}
46+
}, this.apiSwitcher(credsRegion))
47+
} catch (error) {
48+
throw new Error(error)
49+
}
3050
}
31-
},
51+
})(),
3252

3353
getPath (path) {
3454
if (this.spaceId) {

0 commit comments

Comments
 (0)