Skip to content

Commit

Permalink
feat(client): support passing a custom fetch function (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and RobertCraigie committed Jul 11, 2023
1 parent 047d328 commit 7d54366
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export {

const MAX_RETRIES = 2;

type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;

export abstract class APIClient {
baseURL: string;
Expand All @@ -37,18 +37,20 @@ export abstract class APIClient {
maxRetries,
timeout = 60 * 1000, // 60s
httpAgent,
fetch: overridenFetch,
}: {
baseURL: string;
maxRetries?: number | undefined;
timeout: number | undefined;
httpAgent: Agent | undefined;
fetch: Fetch | undefined;
}) {
this.baseURL = baseURL;
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries ?? MAX_RETRIES);
this.timeout = validatePositiveInteger('timeout', timeout);
this.httpAgent = httpAgent;

this.fetch = fetch;
this.fetch = overridenFetch ?? fetch;
}

protected authHeaders(): Headers {
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type Config = {
*/
httpAgent?: Agent;

/**
* Specify a custom `fetch` function implementation.
*
* If not provided, we use `node-fetch` on Node.js and otherwise expect that `fetch` is
* defined globally.
*/
fetch?: Core.Fetch | undefined;

/**
* The maximum number of times that the client will retry a request in case of a
* temporary failure, like a network error or a 5XX error from the server.
Expand Down Expand Up @@ -81,6 +89,7 @@ export class Anthropic extends Core.APIClient {
timeout: options.timeout,
httpAgent: options.httpAgent,
maxRetries: options.maxRetries,
fetch: options.fetch,
});
this.apiKey = options.apiKey || null;
this._options = options;
Expand Down
18 changes: 18 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Headers } from '@anthropic-ai/sdk/core';
import Anthropic from '@anthropic-ai/sdk';
import { Response } from '@anthropic-ai/sdk/_shims/fetch';

describe('instantiate client', () => {
const env = process.env;
Expand Down Expand Up @@ -77,6 +78,23 @@ describe('instantiate client', () => {
});
});

test('custom fetch', async () => {
const client = new Anthropic({
baseURL: 'http://localhost:5000/',
apiKey: 'my api key',
fetch: (url) => {
return Promise.resolve(
new Response(JSON.stringify({ url, custom: true }), {
headers: { 'Content-Type': 'application/json' },
}),
);
},
});

const response = await client.get('/foo');
expect(response).toEqual({ url: 'http://localhost:5000/foo', custom: true });
});

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 7d54366

Please sign in to comment.