File tree 2 files changed +40
-3
lines changed
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 @@ -295,6 +295,39 @@ describe('retries', () => {
295
295
expect ( capturedRequest ! . headers as Headers ) . not . toHaveProperty ( 'x-stainless-retry-count' ) ;
296
296
} ) ;
297
297
298
+ test ( 'omit retry count header by default' , async ( ) => {
299
+ let count = 0 ;
300
+ let capturedRequest : RequestInit | undefined ;
301
+ const testFetch = async ( url : RequestInfo , init : RequestInit = { } ) : Promise < Response > => {
302
+ count ++ ;
303
+ if ( count <= 2 ) {
304
+ return new Response ( undefined , {
305
+ status : 429 ,
306
+ headers : {
307
+ 'Retry-After' : '0.1' ,
308
+ } ,
309
+ } ) ;
310
+ }
311
+ capturedRequest = init ;
312
+ return new Response ( JSON . stringify ( { a : 1 } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
313
+ } ;
314
+ const client = new OpenAI ( {
315
+ apiKey : 'My API Key' ,
316
+ fetch : testFetch ,
317
+ maxRetries : 4 ,
318
+ defaultHeaders : { 'X-Stainless-Retry-Count' : null } ,
319
+ } ) ;
320
+
321
+ expect (
322
+ await client . request ( {
323
+ path : '/foo' ,
324
+ method : 'get' ,
325
+ } ) ,
326
+ ) . toEqual ( { a : 1 } ) ;
327
+
328
+ expect ( capturedRequest ! . headers as Headers ) . not . toHaveProperty ( 'x-stainless-retry-count' ) ;
329
+ } ) ;
330
+
298
331
test ( 'overwrite retry count header' , async ( ) => {
299
332
let count = 0 ;
300
333
let capturedRequest : RequestInit | undefined ;
You can’t perform that action at this time.
0 commit comments