Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use destructuring arguments in client constructor and respect false values #89

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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