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

feat(api): manual updates #217

Merged
merged 1 commit into from
Aug 23, 2024
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Blockaid from '@blockaid/client';

const client = new Blockaid({
apiKey: process.env['BLOCKAID_CLIENT_API_KEY'], // This is the default and can be omitted
environment: 'sandbox', // defaults to 'production'
});

async function main() {
Expand Down Expand Up @@ -56,7 +55,6 @@ import Blockaid from '@blockaid/client';

const client = new Blockaid({
apiKey: process.env['BLOCKAID_CLIENT_API_KEY'], // This is the default and can be omitted
environment: 'sandbox', // defaults to 'production'
});

async function main() {
Expand Down
27 changes: 2 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@ import { type Agent } from './_shims/index';
import * as Core from './core';
import * as API from './resources/index';

const environments = {
production: 'https://api.blockaid.io',
sandbox: 'https://example.com',
};
type Environment = keyof typeof environments;

export interface ClientOptions {
/**
* Defaults to process.env['BLOCKAID_CLIENT_API_KEY'].
*/
apiKey?: string | undefined;

/**
* Specifies the environment to use for the API.
*
* Each environment maps to a different base URL:
* - `production` corresponds to `https://api.blockaid.io`
* - `sandbox` corresponds to `https://example.com`
*/
environment?: Environment;

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
Expand Down Expand Up @@ -96,7 +81,6 @@ export class Blockaid extends Core.APIClient {
* API Client for interfacing with the Blockaid API.
*
* @param {string | undefined} [opts.apiKey=process.env['BLOCKAID_CLIENT_API_KEY'] ?? undefined]
* @param {Environment} [opts.environment=production] - Specifies the environment URL to use for the API.
* @param {string} [opts.baseURL=process.env['BLOCKAID_BASE_URL'] ?? https://api.blockaid.io] - Override the default base URL for the API.
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
Expand All @@ -119,18 +103,11 @@ export class Blockaid extends Core.APIClient {
const options: ClientOptions = {
apiKey,
...opts,
baseURL,
environment: opts.environment ?? 'production',
baseURL: baseURL || `https://api.blockaid.io`,
};

if (baseURL && opts.environment) {
throw new Errors.BlockaidError(
'Ambiguous URL; The `baseURL` option (or BLOCKAID_BASE_URL env var) and the `environment` option are given. If you want to use the environment you must pass baseURL: null',
);
}

super({
baseURL: options.baseURL || environments[options.environment || 'production'],
baseURL: options.baseURL!,
timeout: options.timeout ?? 60000 /* 1 minute */,
httpAgent: options.httpAgent,
maxRetries: options.maxRetries,
Expand Down
13 changes: 0 additions & 13 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,6 @@ describe('instantiate client', () => {
const client = new Blockaid({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://api.blockaid.io');
});

test('env variable with environment', () => {
process.env['BLOCKAID_BASE_URL'] = 'https://example.com/from_env';

expect(
() => new Blockaid({ apiKey: 'My API Key', environment: 'production' }),
).toThrowErrorMatchingInlineSnapshot(
`"Ambiguous URL; The \`baseURL\` option (or BLOCKAID_BASE_URL env var) and the \`environment\` option are given. If you want to use the environment you must pass baseURL: null"`,
);

const client = new Blockaid({ apiKey: 'My API Key', baseURL: null, environment: 'production' });
expect(client.baseURL).toEqual('https://api.blockaid.io');
});
});

test('maxRetries option is correctly set', () => {
Expand Down