Skip to content

Commit

Permalink
feat(client): add support for defaultQuery option
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and RobertCraigie committed Jul 7, 2023
1 parent 4facbae commit 48b1597
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export abstract class APIClient {
};
}

protected abstract defaultQuery(): DefaultQuery | undefined;

/**
* Override this to add your own headers validation:
*/
Expand Down Expand Up @@ -260,6 +262,11 @@ export abstract class APIClient {
new URL(path)
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));

const defaultQuery = this.defaultQuery();
if (!isEmptyObj(defaultQuery)) {
query = { ...defaultQuery, ...query } as Req;
}

if (query) {
url.search = qs.stringify(query, this.qsOptions());
}
Expand Down Expand Up @@ -516,6 +523,7 @@ type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';

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

export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config = {
httpAgent?: Agent;
maxRetries?: number;
defaultHeaders?: Core.Headers;
defaultQuery?: Core.DefaultQuery;
authToken?: string | null;
};

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

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

protected override defaultQuery(): Core.DefaultQuery | undefined {
return this._options.defaultQuery;
}

protected override defaultHeaders(): Core.Headers {
return {
...super.defaultHeaders(),
Expand Down
29 changes: 29 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@ describe('instantiate client', () => {
expect((req.headers as Headers)['X-My-Default-Header']).toEqual('2');
});

describe('defaultQuery', () => {
test('with null query params given', () => {
const client = new Anthropic({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo' },
apiKey: 'my api key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo');
});

test('multiple default query params', () => {
const client = new Anthropic({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo', hello: 'world' },
apiKey: 'my api key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world');
});

test('overriding with `undefined`', () => {
const client = new Anthropic({
baseURL: 'http://localhost:5000/',
defaultQuery: { hello: 'world' },
apiKey: 'my api key',
});
expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo');
});
});

describe('baseUrl', () => {
test('trailing slash', () => {
const client = new Anthropic({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'my api key' });
Expand Down

0 comments on commit 48b1597

Please sign in to comment.