Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use default base url if BASE_URL env var is blank #250

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,14 +961,16 @@ export const ensurePresent = <T>(value: T | null | undefined): T => {
/**
* Read an environment variable.
*
* Trims beginning and trailing whitespace.
*
* Will return undefined if the environment variable doesn't exist or cannot be accessed.
*/
export const readEnv = (env: string): string | undefined => {
if (typeof process !== 'undefined') {
return process.env?.[env] ?? undefined;
return process.env?.[env]?.trim() ?? undefined;
}
if (typeof Deno !== 'undefined') {
return Deno.env?.get?.(env);
return Deno.env?.get?.(env)?.trim();
}
return undefined;
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Anthropic extends Core.APIClient {
apiKey,
authToken,
...opts,
baseURL: baseURL ?? `https://api.anthropic.com`,
baseURL: baseURL || `https://api.anthropic.com`,
};

super({
Expand Down
14 changes: 13 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('instantiate client', () => {
});

afterEach(() => {
process.env['SINK_BASE_URL'] = undefined;
process.env['ANTHROPIC_BASE_URL'] = undefined;
});

test('explicit option', () => {
Expand All @@ -153,6 +153,18 @@ describe('instantiate client', () => {
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});

test('empty env variable', () => {
process.env['ANTHROPIC_BASE_URL'] = ''; // empty
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
expect(client.baseURL).toEqual('https://api.anthropic.com');
});

test('blank env variable', () => {
process.env['ANTHROPIC_BASE_URL'] = ' '; // blank
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
expect(client.baseURL).toEqual('https://api.anthropic.com');
});
});

test('maxRetries option is correctly set', () => {
Expand Down