Skip to content

Commit

Permalink
fix(client): handle undefined process in more places (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and rattrayalex committed Jul 29, 2023
1 parent 5714a14 commit d950c25
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class Anthropic extends Core.APIClient {
this.apiKey = options.apiKey || null;
this._options = options;

this.authToken = opts?.authToken || process.env['ANTHROPIC_AUTH_TOKEN'] || null;
this.authToken = options.authToken || Core.readEnv('ANTHROPIC_AUTH_TOKEN') || null;
}

completions: API.Completions = new API.Completions(this);
Expand Down

0 comments on commit d950c25

Please sign in to comment.