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

feat(request): Add support for SDK request method #8

Merged
merged 2 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ Coming soon...

## Usage

Coming soon...
```js
const sdk = require('eventbrite')({token: 'OATH_TOKEN_HERE'});

// See: https://www.eventbrite.com/developer/v3/endpoints/users/#ebapi-get-users-id
sdk.request('/users/me').then(res => {
Copy link
Contributor

Choose a reason for hiding this comment

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

so I think we shouldn't be encouraging people to use .request method directly and instead provide them wrappers for particular objects like getUser or getEvents so that when the endpoint changes the api kit will mask the change

Choose a reason for hiding this comment

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

I agree! I think it serves a purpose though and I think we should definitely make it available to our consumers. Maybe when the rest of our main apis get fleshed out we can move those to the forefront of our documentation, and note that they are slightly more stable and preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. When we have the wrappers this example will get replaced. But the nice thing is that someone (like core) could start using it now with just .request()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I imagine a use-case for .request() even when we do have the wrappers is if a new API is added and we haven't gotten around to building the wrapper for it. I believe it should always be made available in the SDK, just not in the forefront of docs

Copy link
Contributor

Choose a reason for hiding this comment

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

yup, exactly. Basically it's you're on your own method because we can change/deprecate the endpoints, whereas with the wrappers you're guaranteed to get some type of response back

// handle response data
});
```

Read more on [getting a token](https://www.eventbrite.com/developer/v3/api_overview/authentication/#ebapi-getting-a-token).

## Contributing

Coming soon...

## Project philosophy

We take the stability of this SDK **very** seriously. `brite-rest` follows the [SemVer](http://semver.org/) standard for versioning.
We take the stability of this SDK **very** seriously. `eventbrite` follows the [SemVer](http://semver.org/) standard for versioning.

## License

Expand Down
12 changes: 12 additions & 0 deletions definitions/url-lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module "url-lib" {
export function formatQuery(queryParams: {}): string;
export function formatQuery(queryParamsList: Array<{}>): string;

export function formatUrl(urlPath: string, queryParams: {}): string;
export function formatUrl(
urlPath: string,
queryParamsList: Array<{}>
): string;

export function parseQuery(serializedQuery: string): {};
}
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"jsnext:main": "lib/esm/index.js",
"browser": "dist/brite-rest.js",
"types": "lib/cjs/index.d.ts",
"keywords": ["rest", "api", "sdk", "events", "tickets", "eventbrite"],
"keywords": [
"rest",
"api",
"sdk",
"events",
"tickets",
"eventbrite"
],
"repository": {
"type": "git",
"url": "https://github.com/eventbrite/eventbrite-sdk-javascript.git"
Expand All @@ -33,11 +40,15 @@
"validate": "npm-run-all --parallel check:static test:ci"
},
"lint-staged": {
"*.{ts,js}": ["yarn format", "git add"]
"*.{ts,js}": [
"yarn format",
"git add"
]
},
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"lodash": "^4.17.5"
"lodash": "^4.17.5",
"url-lib": "^2.0.2"
},
"resolutions": {
"babel-core": "^7.0.0-bridge.0"
Expand All @@ -52,6 +63,7 @@
"@types/isomorphic-fetch": "^0.0.34",
"@types/jest": "^22.1.3",
"@types/lodash": "^4.14.104",
"@types/node": "^9.4.6",
"babel-eslint": "^7.0.0",
"eslint": "^3.0.0",
"eslint-config-eventbrite": "^4.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"parser": "typescript-eslint-parser",
"plugins": ["typescript"],
"rules": {
"no-undef": "off"
"no-undef": "off",
"camelcase": "off"
},
"settings": {
"import/resolver": {
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/__fixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const MOCK_USERS_ME_RESPONSE_DATA = {
emails: [
{
email: '[email protected]',
verified: true,
primary: true,
},
],
id: '142429416488',
name: 'Eventbrite Engineer',
first_name: 'Eventbrite',
last_name: 'Engineer',
is_public: false,
image_id: null as string,
};

export const MOCK_ERROR_RESPONSE_DATA = {
status_code: 400,
error: 'INVALID_TEST',
error_description: 'This is an invalid test',
};
90 changes: 90 additions & 0 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import eventbrite from '../';
import {
mockFetch,
getMockFetch,
restoreMockFetch,
getMockResponse
} from './utils';
import {MOCK_USERS_ME_RESPONSE_DATA} from './__fixtures__';

describe('configurations', () => {
it('does not error when creating sdk object w/o configuration', () => {
expect(() => eventbrite()).not.toThrow();
});
});

describe('request', () => {
const MOCK_TOKEN = 'MOCK_TOKEN';
const MOCK_BASE_URL = '/api/v3';

beforeEach(() => {
mockFetch(getMockResponse(MOCK_USERS_ME_RESPONSE_DATA));
});

afterEach(() => {
restoreMockFetch();
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

});

it('makes request to API base url default w/ no token when no configuration is specified', async () => {
const {request} = eventbrite();

await expect(request('/users/me/')).resolves.toEqual(
MOCK_USERS_ME_RESPONSE_DATA
);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
'https://www.eventbriteapi.com/v3/users/me/',
expect.objectContaining({})
);
});

it('makes request to API base url override w/ specified token', async () => {
const {request} = eventbrite({
token: MOCK_TOKEN,
baseUrl: MOCK_BASE_URL,
});

await expect(request('/users/me/')).resolves.toEqual(
MOCK_USERS_ME_RESPONSE_DATA
);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
`${MOCK_BASE_URL}/users/me/?token=${MOCK_TOKEN}`,
expect.objectContaining({})
);
});

it('properly appends token to API URL when endpoint already contains query parameters', async () => {
const {request} = eventbrite({
token: MOCK_TOKEN,
});

await expect(
request('/users/me/orders/?time_filter=past')
).resolves.toEqual(MOCK_USERS_ME_RESPONSE_DATA);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
`https://www.eventbriteapi.com/v3/users/me/orders/?time_filter=past&token=${MOCK_TOKEN}`,
expect.objectContaining({})
);
});

it('properly passes through request options', async () => {
const {request} = eventbrite();
const requestOptions = {
method: 'POST',
body: JSON.stringify({plan: 'package2'}),
};

await request('/users/:id/assortment/', requestOptions);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
'https://www.eventbriteapi.com/v3/users/:id/assortment/',
expect.objectContaining(requestOptions)
);
});
});
Loading