https://github.com/fed/fetch-wrapper
https://github.com/fed/fetch-wrapper
fetch fetch-api
Last synced: 19 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/fed/fetch-wrapper
- Owner: fed
- Created: 2020-04-07T10:57:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T11:08:29.000Z (almost 6 years ago)
- Last Synced: 2025-01-18T13:38:11.640Z (12 months ago)
- Topics: fetch, fetch-api
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fetch-wrapper
A simple fetch wrapper inspired on [axios](https://github.com/axios/axios) and [this post](https://kentcdodds.com/blog/replace-axios-with-a-simple-custom-fetch-wrapper) by Kent C. Dodds.
## Usage examples
### apiClient.get(endpoint, options)
```js
apiClient
.get('https://api.github.com/users')
.then(data => console.log(data))
.catch(err => console.error(err));
```
You can also pass in your authorization headers in case the endpoint is protected:
```js
const token = 'eyJhbGciOiJIv5cCI6IkpXVCJ9...';
const headers = {
'Authorization': `Bearer ${token}`
};
apiClient
.get('https://rawr.com/your-protected-endpoint', { headers })
.then(data => console.log(data))
.catch(err => console.error(err));
```
## apiClient.post(endpoint, body, options)
```js
const token = 'eyJhbGciOiJIv5cCI6IkpXVCJ9...';
const headers = {
'Authorization': `Bearer ${token}`
};
const body = {
foo: 'rawr'
};
apiClient
.post('https://rawr.com/something', body, { headers })
.then(data => console.log(data))
.catch(err => console.error(err));