@@ -38,6 +38,7 @@ export type ClerkBackendApiResponse<T> =
3838 clerkTraceId ?: string ;
3939 status ?: number ;
4040 statusText ?: string ;
41+ retryAfter ?: number ;
4142 } ;
4243
4344export type RequestFunction = ReturnType < typeof buildRequest > ;
@@ -135,6 +136,7 @@ export function buildRequest(options: BuildRequestOptions) {
135136 status : res ?. status ,
136137 statusText : res ?. statusText ,
137138 clerkTraceId : getTraceId ( responseBody , res ?. headers ) ,
139+ retryAfter : getRetryAfter ( res ?. headers ) ,
138140 } ;
139141 }
140142
@@ -162,6 +164,7 @@ export function buildRequest(options: BuildRequestOptions) {
162164 status : res ?. status ,
163165 statusText : res ?. statusText ,
164166 clerkTraceId : getTraceId ( err , res ?. headers ) ,
167+ retryAfter : getRetryAfter ( res ?. headers ) ,
165168 } ;
166169 }
167170 } ;
@@ -180,6 +183,16 @@ function getTraceId(data: unknown, headers?: Headers): string {
180183 return cfRay || '' ;
181184}
182185
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+
183196function parseErrors ( data : unknown ) : ClerkAPIError [ ] {
184197 if ( ! ! data && typeof data === 'object' && 'errors' in data ) {
185198 const errors = data . errors as ClerkAPIErrorJSON [ ] ;
@@ -193,7 +206,7 @@ type LegacyRequestFunction = <T>(requestOptions: ClerkBackendApiRequestOptions)
193206// TODO(dimkl): Will be probably be dropped in next major version
194207function withLegacyRequestReturn ( cb : any ) : LegacyRequestFunction {
195208 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 ) ;
197210 if ( errors ) {
198211 // instead of passing `data: errors`, we have set the `error.errors` because
199212 // the errors returned from callback is already parsed and passing them as `data`
@@ -202,6 +215,7 @@ function withLegacyRequestReturn(cb: any): LegacyRequestFunction {
202215 data : [ ] ,
203216 status,
204217 clerkTraceId,
218+ retryAfter,
205219 } ) ;
206220 error . errors = errors ;
207221 throw error ;
0 commit comments