Skip to content

Commit

Permalink
refactor: use destructuring arguments in client constructor and respe…
Browse files Browse the repository at this point in the history
…ct false values (#172)
  • Loading branch information
stainless-bot authored and Caleb Siu committed Aug 1, 2023
1 parent c47cae2 commit 2efa6bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
27 changes: 24 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,12 @@ export const ensurePresent = <T>(value: T | null | undefined): T => {
*
* Will return an empty string if the environment variable doesn't exist or cannot be accessed.
*/
export const readEnv = (env: string): string => {
export const readEnv = (env: string): string | undefined => {
if (typeof process === 'undefined') {
return '';
return undefined;
}

return process.env[env] ?? '';
return process.env[env] ?? undefined;
};

export const coerceInteger = (value: unknown): number => {
Expand All @@ -790,6 +790,27 @@ export const coerceBoolean = (value: unknown): boolean => {
return Boolean(value);
};

export const maybeCoerceInteger = (value: unknown): number | undefined => {
if (value === undefined) {
return undefined;
}
return coerceInteger(value);
};

export const maybeCoerceFloat = (value: unknown): number | undefined => {
if (value === undefined) {
return undefined;
}
return coerceFloat(value);
};

export const maybeCoerceBoolean = (value: unknown): boolean | undefined => {
if (value === undefined) {
return undefined;
}
return coerceBoolean(value);
};

// https://stackoverflow.com/a/34491287
export function isEmptyObj(obj: Object | null | undefined): boolean {
if (!obj) return true;
Expand Down
28 changes: 15 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,40 +81,42 @@ export class ModernTreasury extends Core.APIClient {

private _options: ClientOptions;

constructor(opts: ClientOptions = {}) {
const organizationId = opts.organizationId || Core.readEnv('MODERN_TREASURY_ORGANIZATION_ID');
constructor({
apiKey = Core.readEnv('MODERN_TREASURY_API_KEY'),
organizationId = Core.readEnv('MODERN_TREASURY_ORGANIZATION_ID'),
webhookKey = Core.readEnv('MODERN_TREASURY_WEBHOOK_KEY') ?? null,
...opts
}: ClientOptions = {}) {
if (apiKey === undefined) {
throw new Error(
'The MODERN_TREASURY_API_KEY environment variable is missing or empty; either provide it, or instantiate the ModernTreasury client with an apiKey option, like new ModernTreasury({ apiKey: undefined }).',
);
}
if (organizationId === undefined) {
throw new Error(
"The MODERN_TREASURY_ORGANIZATION_ID environment variable is missing or empty; either provide it, or instantiate the ModernTreasury client with an organizationId option, like new ModernTreasury({ organizationId: 'my-organization-ID' }).",
);
}
const webhookKey = opts.webhookKey || Core.readEnv('MODERN_TREASURY_WEBHOOK_KEY') || null;

const options: ClientOptions = {
apiKey: typeof process === 'undefined' ? '' : process.env['MODERN_TREASURY_API_KEY'] || '',
baseURL: `https://app.moderntreasury.com`,
...opts,
apiKey,
organizationId,
webhookKey,
baseURL: `https://app.moderntreasury.com`,
...opts,
};

if (!options.apiKey && options.apiKey !== null) {
throw new Error(
"The MODERN_TREASURY_API_KEY environment variable is missing or empty; either provide it, or instantiate the ModernTreasury client with an apiKey option, like new ModernTreasury({ apiKey: 'my api key' }).",
);
}

super({
baseURL: options.baseURL!,
timeout: options.timeout,
httpAgent: options.httpAgent,
maxRetries: options.maxRetries,
fetch: options.fetch,
});
this.apiKey = options.apiKey;
this._options = options;
this.idempotencyHeader = 'Idempotency-Key';

this.apiKey = apiKey;
this.organizationId = organizationId;
this.webhookKey = webhookKey;
}
Expand Down

0 comments on commit 2efa6bd

Please sign in to comment.