Skip to content

Commit 76b1429

Browse files
committed
fix: use default base url if BASE_URL env var is blank (#250)
Previously, a blank BASE_URL environment variable would cause an invalid URL error. Now it uses the default.
1 parent 8797990 commit 76b1429

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/core.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,16 @@ export const ensurePresent = <T>(value: T | null | undefined): T => {
961961
/**
962962
* Read an environment variable.
963963
*
964+
* Trims beginning and trailing whitespace.
965+
*
964966
* Will return undefined if the environment variable doesn't exist or cannot be accessed.
965967
*/
966968
export const readEnv = (env: string): string | undefined => {
967969
if (typeof process !== 'undefined') {
968-
return process.env?.[env] ?? undefined;
970+
return process.env?.[env]?.trim() ?? undefined;
969971
}
970972
if (typeof Deno !== 'undefined') {
971-
return Deno.env?.get?.(env);
973+
return Deno.env?.get?.(env)?.trim();
972974
}
973975
return undefined;
974976
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Anthropic extends Core.APIClient {
104104
apiKey,
105105
authToken,
106106
...opts,
107-
baseURL: baseURL ?? `https://api.anthropic.com`,
107+
baseURL: baseURL || `https://api.anthropic.com`,
108108
};
109109

110110
super({

tests/index.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('instantiate client', () => {
140140
});
141141

142142
afterEach(() => {
143-
process.env['SINK_BASE_URL'] = undefined;
143+
process.env['ANTHROPIC_BASE_URL'] = undefined;
144144
});
145145

146146
test('explicit option', () => {
@@ -153,6 +153,18 @@ describe('instantiate client', () => {
153153
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
154154
expect(client.baseURL).toEqual('https://example.com/from_env');
155155
});
156+
157+
test('empty env variable', () => {
158+
process.env['ANTHROPIC_BASE_URL'] = ''; // empty
159+
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
160+
expect(client.baseURL).toEqual('https://api.anthropic.com');
161+
});
162+
163+
test('blank env variable', () => {
164+
process.env['ANTHROPIC_BASE_URL'] = ' '; // blank
165+
const client = new Anthropic({ apiKey: 'my-anthropic-api-key' });
166+
expect(client.baseURL).toEqual('https://api.anthropic.com');
167+
});
156168
});
157169

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

0 commit comments

Comments
 (0)