-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request): Add support for SDK request method
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 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 => { // 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 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. Fixes #6.
- Loading branch information
Showing
12 changed files
with
586 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.