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 @@ -386,9 +386,13 @@ export abstract class APIClient {
386
386
delete reqHeaders [ 'content-type' ] ;
387
387
}
388
388
389
- // Don't set the retry count header if it was already set or removed by the caller. We check `headers`,
390
- // which can contain nulls, instead of `reqHeaders` to account for the removal case.
391
- if ( getHeader ( headers , 'x-stainless-retry-count' ) === undefined ) {
389
+ // Don't set the retry count header if it was already set or removed through default headers or by the
390
+ // caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
391
+ // account for the removal case.
392
+ if (
393
+ getHeader ( defaultHeaders , 'x-stainless-retry-count' ) === undefined &&
394
+ getHeader ( headers , 'x-stainless-retry-count' ) === undefined
395
+ ) {
392
396
reqHeaders [ 'x-stainless-retry-count' ] = String ( retryCount ) ;
393
397
}
394
398
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