simple-fetch-client

1.2.0 • Public • Published

🌐 simple-fetch-client

package version package downloads standard-readme compliant package license make a pull request

A friendly fetch client

Table of Contents

Features

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

  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

Package Sidebar

Install

npm i simple-fetch-client

Weekly Downloads

0

Version

1.2.0

License

MIT

Unpacked Size

6.14 kB

Total Files

5

Last publish

Collaborators

  • tiaanduplessis