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 (#89)
  • Loading branch information
stainless-bot authored and rattrayalex committed Jul 29, 2023
1 parent 2c53e1c commit 8d4c686
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
27 changes: 24 additions & 3 deletions src/core.ts
Expand Up @@ -769,12 +769,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 @@ -797,6 +797,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
14 changes: 9 additions & 5 deletions src/index.ts
Expand Up @@ -76,14 +76,18 @@ export class Anthropic extends Core.APIClient {

private _options: ClientOptions;

constructor(opts: ClientOptions = {}) {
const authToken = opts.authToken || Core.readEnv('ANTHROPIC_AUTH_TOKEN') || null;
constructor({
apiKey = Core.readEnv('ANTHROPIC_API_KEY') ?? null,
authToken = Core.readEnv('ANTHROPIC_AUTH_TOKEN') ?? null,
...opts
}: ClientOptions = {}) {
undefined;

const options: ClientOptions = {
apiKey: typeof process === 'undefined' ? '' : process.env['ANTHROPIC_API_KEY'] || '',
apiKey,
authToken,
baseURL: `https://api.anthropic.com`,
...opts,
authToken,
};

super({
Expand All @@ -93,9 +97,9 @@ export class Anthropic extends Core.APIClient {
maxRetries: options.maxRetries,
fetch: options.fetch,
});
this.apiKey = options.apiKey || null;
this._options = options;

this.apiKey = apiKey;
this.authToken = authToken;
}

Expand Down

0 comments on commit 8d4c686

Please sign in to comment.