-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(request): Fix request() w/ relevant docs (#36)
The intention of this PR was just to write the docs for the `request()` method. But while writing the docs, I realized that the error handling was broken in certain cases. So that got fixed along with the tests. So what ended up happening: - Wrote docs for configuring an SDK object - Wrote docs for lowest-level `request()` method - Made `request()` the only export in `request.tsc` - Fixed error handling in `request.tsc` - Updated tests to only test `request()` and to test for more error cases - Moved `PULL_REQUEST_TEMPLATE.md` & `CONTRIBUTING.md` from #33
- Loading branch information
1 parent
f126268
commit fd25694
Showing
8 changed files
with
526 additions
and
321 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
<!--- Provide a general summary of your changes in the Title above --> | ||
|
||
## Description | ||
|
||
<!--- Describe your changes in detail --> | ||
|
||
<!--- Please include the phrase "BREAKING CHANGE:" here if your require a major release --> | ||
|
||
<!--- Don't forget to note any issues here with "fixes #<issue number>" --> | ||
|
||
## How Has This Been Tested? | ||
|
||
<!--- Please describe in detail how you tested your changes. --> | ||
|
||
<!--- For bug fixes, include regression unit tests that fail without the fix --> | ||
|
||
<!--- For new features, include unit tests for the new functionality --> | ||
|
||
## Screenshots (if appropriate): | ||
|
||
## Checklist: | ||
|
||
<!--- Please mark an `x` in all the boxes that apply. --> | ||
|
||
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> | ||
- [ ] I have read the [**CONTRIBUTING** document](../CONTRIBUTING.md). | ||
- [ ] I have updated the documentation accordingly. | ||
- [ ] I have added tests to cover my changes. | ||
- [ ] I have run yarn validate to ensure that tests, typescript and linting are all in order. | ||
|
||
* [ ] I have read the [**CONTRIBUTING** document](https://github.com/eventbrite/eventbrite-sdk-javascript/blob/master/CONTRIBUTING.md). | ||
* [ ] I have updated the documentation accordingly. | ||
* [ ] I have added tests to cover my changes. | ||
* [ ] I have run `yarn validate` to ensure that tests, typescript and linting are all in order. |
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 |
---|---|---|
@@ -1,71 +1,76 @@ | ||
#Contributing | ||
# Contributing | ||
|
||
Thank you for your interest in contributing to Eventbrite's Javascript SDK! | ||
|
||
###Table of Contents | ||
* [Workflow](#workflow) | ||
* [Setup](#setup) | ||
* [Branches](#using-branches-to-submit-changes) | ||
* [Keeping up to date](#keeping-your-local-repo-up-to-date) | ||
* [Creating issues](#creating-issues) | ||
* [Working on and submitting changes](#working-on-and-submitting-changes) | ||
* [Steps to submit](#steps-to-submit) | ||
### Table of Contents | ||
|
||
* [Workflow](#workflow) | ||
* [Setup](#setup) | ||
* [Branches](#using-branches-to-submit-changes) | ||
* [Keeping up to date](#keeping-your-local-repo-up-to-date) | ||
* [Creating issues](#creating-issues) | ||
* [Working on and submitting changes](#working-on-and-submitting-changes) | ||
* [Steps to submit](#steps-to-submit) | ||
|
||
## Workflow | ||
|
||
### Setup | ||
|
||
1. This project uses [yarn](https://yarnpkg.com/en/) as a package management system. If you don't have it installed, you can follow the instructions [here](https://yarnpkg.com/lang/en/docs/install/) | ||
1. Fork the repository. [need help?](https://help.github.com/articles/fork-a-repo/) | ||
1. Clone your new forked repository to your local computer | ||
1. Set the Eventbrite repository as your branches upstream branch | ||
`git remote add upstream https://github.com/eventbrite/eventbrite-sdk-javascript.git` | ||
1. Navigate to the root directory of your newly cloned repository | ||
(for mac `cd /path/to/eventbrite-sdk-javascript`) | ||
1. `yarn install` to install local dependencies | ||
1. This project uses [yarn](https://yarnpkg.com/en/) as a package management system. If you don't have it installed, you can follow the instructions [here](https://yarnpkg.com/lang/en/docs/install/) | ||
1. Fork the repository. [need help?](https://help.github.com/articles/fork-a-repo/) | ||
1. Clone your new forked repository to your local computer | ||
1. Set the Eventbrite repository as your branches upstream branch | ||
`git remote add upstream https://github.com/eventbrite/eventbrite-sdk-javascript.git` | ||
1. Navigate to the root directory of your newly cloned repository | ||
(for mac `cd /path/to/eventbrite-sdk-javascript`) | ||
1. `yarn install` to install local dependencies | ||
|
||
### Using branches to submit changes | ||
To work on changes to the Eventbrite repository, create a new branch on your local repository. `git checkout -b <your-new-branch-name>` | ||
|
||
To work on changes to the Eventbrite repository, create a new branch on your local repository. `git checkout -b <your-new-branch-name>` | ||
|
||
### Keeping your local repo up to date | ||
|
||
To ensure your branch never gets out of sync with Eventbrite's master, ensure that you have your upstream set properly (see the [Setup](#setup) step) | ||
|
||
1. `git checkout master` (you may have to [stash or commit][stash-docs] your local changes if on a new branch) | ||
1. `git pull upstream master` | ||
1. `git checkout <your-new-branch-name>` | ||
1. `git rebase master` | ||
1. If you've stashed changes, [unstash][stash-docs] them now, otherwise your branch should now be up to date | ||
1. `git checkout master` (you may have to [stash or commit][stash-docs] your local changes if on a new branch) | ||
1. `git pull upstream master` | ||
1. `git checkout <your-new-branch-name>` | ||
1. `git rebase master` | ||
1. If you've stashed changes, [unstash][stash-docs] them now, otherwise your branch should now be up to date | ||
|
||
Always try to keep your master 'clean' by only pulling changes directly from upstream into your master branch and rebasing those changes onto your working branch. | ||
|
||
It is always a good idea to pull the upstream branch in to your master branch before creating a new feature branch to work from. This will minimize the chances of encountering merge conflicts. | ||
|
||
## Creating Issues | ||
|
||
Create issues to file bugs, changes, and proposals. | ||
|
||
Before opening a new issue, please [search][issues] to see if there has been previous discussion about the same feature or issue. If so, please contribute to the discussion there. | ||
Before opening a new issue, please [search][issues] to see if there has been previous discussion about the same feature or issue. If so, please contribute to the discussion there. | ||
|
||
If nothing is found, feel free to [open a new issue][issues] and fill out the issue template to the best of your ability. | ||
|
||
## Working on and submitting changes | ||
When starting on improvements or new features that are non-trivial, it is always a good idea to first discuss the changes you wish to implement by [opening a github issue][issues] before getting started. | ||
|
||
When starting on improvements or new features that are non-trivial, it is always a good idea to first discuss the changes you wish to implement by [opening a github issue][issues] before getting started. | ||
|
||
If you've found a bug or feature you'd like to work on in our [github issue tracker][issues], please comment on the issue to let others know that you'd like to work on it. | ||
|
||
While implementing fixes, please try to change as little code as possible. This helps speed up the review process and helps diminish the chance of additional bugs. | ||
While implementing fixes, please try to change as little code as possible. This helps speed up the review process and helps diminish the chance of additional bugs. | ||
|
||
Please try to conform to the coding style of the code base. | ||
|
||
###Steps to submit: | ||
|
||
1. Please ensure that your changes are fully covered by one or more unit test(s). | ||
1. Check to make sure that your changes are documented properly (inline comments for interesting lines, READMEs, etc.) | ||
1. Run `yarn validate` to ensure that all tests pass, the linter is satisfied and your changes are typescript compliant. | ||
1. PR titles must be prefixed by the type of changes the PR contains followed by the scope of what the pr touches. We are following the [angular commit guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines). Please use one of `feat, fix, docs, style, refactor, perf, test, chore` as the prefix. The scope is the the direct product your changes affect. Example: `chore(build): Add encrypted ssh key for semantic-release` because its a chore and it touches the build. | ||
* For multiple scope items, you can comma separate 2 or 3 but if there are more than that please use a `*` instead. | ||
1. Please use a [closing issue keyword](https://help.github.com/articles/closing-issues-using-keywords/) to indicate the issue that your fix addresses in the description section of the pull request template. Example: `fixes #32` to close issue #32 | ||
1. Please ensure that your changes are fully covered by one or more unit test(s). | ||
1. Check to make sure that your changes are documented properly (inline comments for interesting lines, READMEs, etc.) | ||
1. Run `yarn validate` to ensure that all tests pass, the linter is satisfied and your changes are typescript compliant. | ||
1. PR titles must be prefixed by the type of changes the PR contains followed by the scope of what the pr touches. We are following the [angular commit guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines). Please use one of `feat, fix, docs, style, refactor, perf, test, chore` as the prefix. The scope is the the direct product your changes affect. Example: `chore(build): Add encrypted ssh key for semantic-release` because its a chore and it touches the build. | ||
|
||
* For multiple scope items, you can comma separate 2 or 3 but if there are more than that please use a `*` instead. | ||
|
||
1. Please use a [closing issue keyword](https://help.github.com/articles/closing-issues-using-keywords/) to indicate the issue that your fix addresses in the description section of the pull request template. Example: `fixes #32` to close issue #32 | ||
|
||
[issues]: https://github.com/eventbrite/eventbrite-sdk-javascript/issues | ||
[stash-docs]: https://git-scm.com/book/en/v1/Git-Tools-Stashing |
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,51 @@ | ||
# Eventbrite JavaScript SDK Documentation | ||
|
||
This SDK interface closely mirors the [Eventbrite v3 REST API](https://www.eventbrite.com/developer/v3/) endpoints that it wraps. The SDK provides many conveniences for making requests and processing responses to make it easier to use in the JavaScript environment. | ||
|
||
## ToC | ||
|
||
* [Including the package](#including-the-package) | ||
* [Configuring a SDK object](#configuring-a-sdk-object) | ||
* [`request()`](./request) | ||
|
||
## Including the package | ||
|
||
First include the `eventbrite` package (depending on your module environment): | ||
|
||
### Webpack / Rollup / etc ([ECMAScript modules](https://unpkg.com/eventbrite/lib/esm/)): | ||
|
||
```js | ||
import eventbrite from 'eventbrite'; | ||
``` | ||
|
||
### Node / legacy dependency systems ([CommonJS](https://unpkg.com/eventbrite/lib/cjs/) / [Universal Module Definition](https://unpkg.com/eventbrite/lib/umd/)): | ||
|
||
```js | ||
const eventbrite = require('eventbrite'); | ||
``` | ||
|
||
### `<script>` [distribution bundle](https://unpkg.com/eventbrite/dist/) include: | ||
|
||
``` | ||
<script src="https://unpkg.com/eventbrite/dist/eventbrite.min.js"></script> | ||
``` | ||
|
||
NOTE: `window.Eventbrite` will be a reference to the package. | ||
|
||
## Configuring a SDK object | ||
|
||
In order to make requests, you need to configure the SDK object. | ||
|
||
```js | ||
import eventbrite from 'eventbrite'; | ||
|
||
// Create configured Eventbrite SDK | ||
const sdk = eventbrite({token: 'OATH_TOKEN_HERE'}); | ||
``` | ||
|
||
You can configure the SDK object with the following properties: | ||
|
||
* `token` - The Eventbrite [OAuth token](https://www.eventbrite.com/developer/v3/api_overview/authentication/#ebapi-getting-a-token) | ||
* `baseUrl` - The base URL prepending to endpoints when making API requests (defaults to `'https://www.eventbriteapi.com/v3'`). So when using the `'/users/me/'` endpoint, a request would be made to `https://www.eventbriteapi.com/v3/users/me/`. _NOTE: You probably will not need to use this property._ | ||
|
||
From then on, you can use `sdk` to make API requests. |
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,156 @@ | ||
# `request()` | ||
|
||
`request()` is the Promise-based, low-level function for making [`fetch`](https://github.com/matthew-andrews/isomorphic-fetch) requests to the Eventbrite v3 REST API, returning responses as JSON. The higher-level convenience endpoint functions use `request()` under the hood for making their requests. We suggest that you use the convenience endpoint functions over `request()`. But there may be cases where new or updated endpoints exist withing the REST API, and the SDK has not yet been updated to provide convenience functions. | ||
|
||
`request()` provides additional request and response handling over `fetch`. | ||
|
||
For requests it: | ||
|
||
* Prepends a configurable base URL to the endpoint you specify (see [Configuring a SDK object](./#configuring-a-sdk-object)) | ||
* Adds your [OAuth token](https://www.eventbrite.com/developer/v3/api_overview/authentication/#ebapi-getting-a-token) in the request `Authorization` header | ||
* Sets the appropriate `Content-type` header depending on the `fetch` `method` (`GET`, `POST`, etc.) configuration you use | ||
* Sets the appropriate `credentials` setting depending on the `fetch` `mode` (`cors`, etc.) configuration you use | ||
|
||
For responses it: | ||
|
||
* Returns a resolved `Promise` with the response data parsed as JSON | ||
* If the HTTP status is in the 400 or 500 range, returns a rejected `Promise` with parsed API errors, [if applicable](#error-handling) | ||
|
||
## API | ||
|
||
The TypeScript function definition of `request()` is: | ||
|
||
``` | ||
(endpoint: string, options?: RequestInit): Promise<{}> | ||
``` | ||
|
||
### Parameters | ||
|
||
`request()` accepts the following parameters: | ||
|
||
* `endpoint`: The Eventbrite v3 API endpoint path, such as `/users/me/`. This will be appended to the `baseUrl` defined when [configuring the SDK object](./#configuring-a-sdk-object). | ||
* `options`: The request initialization options that [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) accepts. Your [OAuth token](https://www.eventbrite.com/developer/v3/api_overview/authentication/#ebapi-getting-a-token) will be added to the request `Authorization` header for you. Some additional options you may need to pass in are: | ||
* `options.method`: The [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) (e.g. `GET`, `POST`, etc.) for the fetch. Non-`GET` requests add `application/json` as `Content-Type` by default. | ||
* `options.mode`: The request mode (e.g. `cors`, `same-origin`, etc.) for the fetch. Defaults the `credentials` option to `include` when `mode` is `cors`. Otherwise the `credentials` default to `same-origin`. | ||
|
||
### Response | ||
|
||
The return value from `request()` is a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) that contains the response from the API call. If the response is successful, the response data will be parsed as JSON. | ||
|
||
See the [Error Handling](#error-handling) section for more information on the default error handling that `request()` provides. | ||
|
||
## Examples | ||
|
||
The simplest approach is to use ES2015 Promises: | ||
|
||
```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(user => { | ||
console.log(`Hi ${user.name}!`); | ||
}); | ||
``` | ||
|
||
## Error Handling | ||
|
||
To make interacting with the Eventbrite API easier, the SDK handles and parses some additional errors by default. | ||
|
||
When an error occurs during an API request, the Eventbrite v3 API will send a response with an error HTTP status (in the 400 or 500 range), as well as a JSON response containing more information about the error: | ||
|
||
```json | ||
{ | ||
"status_code": 404, | ||
"error_description": "The user you requested does not exist.", | ||
"error": "NOT_FOUND" | ||
} | ||
``` | ||
|
||
The SDK recognizes that an error has occurred (by inspecting the HTTP status code) and returns a **rejected** promise with an object that contains the errored response as the `response` property and error information in the `parsedError` property. This way you can easily distinguish whether or not your API request succeeded or failed: | ||
|
||
```js | ||
const eventbrite = require('eventbrite'); | ||
|
||
// Create configured Eventbrite SDK | ||
const sdk = eventbrite({token: 'OATH_TOKEN_HERE'}); | ||
|
||
// BAD User ID | ||
const userId = '123456789'; | ||
|
||
// See: https://www.eventbrite.com/developer/v3/endpoints/users/#ebapi-get-users-id | ||
sdk | ||
.request(`/users/${userId}`) | ||
.then(user => { | ||
// Successful response | ||
console.log(`Hi ${user.name}!`); | ||
}) | ||
.catch(errInfo => { | ||
// An error occurred | ||
// Original error response is passed in `response` property | ||
console.error(errInfo.response['error_description']); | ||
|
||
// `ARGUMENT_ERROR` errors are parsed into `parsedError` property | ||
const parsedError = errorInfo.parsedError; | ||
|
||
// equivalent to errorInfo.response.error | ||
console.log(parsedError.error); | ||
|
||
// equivalent to errorInfo.response['error_desscripion'] | ||
console.log(parsedError.description); | ||
}); | ||
``` | ||
|
||
Read more about [Errors](https://www.eventbrite.com/developer/v3/api_overview/errors/) within the Eventbrite v3 API. | ||
|
||
One of the [Common Errors](https://www.eventbrite.com/developer/v3/api_overview/errors/#ebapi-common-errors) in the Eventbrite v3 API, is the `ARGUMENTS_ERROR` error (returned with `400` HTTP code). This happens when one of the parameters passed to the API call is invalid. You would get a response like: | ||
|
||
```json | ||
{ | ||
"status_code": 400, | ||
"error_detail": { | ||
"ARGUMENTS_ERROR": { | ||
"status": ["INVALID"] | ||
} | ||
}, | ||
"error_description": | ||
"There are errors with your arguments: status - INVALID", | ||
"error": "ARGUMENTS_ERROR" | ||
} | ||
``` | ||
|
||
It includes an `error_detail` property that contains additional data about the offending parameters. The SDK parses the `ARGUMENT_ERROR` data within `error_detail` adding it to the `parsedError` property in the rejected promise as the `argumentErrors` property: | ||
|
||
```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-events | ||
sdk | ||
.request('/users/me/events?status=blah') | ||
.then(user => { | ||
// Successful response | ||
console.log(`Hi ${user.name}!`); | ||
}) | ||
.catch(errInfo => { | ||
// An error occurred | ||
// Original error response is passed in `response` property | ||
console.error(errInfo.response['error_description']); | ||
|
||
// `ARGUMENT_ERROR` errors are parsed into `parsedError` property | ||
const parsedError = errorInfo.parsedError; | ||
|
||
// equivalent to errorInfo.response['error_detail']['ARGUMENT_ERROR'] | ||
console.log(parsedError.argumentErrors); | ||
|
||
// equivalent to errorInfo.response.error (would be "ARGUMENT_ERROR") | ||
console.log(parsedError.error); | ||
|
||
// equivalent to errorInfo.response['error_desscripion'] | ||
console.log(parsedError.description); | ||
}); | ||
``` |
Oops, something went wrong.