File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -365,9 +365,13 @@ export abstract class APIClient {
365
365
delete reqHeaders [ 'content-type' ] ;
366
366
}
367
367
368
- // Don't set the retry count header if it was already set or removed by the caller. We check `headers`,
369
- // which can contain nulls, instead of `reqHeaders` to account for the removal case.
370
- if ( getHeader ( headers , 'x-stainless-retry-count' ) === undefined ) {
368
+ // Don't set the retry count header if it was already set or removed through default headers or by the
369
+ // caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
370
+ // account for the removal case.
371
+ if (
372
+ getHeader ( defaultHeaders , 'x-stainless-retry-count' ) === undefined &&
373
+ getHeader ( headers , 'x-stainless-retry-count' ) === undefined
374
+ ) {
371
375
reqHeaders [ 'x-stainless-retry-count' ] = String ( retryCount ) ;
372
376
}
373
377
Original file line number Diff line number Diff line change @@ -301,6 +301,39 @@ describe('retries', () => {
301
301
expect ( capturedRequest ! . headers as Headers ) . not . toHaveProperty ( 'x-stainless-retry-count' ) ;
302
302
} ) ;
303
303
304
+ test ( 'omit retry count header by default' , async ( ) => {
305
+ let count = 0 ;
306
+ let capturedRequest : RequestInit | undefined ;
307
+ const testFetch = async ( url : RequestInfo , init : RequestInit = { } ) : Promise < Response > => {
308
+ count ++ ;
309
+ if ( count <= 2 ) {
310
+ return new Response ( undefined , {
311
+ status : 429 ,
312
+ headers : {
313
+ 'Retry-After' : '0.1' ,
314
+ } ,
315
+ } ) ;
316
+ }
317
+ capturedRequest = init ;
318
+ return new Response ( JSON . stringify ( { a : 1 } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
319
+ } ;
320
+ const client = new Anthropic ( {
321
+ apiKey : 'my-anthropic-api-key' ,
322
+ fetch : testFetch ,
323
+ maxRetries : 4 ,
324
+ defaultHeaders : { 'X-Stainless-Retry-Count' : null } ,
325
+ } ) ;
326
+
327
+ expect (
328
+ await client . request ( {
329
+ path : '/foo' ,
330
+ method : 'get' ,
331
+ } ) ,
332
+ ) . toEqual ( { a : 1 } ) ;
333
+
334
+ expect ( capturedRequest ! . headers as Headers ) . not . toHaveProperty ( 'x-stainless-retry-count' ) ;
335
+ } ) ;
336
+
304
337
test ( 'overwrite retry count header' , async ( ) => {
305
338
let count = 0 ;
306
339
let capturedRequest : RequestInit | undefined ;
You can’t perform that action at this time.
0 commit comments