Skip to content

Commit

Permalink
feat(client): support reading the base url from an env variable (#277)
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 committed Dec 5, 2023
1 parent 7020b23 commit d6b7fa5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ export interface ClientOptions {

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
* Defaults to process.env['LITHIC_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 @@ -100,10 +102,10 @@ export class Lithic extends Core.APIClient {
/**
* API Client for interfacing with the Lithic API.
*
* @param {string} [opts.apiKey==process.env['LITHIC_API_KEY'] ?? undefined]
* @param {string | null} [opts.webhookSecret==process.env['LITHIC_WEBHOOK_SECRET'] ?? null]
* @param {string} [opts.apiKey=process.env['LITHIC_API_KEY'] ?? undefined]
* @param {string | null} [opts.webhookSecret=process.env['LITHIC_WEBHOOK_SECRET'] ?? null]
* @param {Environment} [opts.environment=production] - Specifies the environment URL to use for the API.
* @param {string} [opts.baseURL] - Override the default base URL for the API.
* @param {string} [opts.baseURL=process.env['LITHIC_BASE_URL'] ?? https://api.lithic.com/v1] - 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 @@ -112,6 +114,7 @@ export class Lithic extends Core.APIClient {
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
*/
constructor({
baseURL = Core.readEnv('LITHIC_BASE_URL'),
apiKey = Core.readEnv('LITHIC_API_KEY'),
webhookSecret = Core.readEnv('LITHIC_WEBHOOK_SECRET') ?? null,
...opts
Expand All @@ -126,9 +129,16 @@ export class Lithic extends Core.APIClient {
apiKey,
webhookSecret,
...opts,
baseURL,
environment: opts.environment ?? 'production',
};

if (baseURL && opts.environment) {
throw new Errors.LithicError(
'Ambiguous URL; The `baseURL` option (or LITHIC_BASE_URL env var) and the `environment` option are given. If you want to use the environment you must pass baseURL: null',
);
}

super({
baseURL: options.baseURL || environments[options.environment || 'production'],
timeout: options.timeout ?? 60000 /* 1 minute */,
Expand Down
28 changes: 28 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ 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 Lithic({ baseURL: 'https://example.com', apiKey: 'My Lithic API Key' });
expect(client.baseURL).toEqual('https://example.com');
});

test('env variable', () => {
process.env['LITHIC_BASE_URL'] = 'https://example.com/from_env';
const client = new Lithic({ apiKey: 'My Lithic API Key' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});

test('env variable with environment', () => {
process.env['LITHIC_BASE_URL'] = 'https://example.com/from_env';

expect(
() => new Lithic({ apiKey: 'My Lithic API Key', environment: 'production' }),
).toThrowErrorMatchingInlineSnapshot(
`"Ambiguous URL; The \`baseURL\` option (or LITHIC_BASE_URL env var) and the \`environment\` option are given. If you want to use the environment you must pass baseURL: null"`,
);

const client = new Lithic({ apiKey: 'My Lithic API Key', baseURL: null, environment: 'production' });
expect(client.baseURL).toEqual('https://api.lithic.com/v1');
});
});

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

0 comments on commit d6b7fa5

Please sign in to comment.