diff --git a/CHANGELOG.md b/CHANGELOG.md index d32af699..a1dde545 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,3 +7,4 @@ Full Changelog: [v0.0.1...v0.1.0](https://github.com/dubinc/dub-node/compare/v0. ### Features * **api:** OpenAPI spec update ([458279d](https://github.com/dubinc/dub-node/commit/458279daf1ee67a3d0d1c27ef5ffb686bcd49fc1)) +* **api:** OpenAPI spec update ([#2](https://github.com/dubinc/dub-node/issues/2)) ([8472c55](https://github.com/dubinc/dub-node/commit/8472c55033077b41ece1e00d1dfe1fd9c15722d5)) \ No newline at end of file diff --git a/README.md b/README.md index 0f2671c9..56ec716f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,9 @@ The full API of this library can be found in [api.md](https://www.github.com/dub ```js import Dub from 'dub'; -const dub = new Dub(); +const dub = new Dub({ + apiKey: process.env['DUB_API_KEY'], // This is the default and can be omitted +}); async function main() { const projectDetails = await dub.projects.retrieve('REPLACE_ME'); @@ -39,7 +41,9 @@ This library includes TypeScript definitions for all request params and response ```ts import Dub from 'dub'; -const dub = new Dub(); +const dub = new Dub({ + apiKey: process.env['DUB_API_KEY'], // This is the default and can be omitted +}); async function main() { const projectDetails: Dub.ProjectDetails = await dub.projects.retrieve('REPLACE_ME'); diff --git a/src/index.ts b/src/index.ts index ab3ad1e2..b0173d90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,9 +8,11 @@ import * as API from 'dub/resources/index'; export interface ClientOptions { /** - * Defaults to process.env['DUB_BEARER_TOKEN']. + * Defaults to process.env['DUB_API_KEY']. */ - bearerToken?: string; + apiKey?: string; + + projectSlug?: string | null; /** * Override the default base URL for the API, e.g., "https://api.example.com/v2/" @@ -71,14 +73,16 @@ export interface ClientOptions { /** API Client for interfacing with the Dub API. */ export class Dub extends Core.APIClient { - bearerToken: string; + apiKey: string; + projectSlug: string | null; private _options: ClientOptions; /** * API Client for interfacing with the Dub API. * - * @param {string} [opts.bearerToken=process.env['DUB_BEARER_TOKEN'] ?? undefined] + * @param {string} [opts.apiKey=process.env['DUB_API_KEY'] ?? undefined] + * @param {string | null} [opts.projectSlug] * @param {string} [opts.baseURL=process.env['DUB_BASE_URL'] ?? https://api.dub.co] - 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. @@ -89,17 +93,19 @@ export class Dub extends Core.APIClient { */ constructor({ baseURL = Core.readEnv('DUB_BASE_URL'), - bearerToken = Core.readEnv('DUB_BEARER_TOKEN'), + apiKey = Core.readEnv('DUB_API_KEY'), + projectSlug = null, ...opts }: ClientOptions = {}) { - if (bearerToken === undefined) { + if (apiKey === undefined) { throw new Errors.DubError( - "The DUB_BEARER_TOKEN environment variable is missing or empty; either provide it, or instantiate the Dub client with an bearerToken option, like new Dub({ bearerToken: 'My Bearer Token' }).", + "The DUB_API_KEY environment variable is missing or empty; either provide it, or instantiate the Dub client with an apiKey option, like new Dub({ apiKey: 'My API Key' }).", ); } const options: ClientOptions = { - bearerToken, + apiKey, + projectSlug, ...opts, baseURL: baseURL ?? `https://api.dub.co`, }; @@ -113,7 +119,8 @@ export class Dub extends Core.APIClient { }); this._options = options; - this.bearerToken = bearerToken; + this.apiKey = apiKey; + this.projectSlug = projectSlug; } links: API.Links = new API.Links(this); @@ -131,7 +138,7 @@ export class Dub extends Core.APIClient { } protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers { - return { Authorization: `Bearer ${this.bearerToken}` }; + return { Authorization: `Bearer ${this.apiKey}` }; } static Dub = this; diff --git a/tests/api-resources/links/bulk.test.ts b/tests/api-resources/links/bulk.test.ts index 4f89d443..3e7b3e64 100644 --- a/tests/api-resources/links/bulk.test.ts +++ b/tests/api-resources/links/bulk.test.ts @@ -4,7 +4,7 @@ import Dub from 'dub'; import { Response } from 'node-fetch'; const dub = new Dub({ - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/links/info.test.ts b/tests/api-resources/links/info.test.ts index 8169f552..d2220742 100644 --- a/tests/api-resources/links/info.test.ts +++ b/tests/api-resources/links/info.test.ts @@ -4,7 +4,7 @@ import Dub from 'dub'; import { Response } from 'node-fetch'; const dub = new Dub({ - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/links/links.test.ts b/tests/api-resources/links/links.test.ts index 7bb5306a..497ae0d2 100644 --- a/tests/api-resources/links/links.test.ts +++ b/tests/api-resources/links/links.test.ts @@ -4,7 +4,7 @@ import Dub from 'dub'; import { Response } from 'node-fetch'; const dub = new Dub({ - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/projects/projects.test.ts b/tests/api-resources/projects/projects.test.ts index bbfae1be..17683176 100644 --- a/tests/api-resources/projects/projects.test.ts +++ b/tests/api-resources/projects/projects.test.ts @@ -4,7 +4,7 @@ import Dub from 'dub'; import { Response } from 'node-fetch'; const dub = new Dub({ - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/projects/tags.test.ts b/tests/api-resources/projects/tags.test.ts index 41b3d35e..b98f704b 100644 --- a/tests/api-resources/projects/tags.test.ts +++ b/tests/api-resources/projects/tags.test.ts @@ -4,7 +4,7 @@ import Dub from 'dub'; import { Response } from 'node-fetch'; const dub = new Dub({ - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/index.test.ts b/tests/index.test.ts index 90ffb056..39f5a663 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -23,7 +23,7 @@ describe('instantiate client', () => { const client = new Dub({ baseURL: 'http://localhost:5000/', defaultHeaders: { 'X-My-Default-Header': '2' }, - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', }); test('they are used in the request', () => { @@ -55,7 +55,7 @@ describe('instantiate client', () => { const client = new Dub({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo' }, - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo'); }); @@ -64,7 +64,7 @@ describe('instantiate client', () => { const client = new Dub({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo', hello: 'world' }, - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world'); }); @@ -73,7 +73,7 @@ describe('instantiate client', () => { const client = new Dub({ baseURL: 'http://localhost:5000/', defaultQuery: { hello: 'world' }, - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', }); expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo'); }); @@ -82,7 +82,7 @@ describe('instantiate client', () => { test('custom fetch', async () => { const client = new Dub({ baseURL: 'http://localhost:5000/', - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', fetch: (url) => { return Promise.resolve( new Response(JSON.stringify({ url, custom: true }), { @@ -99,7 +99,7 @@ describe('instantiate client', () => { test('custom signal', async () => { const client = new Dub({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', - bearerToken: 'My Bearer Token', + apiKey: 'My API Key', fetch: (...args) => { return new Promise((resolve, reject) => setTimeout( @@ -124,18 +124,12 @@ describe('instantiate client', () => { describe('baseUrl', () => { test('trailing slash', () => { - const client = new Dub({ - baseURL: 'http://localhost:5000/custom/path/', - bearerToken: 'My Bearer Token', - }); + const client = new Dub({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo'); }); test('no trailing slash', () => { - const client = new Dub({ - baseURL: 'http://localhost:5000/custom/path', - bearerToken: 'My Bearer Token', - }); + const client = new Dub({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo'); }); @@ -144,43 +138,43 @@ describe('instantiate client', () => { }); test('explicit option', () => { - const client = new Dub({ baseURL: 'https://example.com', bearerToken: 'My Bearer Token' }); + const client = new Dub({ baseURL: 'https://example.com', apiKey: 'My API Key' }); expect(client.baseURL).toEqual('https://example.com'); }); test('env variable', () => { process.env['DUB_BASE_URL'] = 'https://example.com/from_env'; - const client = new Dub({ bearerToken: 'My Bearer Token' }); + const client = new Dub({ apiKey: 'My API Key' }); expect(client.baseURL).toEqual('https://example.com/from_env'); }); }); test('maxRetries option is correctly set', () => { - const client = new Dub({ maxRetries: 4, bearerToken: 'My Bearer Token' }); + const client = new Dub({ maxRetries: 4, apiKey: 'My API Key' }); expect(client.maxRetries).toEqual(4); // default - const client2 = new Dub({ bearerToken: 'My Bearer Token' }); + const client2 = new Dub({ apiKey: 'My API Key' }); expect(client2.maxRetries).toEqual(2); }); test('with environment variable arguments', () => { // set options via env var - process.env['DUB_BEARER_TOKEN'] = 'My Bearer Token'; + process.env['DUB_API_KEY'] = 'My API Key'; const client = new Dub(); - expect(client.bearerToken).toBe('My Bearer Token'); + expect(client.apiKey).toBe('My API Key'); }); test('with overriden environment variable arguments', () => { // set options via env var - process.env['DUB_BEARER_TOKEN'] = 'another My Bearer Token'; - const client = new Dub({ bearerToken: 'My Bearer Token' }); - expect(client.bearerToken).toBe('My Bearer Token'); + process.env['DUB_API_KEY'] = 'another My API Key'; + const client = new Dub({ apiKey: 'My API Key' }); + expect(client.apiKey).toBe('My API Key'); }); }); describe('request building', () => { - const client = new Dub({ bearerToken: 'My Bearer Token' }); + const client = new Dub({ apiKey: 'My API Key' }); describe('Content-Length', () => { test('handles multi-byte characters', () => { @@ -221,7 +215,7 @@ describe('retries', () => { return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } }); }; - const client = new Dub({ bearerToken: 'My Bearer Token', timeout: 2000, fetch: testFetch }); + const client = new Dub({ apiKey: 'My API Key', timeout: 2000, fetch: testFetch }); expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 }); expect(count).toEqual(2);