Skip to content

Commit

Permalink
Merge pull request #3 from dubinc/release-please--branches--main--cha…
Browse files Browse the repository at this point in the history
…nges--next--components--dub

release: 0.1.0
  • Loading branch information
steven-tey authored Jan 10, 2024
2 parents dcd66ad + 835797b commit 4745a3d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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.
Expand All @@ -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`,
};
Expand All @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/links/bulk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/links/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/links/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/projects/projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/projects/tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
44 changes: 19 additions & 25 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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 }), {
Expand All @@ -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(
Expand All @@ -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');
});

Expand All @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4745a3d

Please sign in to comment.