Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add organizations api #54

Merged
merged 6 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions docs/organizations.md
Original file line number Diff line number Diff line change
@@ -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)

<a id="getByUser"></a>

## `sdk.organizations.getByUser(id)`
Gets the details for a specific user by their user id.

**Read [`/users/:userId/organizations/` documentation](organization-by-user) for more details.**

### API
```js
sdk.organizations.getByUser(userId: string): Promise<Paginated<Organization[]>>
```

### Example

```js
const eventbrite = require('eventbrite');

// Create configured Eventbrite SDK
const sdk = eventbrite({token: 'OATH_TOKEN_HERE'});

sdk.organizations.getByUser('1234567890').then((paginatedResponse) => {
console.log(`Here are your organizations: ${paginatedResponse.organizations.join(' ')}.`);
});
```

<!-- link reference section -->
[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/organization/list-organizations-by-user
50 changes: 50 additions & 0 deletions src/__tests__/__fixtures__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,53 @@ 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 = {
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',
name: 'Organization 1',
},
{
id: '2',
name: 'Organization 2',
},
{
id: '3',
name: 'Organization 3',
},
],
};

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',
},
],
};
67 changes: 67 additions & 0 deletions src/__tests__/organizations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
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';

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();
});
});
});
23 changes: 23 additions & 0 deletions src/organizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {BaseApi} from './baseApi';
import {PaginatedResponse} from './types';

export interface Organization {
id: string;
name: string;
imageId: string;
locale?: string;
vertical?: 'Default' | 'Music';
}

/**
* API for working with Organizations
*/
export class OrganizationsApi extends BaseApi<PaginatedResponse<Organization>> {
/**
* Get organizations based off a user id.
* @param {string} userId
*/
getByUser(userId: string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems silly, but maybe add some docs to each method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc update pushed

return this.request(`/users/${userId}/organizations/`);
}
}
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
pagination: Pagination;
[key: string]: T[] | Pagination;
}