Skip to content

Commit

Permalink
fix: use default base url if BASE_URL env var is blank (#316)
Browse files Browse the repository at this point in the history
Previously, a blank BASE_URL environment variable would cause an invalid URL error. Now it uses the default.
  • Loading branch information
stainless-bot committed Jan 10, 2024
1 parent e06c3a0 commit 38a3e62
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,14 +953,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 @@ -127,7 +127,7 @@ export class ModernTreasury extends Core.APIClient {
organizationId,
webhookKey,
...opts,
baseURL: baseURL ?? `https://app.moderntreasury.com`,
baseURL: baseURL || `https://app.moderntreasury.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 @@ -148,7 +148,7 @@ describe('instantiate client', () => {
});

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

test('explicit option', () => {
Expand All @@ -165,6 +165,18 @@ describe('instantiate client', () => {
const client = new ModernTreasury({ apiKey: 'My API Key', organizationId: 'my-organization-ID' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});

test('empty env variable', () => {
process.env['MODERN_TREASURY_BASE_URL'] = ''; // empty
const client = new ModernTreasury({ apiKey: 'My API Key', organizationId: 'my-organization-ID' });
expect(client.baseURL).toEqual('https://app.moderntreasury.com');
});

test('blank env variable', () => {
process.env['MODERN_TREASURY_BASE_URL'] = ' '; // blank
const client = new ModernTreasury({ apiKey: 'My API Key', organizationId: 'my-organization-ID' });
expect(client.baseURL).toEqual('https://app.moderntreasury.com');
});
});

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

0 comments on commit 38a3e62

Please sign in to comment.