diff --git a/.stats.yml b/.stats.yml index 70bddde..e5f4ae3 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ configured_endpoints: 18 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fbrowserbase-60444f8b1aa1aa8dbec1e9f11e929c2b7ac27470764ef5f1796134fc27f3381c.yml +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fbrowserbase-9f93c744538f57747ea1385817e21b40c318b65ebc155dca8950268beb280bc9.yml diff --git a/README.md b/README.md index 0380e8f..9dfea97 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ import Browserbase from 'browserbase'; const client = new Browserbase({ apiKey: process.env['BROWSERBASE_API_KEY'], // This is the default and can be omitted + environment: 'development', // or 'production' | 'local'; defaults to 'production' }); async function main() { @@ -48,6 +49,7 @@ import Browserbase from 'browserbase'; const client = new Browserbase({ apiKey: process.env['BROWSERBASE_API_KEY'], // This is the default and can be omitted + environment: 'development', // or 'production' | 'local'; defaults to 'production' }); async function main() { diff --git a/package.json b/package.json index 593ead0..f56ff7d 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "packageManager": "yarn@1.22.22", "files": [ - "*" + "**/*" ], "private": false, "scripts": { diff --git a/src/index.ts b/src/index.ts index 03c44c2..daf7904 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,12 +6,29 @@ import { type Agent } from './_shims/index'; import * as Core from './core'; import * as API from './resources/index'; +const environments = { + production: 'https://api.browserbase.com', + development: 'https://api.dev.browserbase.com', + local: 'http://api.localhost', +}; +type Environment = keyof typeof environments; + export interface ClientOptions { /** * Your [Browserbase API Key](https://www.browserbase.com/settings). */ apiKey?: string | undefined; + /** + * Specifies the environment to use for the API. + * + * Each environment maps to a different base URL: + * - `production` corresponds to `https://api.browserbase.com` + * - `development` corresponds to `https://api.dev.browserbase.com` + * - `local` corresponds to `http://api.localhost` + */ + environment?: Environment; + /** * Override the default base URL for the API, e.g., "https://api.example.com/v2/" * @@ -81,7 +98,8 @@ export class Browserbase extends Core.APIClient { * API Client for interfacing with the Browserbase API. * * @param {string | undefined} [opts.apiKey=process.env['BROWSERBASE_API_KEY'] ?? undefined] - * @param {string} [opts.baseURL=process.env['BROWSERBASE_BASE_URL'] ?? https://api.dev.browserbase.com] - Override the default base URL for the API. + * @param {Environment} [opts.environment=production] - Specifies the environment URL to use for the API. + * @param {string} [opts.baseURL=process.env['BROWSERBASE_BASE_URL'] ?? https://api.browserbase.com] - 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. * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation. @@ -103,11 +121,18 @@ export class Browserbase extends Core.APIClient { const options: ClientOptions = { apiKey, ...opts, - baseURL: baseURL || `https://api.dev.browserbase.com`, + baseURL, + environment: opts.environment ?? 'production', }; + if (baseURL && opts.environment) { + throw new Errors.BrowserbaseError( + 'Ambiguous URL; The `baseURL` option (or BROWSERBASE_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!, + baseURL: options.baseURL || environments[options.environment || 'production'], timeout: options.timeout ?? 60000 /* 1 minute */, httpAgent: options.httpAgent, maxRetries: options.maxRetries, diff --git a/tests/index.test.ts b/tests/index.test.ts index 55a9e2e..c1f07a2 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -151,13 +151,26 @@ describe('instantiate client', () => { test('empty env variable', () => { process.env['BROWSERBASE_BASE_URL'] = ''; // empty const client = new Browserbase({ apiKey: 'My API Key' }); - expect(client.baseURL).toEqual('https://api.dev.browserbase.com'); + expect(client.baseURL).toEqual('https://api.browserbase.com'); }); test('blank env variable', () => { process.env['BROWSERBASE_BASE_URL'] = ' '; // blank const client = new Browserbase({ apiKey: 'My API Key' }); - expect(client.baseURL).toEqual('https://api.dev.browserbase.com'); + expect(client.baseURL).toEqual('https://api.browserbase.com'); + }); + + test('env variable with environment', () => { + process.env['BROWSERBASE_BASE_URL'] = 'https://example.com/from_env'; + + expect( + () => new Browserbase({ apiKey: 'My API Key', environment: 'production' }), + ).toThrowErrorMatchingInlineSnapshot( + `"Ambiguous URL; The \`baseURL\` option (or BROWSERBASE_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 Browserbase({ apiKey: 'My API Key', baseURL: null, environment: 'production' }); + expect(client.baseURL).toEqual('https://api.browserbase.com'); }); });