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

Commit 505356a

Browse files
authored
Merge pull request #72 from cashy-at/fix/request-throttling
Fix request throttling
2 parents 8a3c914 + dfc0dcd commit 505356a

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
@@ -14,22 +14,42 @@ export default {
1414
spaceId: null,
1515
region: '',
1616

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

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

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

0 commit comments

Comments
 (0)