https://github.com/tiaanduplessis/simple-fetch-client
A friendly fetch client
https://github.com/tiaanduplessis/simple-fetch-client
client fetch
Last synced: 11 months ago
JSON representation
A friendly fetch client
- Host: GitHub
- URL: https://github.com/tiaanduplessis/simple-fetch-client
- Owner: tiaanduplessis
- Created: 2018-06-23T21:05:08.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-07T04:07:04.000Z (about 6 years ago)
- Last Synced: 2025-07-12T15:45:45.285Z (12 months ago)
- Topics: client, fetch
- Language: JavaScript
- Size: 399 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 🌐 simple-fetch-client
[](https://npmjs.org/package/simple-fetch-client)
[](https://npmjs.org/package/simple-fetch-client)
[](https://github.com/RichardLitt/standard-readme)
[](https://npmjs.org/package/simple-fetch-client)
[](http://makeapullrequest.com)
> A friendly fetch client
## Table of Contents
- [🌐 simple-fetch-client](#simple-fetch-client)
- [Table of Contents](#table-of-contents)
- [Features](#features)
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)
## Features
- Retries and timeouts on requests via [tenacious-fetch](https://github.com/tiaanduplessis/tenacious-fetch)
- Using middleware via [nanomiddleware](https://github.com/tiaanduplessis/nanomiddleware)
- Parsing of responses via [fetch-response-enhancer](https://github.com/tiaanduplessis/fetch-response-enhancer)
## Install
This project uses [node](https://nodejs.org) and [npm](https://www.npmjs.com).
```sh
$ npm install simple-fetch-client
$ # OR
$ yarn add simple-fetch-client
```
## Usage
```js
import 'babel-polyfill'
import Client from 'simple-fetch-client'
const api = new Client('https://jsonplaceholder.typicode.com')
api.useMiddleware((res) => {
console.log(res.url, res.status)
return res
})
api.get('/posts/1').then(res => console.log(res.data.userId)) // 1
api.post('/posts', {hello: true}).then(res => console.log(res.data)) // Object {id: 101}
api.get('/posts', {params: {q: true}})
class MyAPI extends Client {
constructor (config = {}) {
super('https://jsonplaceholder.typicode.com', {
delay: 2000 // delay requests with 2 seconds
})
this.useMiddleware(res => {
if (config.log) {
console.log('MyAPI', res.url, res.data)
}
return res
})
}
async getPosts () {
const res = this.get('/posts').then(res => res.d)
return res.body
}
}
const myAPI = new MyAPI({log: true})
myAPI.getPosts().then(console.log)
```
## Contribute
1. Fork it and create your feature branch: git checkout -b my-new-feature
2. Commit your changes: git commit -am 'Add some feature'
3. Push to the branch: git push origin my-new-feature
4. Submit a pull request
## License
MIT