@@ -288,7 +288,10 @@ export abstract class APIClient {
288
288
return null ;
289
289
}
290
290
291
- buildRequest < Req > ( options : FinalRequestOptions < Req > ) : { req : RequestInit ; url : string ; timeout : number } {
291
+ buildRequest < Req > (
292
+ options : FinalRequestOptions < Req > ,
293
+ { retryCount = 0 } : { retryCount ?: number } = { } ,
294
+ ) : { req : RequestInit ; url : string ; timeout : number } {
292
295
const { method, path, query, headers : headers = { } } = options ;
293
296
294
297
const body =
@@ -320,7 +323,7 @@ export abstract class APIClient {
320
323
headers [ this . idempotencyHeader ] = options . idempotencyKey ;
321
324
}
322
325
323
- const reqHeaders = this . buildHeaders ( { options, headers, contentLength } ) ;
326
+ const reqHeaders = this . buildHeaders ( { options, headers, contentLength, retryCount } ) ;
324
327
325
328
const req : RequestInit = {
326
329
method,
@@ -339,10 +342,12 @@ export abstract class APIClient {
339
342
options,
340
343
headers,
341
344
contentLength,
345
+ retryCount,
342
346
} : {
343
347
options : FinalRequestOptions ;
344
348
headers : Record < string , string | null | undefined > ;
345
349
contentLength : string | null | undefined ;
350
+ retryCount : number ;
346
351
} ) : Record < string , string > {
347
352
const reqHeaders : Record < string , string > = { } ;
348
353
if ( contentLength ) {
@@ -358,6 +363,8 @@ export abstract class APIClient {
358
363
delete reqHeaders [ 'content-type' ] ;
359
364
}
360
365
366
+ reqHeaders [ 'x-stainless-retry-count' ] = String ( retryCount ) ;
367
+
361
368
this . validateHeaders ( reqHeaders , headers ) ;
362
369
363
370
return reqHeaders ;
@@ -409,13 +416,14 @@ export abstract class APIClient {
409
416
retriesRemaining : number | null ,
410
417
) : Promise < APIResponseProps > {
411
418
const options = await optionsInput ;
419
+ const maxRetries = options . maxRetries ?? this . maxRetries ;
412
420
if ( retriesRemaining == null ) {
413
- retriesRemaining = options . maxRetries ?? this . maxRetries ;
421
+ retriesRemaining = maxRetries ;
414
422
}
415
423
416
424
await this . prepareOptions ( options ) ;
417
425
418
- const { req, url, timeout } = this . buildRequest ( options ) ;
426
+ const { req, url, timeout } = this . buildRequest ( options , { retryCount : maxRetries - retriesRemaining } ) ;
419
427
420
428
await this . prepareRequest ( req , { url, options } ) ;
421
429
0 commit comments