@@ -7,7 +7,16 @@ type ThrottleType = 'auto' | string
7
7
8
8
const PERCENTAGE_REGEX = / (?< value > \d + ) ( % ) /
9
9
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 ) {
11
20
let limit = max
12
21
13
22
if ( PERCENTAGE_REGEX . test ( type ) ) {
@@ -46,14 +55,15 @@ export default (axiosInstance: AxiosInstance, type: ThrottleType | number = 'aut
46
55
47
56
const responseInterceptorId = axiosInstance . interceptors . response . use (
48
57
( response ) => {
58
+ // If we haven't yet calculated the limit based on the headers, do so now
49
59
if (
50
60
! isCalculated &&
51
61
isString ( type ) &&
52
62
( type === 'auto' || PERCENTAGE_REGEX . test ( type ) ) &&
53
63
response . headers &&
54
- response . headers [ 'x-contentful-ratelimit-second-limit' ]
64
+ response . headers [ HEADERS . RATE_LIMIT ]
55
65
) {
56
- const rawLimit = parseInt ( response . headers [ 'x-contentful-ratelimit-second-limit' ] )
66
+ const rawLimit = parseInt ( response . headers [ HEADERS . RATE_LIMIT ] )
57
67
const nextLimit = calculateLimit ( type , rawLimit )
58
68
59
69
if ( nextLimit !== limit ) {
0 commit comments