Skip to content

Commit 48b1597

Browse files
stainless-botRobertCraigie
authored andcommitted
feat(client): add support for defaultQuery option
1 parent 4facbae commit 48b1597

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export abstract class APIClient {
7373
};
7474
}
7575

76+
protected abstract defaultQuery(): DefaultQuery | undefined;
77+
7678
/**
7779
* Override this to add your own headers validation:
7880
*/
@@ -260,6 +262,11 @@ export abstract class APIClient {
260262
new URL(path)
261263
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
262264

265+
const defaultQuery = this.defaultQuery();
266+
if (!isEmptyObj(defaultQuery)) {
267+
query = { ...defaultQuery, ...query } as Req;
268+
}
269+
263270
if (query) {
264271
url.search = qs.stringify(query, this.qsOptions());
265272
}
@@ -516,6 +523,7 @@ type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
516523

517524
export type RequestClient = { fetch: Fetch };
518525
export type Headers = Record<string, string | null | undefined>;
526+
export type DefaultQuery = Record<string, string | undefined>;
519527
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
520528

521529
export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Config = {
1717
httpAgent?: Agent;
1818
maxRetries?: number;
1919
defaultHeaders?: Core.Headers;
20+
defaultQuery?: Core.DefaultQuery;
2021
authToken?: string | null;
2122
};
2223

@@ -48,6 +49,10 @@ export class Anthropic extends Core.APIClient {
4849

4950
completions: API.Completions = new API.Completions(this);
5051

52+
protected override defaultQuery(): Core.DefaultQuery | undefined {
53+
return this._options.defaultQuery;
54+
}
55+
5156
protected override defaultHeaders(): Core.Headers {
5257
return {
5358
...super.defaultHeaders(),

tests/index.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ describe('instantiate client', () => {
2424
expect((req.headers as Headers)['X-My-Default-Header']).toEqual('2');
2525
});
2626

27+
describe('defaultQuery', () => {
28+
test('with null query params given', () => {
29+
const client = new Anthropic({
30+
baseURL: 'http://localhost:5000/',
31+
defaultQuery: { apiVersion: 'foo' },
32+
apiKey: 'my api key',
33+
});
34+
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo');
35+
});
36+
37+
test('multiple default query params', () => {
38+
const client = new Anthropic({
39+
baseURL: 'http://localhost:5000/',
40+
defaultQuery: { apiVersion: 'foo', hello: 'world' },
41+
apiKey: 'my api key',
42+
});
43+
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world');
44+
});
45+
46+
test('overriding with `undefined`', () => {
47+
const client = new Anthropic({
48+
baseURL: 'http://localhost:5000/',
49+
defaultQuery: { hello: 'world' },
50+
apiKey: 'my api key',
51+
});
52+
expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo');
53+
});
54+
});
55+
2756
describe('baseUrl', () => {
2857
test('trailing slash', () => {
2958
const client = new Anthropic({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'my api key' });

0 commit comments

Comments
 (0)