Skip to content

Commit 802475b

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
chore(internal): codegen related update (#494)
1 parent f202eef commit 802475b

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/core.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ export abstract class APIClient {
335335
delete reqHeaders['content-type'];
336336
}
337337

338-
reqHeaders['x-stainless-retry-count'] = String(retryCount);
338+
// Don't set the retry count header if it was already set or removed by the caller. We check `headers`,
339+
// which can contain nulls, instead of `reqHeaders` to account for the removal case.
340+
if (getHeader(headers, 'x-stainless-retry-count') === undefined) {
341+
reqHeaders['x-stainless-retry-count'] = String(retryCount);
342+
}
339343

340344
this.validateHeaders(reqHeaders, headers);
341345

@@ -1109,7 +1113,15 @@ export const isHeadersProtocol = (headers: any): headers is HeadersProtocol => {
11091113
return typeof headers?.get === 'function';
11101114
};
11111115

1112-
export const getRequiredHeader = (headers: HeadersLike, header: string): string => {
1116+
export const getRequiredHeader = (headers: HeadersLike | Headers, header: string): string => {
1117+
const foundHeader = getHeader(headers, header);
1118+
if (foundHeader === undefined) {
1119+
throw new Error(`Could not find ${header} header`);
1120+
}
1121+
return foundHeader;
1122+
};
1123+
1124+
export const getHeader = (headers: HeadersLike | Headers, header: string): string | undefined => {
11131125
const lowerCasedHeader = header.toLowerCase();
11141126
if (isHeadersProtocol(headers)) {
11151127
// to deal with the case where the header looks like Stainless-Event-Id
@@ -1135,7 +1147,7 @@ export const getRequiredHeader = (headers: HeadersLike, header: string): string
11351147
}
11361148
}
11371149

1138-
throw new Error(`Could not find ${header} header`);
1150+
return undefined;
11391151
};
11401152

11411153
/**

tests/index.test.ts

+68
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,74 @@ describe('retries', () => {
298298
expect(count).toEqual(3);
299299
});
300300

301+
test('omit retry count header', async () => {
302+
let count = 0;
303+
let capturedRequest: RequestInit | undefined;
304+
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
305+
count++;
306+
if (count <= 2) {
307+
return new Response(undefined, {
308+
status: 429,
309+
headers: {
310+
'Retry-After': '0.1',
311+
},
312+
});
313+
}
314+
capturedRequest = init;
315+
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
316+
};
317+
const client = new Mux({
318+
tokenId: 'my token id',
319+
tokenSecret: 'my secret',
320+
fetch: testFetch,
321+
maxRetries: 4,
322+
});
323+
324+
expect(
325+
await client.request({
326+
path: '/foo',
327+
method: 'get',
328+
headers: { 'X-Stainless-Retry-Count': null },
329+
}),
330+
).toEqual({ a: 1 });
331+
332+
expect(capturedRequest!.headers as Headers).not.toHaveProperty('x-stainless-retry-count');
333+
});
334+
335+
test('overwrite retry count header', async () => {
336+
let count = 0;
337+
let capturedRequest: RequestInit | undefined;
338+
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
339+
count++;
340+
if (count <= 2) {
341+
return new Response(undefined, {
342+
status: 429,
343+
headers: {
344+
'Retry-After': '0.1',
345+
},
346+
});
347+
}
348+
capturedRequest = init;
349+
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
350+
};
351+
const client = new Mux({
352+
tokenId: 'my token id',
353+
tokenSecret: 'my secret',
354+
fetch: testFetch,
355+
maxRetries: 4,
356+
});
357+
358+
expect(
359+
await client.request({
360+
path: '/foo',
361+
method: 'get',
362+
headers: { 'X-Stainless-Retry-Count': '42' },
363+
}),
364+
).toEqual({ a: 1 });
365+
366+
expect((capturedRequest!.headers as Headers)['x-stainless-retry-count']).toBe('42');
367+
});
368+
301369
test('retry on 429 with retry-after', async () => {
302370
let count = 0;
303371
const testFetch = async (url: RequestInfo, { signal }: RequestInit = {}): Promise<Response> => {

0 commit comments

Comments
 (0)