diff --git a/src/core.ts b/src/core.ts index ab80c666..add3d06d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -961,14 +961,16 @@ export const ensurePresent = (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; }; diff --git a/src/index.ts b/src/index.ts index 9cc56814..500271e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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({ diff --git a/tests/index.test.ts b/tests/index.test.ts index 7b72baf9..67e94353 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -140,7 +140,7 @@ describe('instantiate client', () => { }); afterEach(() => { - process.env['SINK_BASE_URL'] = undefined; + process.env['ANTHROPIC_BASE_URL'] = undefined; }); test('explicit option', () => { @@ -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', () => {