Skip to content

Commit

Permalink
feat(request): Add support for SDK request method (#8)
Browse files Browse the repository at this point in the history
Adds `sdk.request(url, fetchOptions)` that can be used to make any API request without having a convience method for it.

It reads in the `token` & `baseUrl` properties that are passed in while creating the `sdk` object in order to generate the full URL.

Sample usage:

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

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

// See: https://www.eventbrite.com/developer/v3/endpoints/users/#ebapi-get-users-id
sdk.request('/users/me').then(res => {
    // handle response data
});
```

Also:
- Renamed package from `brite-rest` to `eventbrite`
- Updated README to reflect sample usage
- Moved `index.spec.ts` into `__tests__` folder
- Had to disable `camelcase` ESLint rule because Prettier was unquoting non-conforming property names and the `camelcase` rule doesn't have fix functionality for prettier-eslint to fix
- Added a bunch of `eslint-plugin-typescript` rules

NOTE: Per https://www.eventbrite.com/developer/v3/api_overview/authentication/#ebapi-authenticating-requests we should move the token into an authorization header instead of a query parameter, which is preferred. It'd also allow us to remove the `url-lib` dependency (and the one-off Typescript definition file).

Fixes #6
  • Loading branch information
benmvp authored and BenAtEventbrite committed Mar 1, 2018
1 parent 45901bd commit 149b0a1
Show file tree
Hide file tree
Showing 13 changed files with 603 additions and 37 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ Coming soon...

## Usage

Coming soon...
```js
const eventbrite = require('eventbrite');

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

// See: https://www.eventbrite.com/developer/v3/endpoints/users/#ebapi-get-users-id
sdk.request('/users/me').then(res => {
// 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): {};
}
20 changes: 16 additions & 4 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,11 +63,12 @@
"@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",
"eslint-plugin-import": "^2.0.0",
"eslint-plugin-typescript": "^0.8.1",
"eslint-plugin-typescript": "^0.9.0",
"husky": "^0.14.3",
"jest": "^22.4.0",
"lint-staged": "^6.1.0",
Expand Down
14 changes: 13 additions & 1 deletion src/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
"parser": "typescript-eslint-parser",
"plugins": ["typescript"],
"rules": {
"no-undef": "off"
"no-undef": "off",
"camelcase": "off",
"typescript/adjacent-overload-signatures": "error",
"typescript/class-name-casing": "error",
"typescript/interface-name-prefix": "error",
"typescript/member-delimiter-style": "error",
"typescript/no-unused-vars": "error",
"typescript/member-ordering": "error",
"typescript/no-angle-bracket-type-assertion": "error",
"typescript/no-array-constructor": "error",
"typescript/no-empty-interface": "error",
"typescript/no-use-before-define": "error",
"typescript/type-annotation-spacing": "error"
},
"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();
});

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

0 comments on commit 149b0a1

Please sign in to comment.