Skip to content

Commit 6c9888c

Browse files
feat(client): send X-Stainless-Timeout header (#1299)
1 parent 5632458 commit 6c9888c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/core.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ export abstract class APIClient {
294294
options: FinalRequestOptions<Req>,
295295
{ retryCount = 0 }: { retryCount?: number } = {},
296296
): { req: RequestInit; url: string; timeout: number } {
297+
options = { ...options };
297298
const { method, path, query, headers: headers = {} } = options;
298299

299300
const body =
@@ -306,9 +307,9 @@ export abstract class APIClient {
306307

307308
const url = this.buildURL(path!, query);
308309
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
309-
const timeout = options.timeout ?? this.timeout;
310+
options.timeout = options.timeout ?? this.timeout;
310311
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
311-
const minAgentTimeout = timeout + 1000;
312+
const minAgentTimeout = options.timeout + 1000;
312313
if (
313314
typeof (httpAgent as any)?.options?.timeout === 'number' &&
314315
minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)
@@ -337,7 +338,7 @@ export abstract class APIClient {
337338
signal: options.signal ?? null,
338339
};
339340

340-
return { req, url, timeout };
341+
return { req, url, timeout: options.timeout };
341342
}
342343

343344
private buildHeaders({
@@ -365,15 +366,22 @@ export abstract class APIClient {
365366
delete reqHeaders['content-type'];
366367
}
367368

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.
369+
// Don't set theses headers if they were already set or removed through default headers or by the caller.
370+
// We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account
371+
// for the removal case.
371372
if (
372373
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
373374
getHeader(headers, 'x-stainless-retry-count') === undefined
374375
) {
375376
reqHeaders['x-stainless-retry-count'] = String(retryCount);
376377
}
378+
if (
379+
getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&
380+
getHeader(headers, 'x-stainless-timeout') === undefined &&
381+
options.timeout
382+
) {
383+
reqHeaders['x-stainless-timeout'] = String(options.timeout);
384+
}
377385

378386
this.validateHeaders(reqHeaders, headers);
379387

0 commit comments

Comments
 (0)