0.6.0 • Published 11 months ago
xfetch-js v0.6.0
xfetch-js
A extremely simple fetch extension for modern browsers inspired by sindresorhus/ky.
Which aims to be as small as possible and easy to use.
Examaple
// get with query string
xf('https://postman-echo.com/get/', { qs: { foo: 'bar' } })
.json()
.then(console.log)
// post form
xf.post('https://postman-echo.com/post', { form: { foo: 'bar' } })
.json()
.then(console.log)
// post json with transforms
xf.post('https://postman-echo.com/post', { json: { foo: 'bar' } })
.json(r => r.data)
.then(console.log)
// extend, default baseURI in browser is document.baseURI
const xf2 = xf.extend({
baseURI: 'https://postman-echo.com/'
})
xf2.get('/get').then(console.log)
// HTTPError
xf.get('https://postman-echo.com/404').catch(e => {
assert(e instanceof xf.HTTPError)
console.log(e.response)
})
With node
const xf = require('xfetch-js')
// This one will pollute global.URL and global.URLSearchParams, then return a xfetch client with node-fetch
// If you want a pure xfetch instance, use require('xfetch-js/xfetch')
xf('https://postman-echo.com/get/', { qs: { foo: 'bar' } })
.json()
.then(console.log)
Main differences bewteen fetch and xfetch-js
credentials
is set tosame-origin
by default.- Throws error when
response.ok
is not true - Some useful methods to call, such as
get
,post
and so on... - Support some simple serialization, including
json
,urlencoded
andquerystring
- Support post transformation like
.json(r => r.body)
- Create another instance with default options
xf.extend({})
fetch(Request)
are not supported
API
See xfetch.base.d.ts.
License
0.6.0
11 months ago
0.5.0
4 years ago
0.4.0
5 years ago
0.3.4
5 years ago
0.3.3
5 years ago
0.3.2
5 years ago
0.3.1
5 years ago
0.3.0
6 years ago
0.2.3
6 years ago
0.2.2
6 years ago
0.2.1
6 years ago
0.2.0
6 years ago
0.1.6
6 years ago
0.1.5
6 years ago
0.1.4
6 years ago
0.1.3
6 years ago
0.1.2
6 years ago
0.1.1
6 years ago
0.1.0
6 years ago
0.0.9
6 years ago
0.0.8
6 years ago
0.0.7
6 years ago
0.0.6
6 years ago
0.0.5
6 years ago
0.0.4
6 years ago
0.0.3
6 years ago
0.0.2
6 years ago
0.0.1
6 years ago