Skip to content

Commit bddd452

Browse files
feat(client): send X-Stainless-Timeout header (#542)
1 parent 81fe307 commit bddd452

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
@@ -268,6 +268,7 @@ export abstract class APIClient {
268268
options: FinalRequestOptions<Req>,
269269
{ retryCount = 0 }: { retryCount?: number } = {},
270270
): { req: RequestInit; url: string; timeout: number } {
271+
options = { ...options };
271272
const { method, path, query, headers: headers = {} } = options;
272273

273274
const body =
@@ -278,9 +279,9 @@ export abstract class APIClient {
278279

279280
const url = this.buildURL(path!, query);
280281
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
281-
const timeout = options.timeout ?? this.timeout;
282+
options.timeout = options.timeout ?? this.timeout;
282283
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
283-
const minAgentTimeout = timeout + 1000;
284+
const minAgentTimeout = options.timeout + 1000;
284285
if (
285286
typeof (httpAgent as any)?.options?.timeout === 'number' &&
286287
minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)
@@ -309,7 +310,7 @@ export abstract class APIClient {
309310
signal: options.signal ?? null,
310311
};
311312

312-
return { req, url, timeout };
313+
return { req, url, timeout: options.timeout };
313314
}
314315

315316
private buildHeaders({
@@ -337,15 +338,22 @@ export abstract class APIClient {
337338
delete reqHeaders['content-type'];
338339
}
339340

340-
// Don't set the retry count header if it was already set or removed through default headers or by the
341-
// caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
342-
// account for the removal case.
341+
// Don't set theses headers if they were already set or removed through default headers or by the caller.
342+
// We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account
343+
// for the removal case.
343344
if (
344345
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
345346
getHeader(headers, 'x-stainless-retry-count') === undefined
346347
) {
347348
reqHeaders['x-stainless-retry-count'] = String(retryCount);
348349
}
350+
if (
351+
getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&
352+
getHeader(headers, 'x-stainless-timeout') === undefined &&
353+
options.timeout
354+
) {
355+
reqHeaders['x-stainless-timeout'] = String(options.timeout);
356+
}
349357

350358
this.validateHeaders(reqHeaders, headers);
351359

0 commit comments

Comments
 (0)