Perform HTTP requests (preferably REST style!) from NodeJS or client-side javascript.
- Simple interface backed by middleware for complete flexibility
- Promise based, with cancellation support
- Automatic JSON serialization
- Optional, automatic validation of server responses
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
Using npm:
$ npm install @berkeleybross/restclient
Until this project reaches a 1.0
release, breaking changes will be released with a new minor version. For example 0.5.1
, and 0.5.4
will have the same API, but 0.6.0
will have breaking changes.
Perform a GET
request. The following performs a GET
to /api/search?term=foo%20bar
, and throws an error if the response code is not "success" (between 200 and 300)
return await this.client.$get(['/api/search', { term: 'foo bar' }])
Perform a PUT
request. The following performs a PUT
to '/api/users/5' with JSON body of { "name": "John Smith" }
.
It will throw an error only if their was an HTTP transport error (e.g. network disconnected). Otherwise it will return an enum (string) describing the result
async function example() {
const response = await this.client.put(['/api/users/{id}', { id: 5 }], {
name: 'John Smith'
})
// Could also check response.success !== false
if (response.status === 201) {
return 'Created'
} else {
return 'Unknown error'
}
}
For more examples, please read the Wiki.
For any issues or feature requests, we would really appreciate it if you report them using our issue tracker.
Contributing to Coding with Chrome is subject to the guidelines in the CONTRIBUTING.md file, which, in brief, requires that contributors sign the [Individual Contributor License Agreement (CLA)][3].
For more information about develop for Coding with Chrome, please check doc/DEVELOPMENT.md
Released under The MIT License. Copyright (c) berkeleybross.
RestClient is made possible by other open source software.