Skip to content

Commit

Permalink
feat(client): support reading the base url from an env variable (#294)
Browse files Browse the repository at this point in the history
warning: this could result in an error if you're passing both an environment
         and a base url argument to the client, you'll need to either omit one
         of them or set `baseURL` to `null`
  • Loading branch information
stainless-bot authored and cleb11 committed Dec 5, 2023
1 parent 25d961f commit 8e6444d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export interface ClientOptions {

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
* Defaults to process.env['MODERN_TREASURY_BASE_URL'].
*/
baseURL?: string;
baseURL?: string | null | undefined;

/**
* The maximum amount of time (in milliseconds) that the client should wait for a response
Expand Down Expand Up @@ -91,10 +93,10 @@ export class ModernTreasury extends Core.APIClient {
/**
* API Client for interfacing with the Modern Treasury API.
*
* @param {string} [opts.apiKey==process.env['MODERN_TREASURY_API_KEY'] ?? undefined]
* @param {string} [opts.organizationId==process.env['MODERN_TREASURY_ORGANIZATION_ID'] ?? undefined]
* @param {string | null} [opts.webhookKey==process.env['MODERN_TREASURY_WEBHOOK_KEY'] ?? null]
* @param {string} [opts.baseURL] - Override the default base URL for the API.
* @param {string} [opts.apiKey=process.env['MODERN_TREASURY_API_KEY'] ?? undefined]
* @param {string} [opts.organizationId=process.env['MODERN_TREASURY_ORGANIZATION_ID'] ?? undefined]
* @param {string | null} [opts.webhookKey=process.env['MODERN_TREASURY_WEBHOOK_KEY'] ?? null]
* @param {string} [opts.baseURL=process.env['MODERN_TREASURY_BASE_URL'] ?? https://app.moderntreasury.com] - Override the default base URL for the API.
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
* @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
Expand All @@ -103,6 +105,7 @@ export class ModernTreasury extends Core.APIClient {
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
*/
constructor({
baseURL = Core.readEnv('MODERN_TREASURY_BASE_URL'),
apiKey = Core.readEnv('MODERN_TREASURY_API_KEY'),
organizationId = Core.readEnv('MODERN_TREASURY_ORGANIZATION_ID'),
webhookKey = Core.readEnv('MODERN_TREASURY_WEBHOOK_KEY') ?? null,
Expand All @@ -124,7 +127,7 @@ export class ModernTreasury extends Core.APIClient {
organizationId,
webhookKey,
...opts,
baseURL: opts.baseURL ?? `https://app.moderntreasury.com`,
baseURL: baseURL ?? `https://app.moderntreasury.com`,
};

super({
Expand Down
19 changes: 19 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ describe('instantiate client', () => {
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

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

test('explicit option', () => {
const client = new ModernTreasury({
baseURL: 'https://example.com',
apiKey: 'My API Key',
organizationId: 'my-organization-ID',
});
expect(client.baseURL).toEqual('https://example.com');
});

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

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

0 comments on commit 8e6444d

Please sign in to comment.