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