@@ -280,6 +280,7 @@ export abstract class APIClient {
280280 options : FinalRequestOptions < Req > ,
281281 { retryCount = 0 } : { retryCount ?: number } = { } ,
282282 ) : { req : RequestInit ; url : string ; timeout : number } {
283+ options = { ...options } ;
283284 const { method, path, query, headers : headers = { } } = options ;
284285
285286 const body =
@@ -292,9 +293,9 @@ export abstract class APIClient {
292293
293294 const url = this . buildURL ( path ! , query ) ;
294295 if ( 'timeout' in options ) validatePositiveInteger ( 'timeout' , options . timeout ) ;
295- const timeout = options . timeout ?? this . timeout ;
296+ options . timeout = options . timeout ?? this . timeout ;
296297 const httpAgent = options . httpAgent ?? this . httpAgent ?? getDefaultAgent ( url ) ;
297- const minAgentTimeout = timeout + 1000 ;
298+ const minAgentTimeout = options . timeout + 1000 ;
298299 if (
299300 typeof ( httpAgent as any ) ?. options ?. timeout === 'number' &&
300301 minAgentTimeout > ( ( httpAgent as any ) . options . timeout ?? 0 )
@@ -323,7 +324,7 @@ export abstract class APIClient {
323324 signal : options . signal ?? null ,
324325 } ;
325326
326- return { req, url, timeout } ;
327+ return { req, url, timeout : options . timeout } ;
327328 }
328329
329330 private buildHeaders ( {
@@ -351,15 +352,22 @@ export abstract class APIClient {
351352 delete reqHeaders [ 'content-type' ] ;
352353 }
353354
354- // Don't set the retry count header if it was already set or removed through default headers or by the
355- // caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
356- // account for the removal case.
355+ // Don't set theses headers if they were already set or removed through default headers or by the caller.
356+ // We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account
357+ // for the removal case.
357358 if (
358359 getHeader ( defaultHeaders , 'x-stainless-retry-count' ) === undefined &&
359360 getHeader ( headers , 'x-stainless-retry-count' ) === undefined
360361 ) {
361362 reqHeaders [ 'x-stainless-retry-count' ] = String ( retryCount ) ;
362363 }
364+ if (
365+ getHeader ( defaultHeaders , 'x-stainless-timeout' ) === undefined &&
366+ getHeader ( headers , 'x-stainless-timeout' ) === undefined &&
367+ options . timeout
368+ ) {
369+ reqHeaders [ 'x-stainless-timeout' ] = String ( options . timeout ) ;
370+ }
363371
364372 this . validateHeaders ( reqHeaders , headers ) ;
365373
0 commit comments