@@ -308,7 +308,10 @@ export abstract class APIClient {
308
308
return null ;
309
309
}
310
310
311
- buildRequest < Req > ( options : FinalRequestOptions < Req > ) : { req : RequestInit ; url : string ; timeout : number } {
311
+ buildRequest < Req > (
312
+ options : FinalRequestOptions < Req > ,
313
+ { retryCount = 0 } : { retryCount ?: number } = { } ,
314
+ ) : { req : RequestInit ; url : string ; timeout : number } {
312
315
const { method, path, query, headers : headers = { } } = options ;
313
316
314
317
const body =
@@ -340,7 +343,7 @@ export abstract class APIClient {
340
343
headers [ this . idempotencyHeader ] = options . idempotencyKey ;
341
344
}
342
345
343
- const reqHeaders = this . buildHeaders ( { options, headers, contentLength } ) ;
346
+ const reqHeaders = this . buildHeaders ( { options, headers, contentLength, retryCount } ) ;
344
347
345
348
const req : RequestInit = {
346
349
method,
@@ -359,10 +362,12 @@ export abstract class APIClient {
359
362
options,
360
363
headers,
361
364
contentLength,
365
+ retryCount,
362
366
} : {
363
367
options : FinalRequestOptions ;
364
368
headers : Record < string , string | null | undefined > ;
365
369
contentLength : string | null | undefined ;
370
+ retryCount : number ;
366
371
} ) : Record < string , string > {
367
372
const reqHeaders : Record < string , string > = { } ;
368
373
if ( contentLength ) {
@@ -378,6 +383,8 @@ export abstract class APIClient {
378
383
delete reqHeaders [ 'content-type' ] ;
379
384
}
380
385
386
+ reqHeaders [ 'x-stainless-retry-count' ] = String ( retryCount ) ;
387
+
381
388
this . validateHeaders ( reqHeaders , headers ) ;
382
389
383
390
return reqHeaders ;
@@ -429,13 +436,14 @@ export abstract class APIClient {
429
436
retriesRemaining : number | null ,
430
437
) : Promise < APIResponseProps > {
431
438
const options = await optionsInput ;
439
+ const maxRetries = options . maxRetries ?? this . maxRetries ;
432
440
if ( retriesRemaining == null ) {
433
- retriesRemaining = options . maxRetries ?? this . maxRetries ;
441
+ retriesRemaining = maxRetries ;
434
442
}
435
443
436
444
await this . prepareOptions ( options ) ;
437
445
438
- const { req, url, timeout } = this . buildRequest ( options ) ;
446
+ const { req, url, timeout } = this . buildRequest ( options , { retryCount : maxRetries - retriesRemaining } ) ;
439
447
440
448
await this . prepareRequest ( req , { url, options } ) ;
441
449
0 commit comments