Skip to content

Commit

Permalink
feat(api): manual updates (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Aug 23, 2024
1 parent 77c5e8f commit 5fd3ab7
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 40 deletions.
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: 'text',
};
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 `text`
*/
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

0 comments on commit 5fd3ab7

Please sign in to comment.