1.2.0 • Published 5 years ago
simple-fetch-client v1.2.0
🌐 simple-fetch-client
A friendly fetch client
Table of Contents
Features
- Retries and timeouts on requests via tenacious-fetch
- Using middleware via nanomiddleware
- Parsing of responses via fetch-response-enhancer
Install
This project uses node and npm.
$ npm install simple-fetch-client
$ # OR
$ yarn add simple-fetch-client
Usage
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
- Fork it and create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request
License
MIT