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 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
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();
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