Skip to content

Commit 29a8627

Browse files
feat(client): send X-Stainless-Timeout header (#1299)
1 parent 7cf2a85 commit 29a8627

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/core.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ export abstract class APIClient {
315315
options: FinalRequestOptions<Req>,
316316
{ retryCount = 0 }: { retryCount?: number } = {},
317317
): { req: RequestInit; url: string; timeout: number } {
318+
options = { ...options };
318319
const { method, path, query, headers: headers = {} } = options;
319320

320321
const body =
@@ -327,9 +328,9 @@ export abstract class APIClient {
327328

328329
const url = this.buildURL(path!, query);
329330
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
330-
const timeout = options.timeout ?? this.timeout;
331+
options.timeout = options.timeout ?? this.timeout;
331332
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
332-
const minAgentTimeout = timeout + 1000;
333+
const minAgentTimeout = options.timeout + 1000;
333334
if (
334335
typeof (httpAgent as any)?.options?.timeout === 'number' &&
335336
minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)
@@ -358,7 +359,7 @@ export abstract class APIClient {
358359
signal: options.signal ?? null,
359360
};
360361

361-
return { req, url, timeout };
362+
return { req, url, timeout: options.timeout };
362363
}
363364

364365
private buildHeaders({
@@ -386,15 +387,22 @@ export abstract class APIClient {
386387
delete reqHeaders['content-type'];
387388
}
388389

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.
390+
// Don't set theses headers if they were already set or removed through default headers or by the caller.
391+
// We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account
392+
// for the removal case.
392393
if (
393394
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
394395
getHeader(headers, 'x-stainless-retry-count') === undefined
395396
) {
396397
reqHeaders['x-stainless-retry-count'] = String(retryCount);
397398
}
399+
if (
400+
getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&
401+
getHeader(headers, 'x-stainless-timeout') === undefined &&
402+
options.timeout
403+
) {
404+
reqHeaders['x-stainless-timeout'] = String(options.timeout);
405+
}
398406

399407
this.validateHeaders(reqHeaders, headers);
400408

0 commit comments

Comments
 (0)