From 6cfc2d9c25db1d175286bce668f1111ed29d3b70 Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Fri, 18 Jan 2019 15:47:14 -0600 Subject: [PATCH 1/6] Start organization API docs --- docs/README.md | 1 + docs/organizations.md | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 docs/organizations.md diff --git a/docs/README.md b/docs/README.md index 5544a6c..ead9102 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,6 +8,7 @@ This SDK interface closely mirors the [Eventbrite v3 REST API](https://www.event - [Configuring a SDK object](#configuring-a-sdk-object) - [`request()`](./request.md) - [Users](./users.md) +- [Organizations](./organizations.md) ## Including the package diff --git a/docs/organizations.md b/docs/organizations.md new file mode 100644 index 0000000..b4596a4 --- /dev/null +++ b/docs/organizations.md @@ -0,0 +1,39 @@ +# Organizations + +This is a collection of methods that are intended to be helpful wrappers around the [organizations API endpoints](organization-api-docs). + +View the [Organizations response object](organization-object-reference) for details on the properties you'll get back with each response. + +## Table on Contents + +- [`sdk.organizations.getByUser(id)`](#getByUser) + + + +## `sdk.organizations.getByUser(id)` +Gets the details for a specific user by their user id. + +**Read [`/users/:id/organizations/` documentation](organization-by-user) for more details.** + +### API +```js +sdk.organizations.getByUser(id: string): Promise +``` + +### Example + +```js +const eventbrite = require('eventbrite'); + +// Create configured Eventbrite SDK +const sdk = eventbrite({token: 'OATH_TOKEN_HERE'}); + +sdk.organizations.getByUser('1234567890').then((organization) => { + console.log(`Welcome to ${organization.name}!`); +}); +``` + + +[organization-api-docs]: https://www.eventbrite.com/platform/api#/reference/organization +[organization-object-reference]: https://www.eventbrite.com/platform/api#/reference/organization +[organization-by-user]: https://www.eventbrite.com/platform/api#/reference/user/retrieve-a-user \ No newline at end of file From 1a260285257c391e1586fd341e15d584195b9e1b Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Wed, 23 Jan 2019 17:37:56 -0600 Subject: [PATCH 2/6] Added implementation and updated docs --- docs/organizations.md | 10 ++++----- src/__tests__/__fixtures__/index.ts | 17 ++++++++++++++ src/__tests__/organizations.spec.ts | 10 +++++++++ src/organizations.ts | 35 +++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/__tests__/organizations.spec.ts create mode 100644 src/organizations.ts diff --git a/docs/organizations.md b/docs/organizations.md index b4596a4..bb4ee10 100644 --- a/docs/organizations.md +++ b/docs/organizations.md @@ -13,11 +13,11 @@ View the [Organizations response object](organization-object-reference) for deta ## `sdk.organizations.getByUser(id)` Gets the details for a specific user by their user id. -**Read [`/users/:id/organizations/` documentation](organization-by-user) for more details.** +**Read [`/users/:userId/organizations/` documentation](organization-by-user) for more details.** ### API ```js -sdk.organizations.getByUser(id: string): Promise +sdk.organizations.getByUser(userId: string): Promise> ``` ### Example @@ -28,12 +28,12 @@ const eventbrite = require('eventbrite'); // Create configured Eventbrite SDK const sdk = eventbrite({token: 'OATH_TOKEN_HERE'}); -sdk.organizations.getByUser('1234567890').then((organization) => { - console.log(`Welcome to ${organization.name}!`); +sdk.organizations.getByUser('1234567890').then((paginatedResponse) => { + console.log(`Here are your organizations: ${paginatedResponse.organizations.join(' ')}.`); }); ``` [organization-api-docs]: https://www.eventbrite.com/platform/api#/reference/organization [organization-object-reference]: https://www.eventbrite.com/platform/api#/reference/organization -[organization-by-user]: https://www.eventbrite.com/platform/api#/reference/user/retrieve-a-user \ No newline at end of file +[organization-by-user]: https://www.eventbrite.com/platform/api#/reference/organization/list-organizations-by-user \ No newline at end of file diff --git a/src/__tests__/__fixtures__/index.ts b/src/__tests__/__fixtures__/index.ts index 4d446ff..e640f52 100644 --- a/src/__tests__/__fixtures__/index.ts +++ b/src/__tests__/__fixtures__/index.ts @@ -42,3 +42,20 @@ export const MOCK_ARGUMENTS_ERROR_RESPOSNE_DATA = { error_description: 'There are errors with your arguments: status - INVALID', error: 'ARGUMENTS_ERROR', }; + +export const MOCK_ORGS_BY_USER_SUCCESS_RESPONSE = { + organizations: [ + { + id: '1', + name: 'Organization 1', + }, + { + id: '2', + name: 'Organization 2', + }, + { + id: '3', + name: 'Organization 3', + }, + ], +}; diff --git a/src/__tests__/organizations.spec.ts b/src/__tests__/organizations.spec.ts new file mode 100644 index 0000000..6fb4353 --- /dev/null +++ b/src/__tests__/organizations.spec.ts @@ -0,0 +1,10 @@ +import { + mockFetch, + getMockFetch, + getMockResponse, + restoreMockFetch, +} from './utils'; +import {} from './__fixtures__'; + +import request from '../request'; +import {OrganizationsApi} from '../organizations'; diff --git a/src/organizations.ts b/src/organizations.ts new file mode 100644 index 0000000..07a6cd4 --- /dev/null +++ b/src/organizations.ts @@ -0,0 +1,35 @@ +import {BaseApi} from './baseApi'; + +enum OrganizationVertical { + Default, + Music, +} + +export interface Organization { + id: string; + name: string; + imageId: string; + locale?: string; + vertical?: OrganizationVertical; +} + +export interface PaginatedOrganizationResponse { + pagination: { + object_count: number; + page_number: number; + page_size: number; + page_count: number; + continuation: string; + has_more_items: boolean; + }; + organizations: Organization[]; +} + +/** + * API for working with Organizations + */ +export class OrganizationsApi extends BaseApi { + getByUser(userId: string) { + return this.request(`/users/${userId}/organizations/`); + } +} From daec78d25c60c8fcdfee8e5cf3edd19b1122a206 Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Thu, 24 Jan 2019 11:55:57 -0600 Subject: [PATCH 3/6] fixed up the types and pagniation to mock data --- src/__tests__/__fixtures__/index.ts | 8 ++++++++ src/organizations.ts | 22 +++------------------- src/types.ts | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/__tests__/__fixtures__/index.ts b/src/__tests__/__fixtures__/index.ts index e640f52..7db7644 100644 --- a/src/__tests__/__fixtures__/index.ts +++ b/src/__tests__/__fixtures__/index.ts @@ -44,6 +44,14 @@ export const MOCK_ARGUMENTS_ERROR_RESPOSNE_DATA = { }; export const MOCK_ORGS_BY_USER_SUCCESS_RESPONSE = { + pagination: { + object_count: 3, + page_number: 1, + page_size: 10, + page_count: 1, + continuation: 'some_fake_continuation_key', + has_more_items: false, + }, organizations: [ { id: '1', diff --git a/src/organizations.ts b/src/organizations.ts index 07a6cd4..d5e1f6e 100644 --- a/src/organizations.ts +++ b/src/organizations.ts @@ -1,34 +1,18 @@ import {BaseApi} from './baseApi'; - -enum OrganizationVertical { - Default, - Music, -} +import {PaginatedResponse} from './types'; export interface Organization { id: string; name: string; imageId: string; locale?: string; - vertical?: OrganizationVertical; -} - -export interface PaginatedOrganizationResponse { - pagination: { - object_count: number; - page_number: number; - page_size: number; - page_count: number; - continuation: string; - has_more_items: boolean; - }; - organizations: Organization[]; + vertical?: 'Default' | 'Music'; } /** * API for working with Organizations */ -export class OrganizationsApi extends BaseApi { +export class OrganizationsApi extends BaseApi> { getByUser(userId: string) { return this.request(`/users/${userId}/organizations/`); } diff --git a/src/types.ts b/src/types.ts index 159a2b3..f5c0920 100644 --- a/src/types.ts +++ b/src/types.ts @@ -31,3 +31,17 @@ export interface JSONResponseData { [propName: string]: any; }; } + +export interface Pagination { + objectCount: number; + pageNumber: number; + pageSize: number; + pageCount: number; + continuation: string; + hasMoreItems: boolean; +} + +export interface PaginatedResponse { + pagination: Pagination; + [key: string]: T[] | Pagination; +} From adc4e0c083131a626a0950a1caf748567bd24639 Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Tue, 29 Jan 2019 10:59:15 -0600 Subject: [PATCH 4/6] added test for organizations api --- src/__tests__/__fixtures__/index.ts | 25 ++++++++++ src/__tests__/organizations.spec.ts | 73 ++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/__tests__/__fixtures__/index.ts b/src/__tests__/__fixtures__/index.ts index 7db7644..b37811c 100644 --- a/src/__tests__/__fixtures__/index.ts +++ b/src/__tests__/__fixtures__/index.ts @@ -67,3 +67,28 @@ export const MOCK_ORGS_BY_USER_SUCCESS_RESPONSE = { }, ], }; + +export const MOCK_TRANSFORMED_ORGS_BY_USER = { + pagination: { + objectCount: 3, + pageNumber: 1, + pageSize: 10, + pageCount: 1, + continuation: 'some_fake_continuation_key', + hasMoreItems: false, + }, + organizations: [ + { + id: '1', + name: 'Organization 1', + }, + { + id: '2', + name: 'Organization 2', + }, + { + id: '3', + name: 'Organization 3', + }, + ], +}; diff --git a/src/__tests__/organizations.spec.ts b/src/__tests__/organizations.spec.ts index 6fb4353..d4772f1 100644 --- a/src/__tests__/organizations.spec.ts +++ b/src/__tests__/organizations.spec.ts @@ -4,7 +4,76 @@ import { getMockResponse, restoreMockFetch, } from './utils'; -import {} from './__fixtures__'; +import { + MOCK_ORGS_BY_USER_SUCCESS_RESPONSE, + MOCK_TRANSFORMED_ORGS_BY_USER, +} from './__fixtures__'; + +import request from '../request'; +import {OrganizationsApiorganizationsApiimport { + mockFetch, + getMockFetch, + getMockResponse, + restoreMockFetch, +} from './utils'; +import { + MOCK_ORGS_BY_USER_SUCCESS_RESPONSE, + MOCK_TRANSFORMED_ORGS_BY_USER, +} from './__fixtures__'; import request from '../request'; -import {OrganizationsApi} from '../organizations'; +import { OrganizationsApi } from '../organizations'; + +describe('OrganizationsApi', () => { + describe('getByUser()', () => { + it('calls fetch and calls fetch with appropriate defaults', async() => { + const organizations = new OrganizationsApi(request); + + mockFetch(getMockResponse(MOCK_ORGS_BY_USER_SUCCESS_RESPONSE)); + + await expect(organizations.getByUser('fake_id')).resolves.toEqual( + MOCK_TRANSFORMED_ORGS_BY_USER + ); + + expect(getMockFetch()).toHaveBeenCalledTimes(1); + expect(getMockFetch()).toHaveBeenCalledWith( + '/users/fake_id/organizations/', + expect.objectContaining({}) + ); + + restoreMockFetch(); + }); + + it('handle token missing requests', async() => { + const organizations = new OrganizationsApi(request); + + mockFetch( + getMockResponse( + { + status_code: 401, + error_description: + 'An OAuth token is required for all requests', + error: 'NO_AUTH', + }, + {status: 401} + ) + ); + + await expect( + organizations.getByUser('fake_id') + ).rejects.toMatchObject({ + response: expect.objectContaining({ + status: 401, + statusText: 'Unauthorized', + ok: false, + }), + parsedError: { + description: 'An OAuth token is required for all requests', + error: 'NO_AUTH', + }, + }); + + restoreMockFetch(); + }); + }); +}); From f48ffd09d1c3074eddbe917651681152486411ff Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Tue, 29 Jan 2019 11:09:09 -0600 Subject: [PATCH 5/6] fix copy paste issue and lint issue on describe name --- .eslintrc.json | 3 ++- src/__tests__/organizations.spec.ts | 14 +------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index cda4c53..c6a8aea 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -21,7 +21,8 @@ "typescript/no-array-constructor": "error", "typescript/no-empty-interface": "error", "typescript/no-use-before-define": "error", - "typescript/type-annotation-spacing": "error" + "typescript/type-annotation-spacing": "error", + "jest/lowercase-name": "none" }, "settings": { "import/resolver": { diff --git a/src/__tests__/organizations.spec.ts b/src/__tests__/organizations.spec.ts index d4772f1..178f50d 100644 --- a/src/__tests__/organizations.spec.ts +++ b/src/__tests__/organizations.spec.ts @@ -10,19 +10,7 @@ import { } from './__fixtures__'; import request from '../request'; -import {OrganizationsApiorganizationsApiimport { - mockFetch, - getMockFetch, - getMockResponse, - restoreMockFetch, -} from './utils'; -import { - MOCK_ORGS_BY_USER_SUCCESS_RESPONSE, - MOCK_TRANSFORMED_ORGS_BY_USER, -} from './__fixtures__'; - -import request from '../request'; -import { OrganizationsApi } from '../organizations'; +import {OrganizationsApi} from '../organizations'; describe('OrganizationsApi', () => { describe('getByUser()', () => { From f8f813a6c1737befaa2e9ef38f96ba1d6713ff66 Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Tue, 29 Jan 2019 11:53:27 -0600 Subject: [PATCH 6/6] add jsdoc string for organization method --- src/organizations.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/organizations.ts b/src/organizations.ts index d5e1f6e..82fff65 100644 --- a/src/organizations.ts +++ b/src/organizations.ts @@ -13,6 +13,10 @@ export interface Organization { * API for working with Organizations */ export class OrganizationsApi extends BaseApi> { + /** + * Get organizations based off a user id. + * @param {string} userId + */ getByUser(userId: string) { return this.request(`/users/${userId}/organizations/`); }