Skip to content

Commit

Permalink
fix(client): handle undefined process in more places (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and cleb11 committed Aug 1, 2023
1 parent 4c914f4 commit 5d0a45b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,19 @@ export const ensurePresent = <T>(value: T | null | undefined): T => {
return value;
};

/**
* Read an environment variable.
*
* Will return an empty string if the environment variable doesn't exist or cannot be accessed.
*/
export const readEnv = (env: string): string => {
if (typeof process === 'undefined') {
return '';
}

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

export const coerceInteger = (value: unknown): number => {
if (typeof value === 'number') return Math.round(value);
if (typeof value === 'string') return parseInt(value, 10);
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ export class ModernTreasury extends Core.APIClient {
this._options = options;
this.idempotencyHeader = 'Idempotency-Key';

const organizationId = opts?.organizationId || process.env['MODERN_TREASURY_ORGANIZATION_ID'];
const organizationId = options.organizationId || Core.readEnv('MODERN_TREASURY_ORGANIZATION_ID');
if (!organizationId) {
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' }).",
);
}
this.organizationId = organizationId;
this.webhookKey = opts?.webhookKey || process.env['MODERN_TREASURY_WEBHOOK_KEY'] || null;
this.webhookKey = options.webhookKey || Core.readEnv('MODERN_TREASURY_WEBHOOK_KEY') || null;
}

connections: API.Connections = new API.Connections(this);
Expand Down

0 comments on commit 5d0a45b

Please sign in to comment.