@@ -38,6 +38,7 @@ export type ClerkBackendApiResponse<T> =
38
38
clerkTraceId ?: string ;
39
39
status ?: number ;
40
40
statusText ?: string ;
41
+ retryAfter ?: number ;
41
42
} ;
42
43
43
44
export type RequestFunction = ReturnType < typeof buildRequest > ;
@@ -135,6 +136,7 @@ export function buildRequest(options: BuildRequestOptions) {
135
136
status : res ?. status ,
136
137
statusText : res ?. statusText ,
137
138
clerkTraceId : getTraceId ( responseBody , res ?. headers ) ,
139
+ retryAfter : getRetryAfter ( res ?. headers ) ,
138
140
} ;
139
141
}
140
142
@@ -162,6 +164,7 @@ export function buildRequest(options: BuildRequestOptions) {
162
164
status : res ?. status ,
163
165
statusText : res ?. statusText ,
164
166
clerkTraceId : getTraceId ( err , res ?. headers ) ,
167
+ retryAfter : getRetryAfter ( res ?. headers ) ,
165
168
} ;
166
169
}
167
170
} ;
@@ -180,6 +183,16 @@ function getTraceId(data: unknown, headers?: Headers): string {
180
183
return cfRay || '' ;
181
184
}
182
185
186
+ function getRetryAfter ( headers ?: Headers ) : number | undefined {
187
+ const retryAfter = headers ?. get ( 'Retry-After' ) ;
188
+ if ( ! retryAfter ) return ;
189
+
190
+ const value = parseInt ( retryAfter , 10 ) ;
191
+ if ( isNaN ( value ) ) return ;
192
+
193
+ return value ;
194
+ }
195
+
183
196
function parseErrors ( data : unknown ) : ClerkAPIError [ ] {
184
197
if ( ! ! data && typeof data === 'object' && 'errors' in data ) {
185
198
const errors = data . errors as ClerkAPIErrorJSON [ ] ;
@@ -193,7 +206,7 @@ type LegacyRequestFunction = <T>(requestOptions: ClerkBackendApiRequestOptions)
193
206
// TODO(dimkl): Will be probably be dropped in next major version
194
207
function withLegacyRequestReturn ( cb : any ) : LegacyRequestFunction {
195
208
return async ( ...args ) => {
196
- const { data, errors, totalCount, status, statusText, clerkTraceId } = await cb ( ...args ) ;
209
+ const { data, errors, totalCount, status, statusText, clerkTraceId, retryAfter } = await cb ( ...args ) ;
197
210
if ( errors ) {
198
211
// instead of passing `data: errors`, we have set the `error.errors` because
199
212
// the errors returned from callback is already parsed and passing them as `data`
@@ -202,6 +215,7 @@ function withLegacyRequestReturn(cb: any): LegacyRequestFunction {
202
215
data : [ ] ,
203
216
status,
204
217
clerkTraceId,
218
+ retryAfter,
205
219
} ) ;
206
220
error . errors = errors ;
207
221
throw error ;
0 commit comments