1.6.0 • Published 4 years ago

the-fetch v1.6.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

the-fetch

Original Fetch API with some additional helpful feature

Feature

  • Global onError callback function
  • Global base URL
  • Request & Response Intecpetors

Browser Support

Sorry ~ Only support the modern browsers

Installation

npm i the-fetch --save

Quick Start

import fetch from 'the-fetch'

fetch.defaults.base = 'https://www.xxx.com/' // set base URL

/**
 * If not set the response will resolve to json like
 * `fetch('').then(res => res.json())`
 * so in the example below you will see you can get json data only resolve once
 * If you want to resolve the res in another format you can pass the resolver in options like
 * `fetch('', { resolver: 'text' })` will resolve as `res.text()`
 * 
 */
fetch.get('/getData').then(res => console.log(res))
// or
fetch('/getData').then(res => console.log(res))

fetch.post('/addData', data, init).then(res => console.log(res))
// or
fetch('/addData', {
  method: 'POST',
  body: data
}).then(res => console.log(res))

Interceptor

import fetch from 'the-fetch'

// Use request interceptor to change the initial config
fetch.interceptors.request.use(config => {
  // this config is the final config that will pass to fetch(url, config)
  config.extra = 'Hello'
  return config
}, err => {
  return Promise.reject(err)
})

// Use response interceptor to do some extra work when request finished
fetch.interceptors.response.use(response => {
  return response
}, err => {
  console.log(err.message)
  return Promise.reject(err)
})
1.6.0

4 years ago

1.5.0

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

5 years ago

1.2.10

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago