A extremely simple fetch extension for modern browsers inspired by sindresorhus/ky.
Which aims to be as small as possible and easy to use.
// 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)
})
const xf = require('xfetch-js').create(require('node-fetch'))
xf('https://postman-echo.com/get/', { qs: { foo: 'bar' } })
.json()
.then(console.log)
Otherwise, you will get an error about global.URL
,global.URLSearchParams
is undefined.
Of course, you can polyfill them by youself to use this module.
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({})
See xfetch.base.d.ts.