https://github.com/raphaelameaume/json-fetcher
simple fetch wrapper
https://github.com/raphaelameaume/json-fetcher
Last synced: 3 months ago
JSON representation
simple fetch wrapper
- Host: GitHub
- URL: https://github.com/raphaelameaume/json-fetcher
- Owner: raphaelameaume
- License: mit
- Created: 2016-05-15T14:09:21.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-05-15T14:44:58.000Z (about 9 years ago)
- Last Synced: 2025-01-18T05:43:21.592Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# json-fetcher
Simple fetch wrapper.
## /!\ None of this code has been tested. Do not use it for now. Work in progress.
[](https://travis-ci.org/raphaelameaume/json-fetcher)
[](https://david-dm.org/raphaelameaume/json-fetcher)
[](https://david-dm.org/raphaelameaume/json-fetcher#info=devDependencies)## Requirements
`json-fetcher` doesn't provide any fetch and Promise polyfills. You need to provide your own before using `json-fetcher`s. If you are using `json-fetcher` in a browser environment, you can use [Github's fetch polyfill](https://github.com/github/fetch). For a node environment, use [Matthew Andrews polyfill](https://github.com/matthew-andrews/isomorphic-fetch/).## How to use
```npm install json-fetcher --save```
```javascript
import jsonFetcher from 'json-fetcher';// You can create a new instance of json-fetcher
const api = new jsonFetcher({
baseUrl: '/api',
headers: ...
});
const users = api.get('/users');// or directly use
const users = jsonFetcher.get('/api/users');
```## Response as a promise
json-fetcher always returns a promise, that can be rejected if the server didn't respond or if the response doesn't contains any of these informations `response.success || response.statusCode === 200 || response.status === 200`.```javascript
jsonFetcher.get('/api/users')
.then( response => {
console.log(response);
})
.catch( error => {
console.error(error);
})
```