@@ -8,7 +8,16 @@ type ThrottleType = 'auto' | string
88
99const PERCENTAGE_REGEX = / (?< value > \d + ) ( % ) /
1010
11- function calculateLimit ( type : ThrottleType , max = 7 ) {
11+ const HEADERS = {
12+ // @desc The maximum amount of requests which can be made in a second.
13+ RATE_LIMIT : 'x-contentful-ratelimit-second-limit' ,
14+ // @desc The number of seconds until the next request can be made.
15+ RATE_LIMIT_RESET : 'x-contentful-ratelimit-second-reset' ,
16+ // @desc The remaining amount of requests which can be made until the next secondly reset.
17+ RATE_LIMIT_REMAINING : 'x-contentful-ratelimit-second-remaining' ,
18+ } as const
19+
20+ export function calculateLimit ( type : ThrottleType , max = 7 ) {
1221 let limit = max
1322
1423 if ( PERCENTAGE_REGEX . test ( type ) ) {
@@ -47,14 +56,15 @@ export default (axiosInstance: AxiosInstance, type: ThrottleType | number = 'aut
4756
4857 const responseInterceptorId = axiosInstance . interceptors . response . use (
4958 ( response ) => {
59+ // If we haven't yet calculated the limit based on the headers, do so now
5060 if (
5161 ! isCalculated &&
5262 isString ( type ) &&
5363 ( type === 'auto' || PERCENTAGE_REGEX . test ( type ) ) &&
5464 response . headers &&
55- response . headers [ 'x-contentful-ratelimit-second-limit' ]
65+ response . headers [ HEADERS . RATE_LIMIT ]
5666 ) {
57- const rawLimit = parseInt ( response . headers [ 'x-contentful-ratelimit-second-limit' ] )
67+ const rawLimit = parseInt ( response . headers [ HEADERS . RATE_LIMIT ] )
5868 const nextLimit = calculateLimit ( type , rawLimit )
5969
6070 if ( nextLimit !== limit ) {
0 commit comments