A simple but elegant fetch
API wrapper with less than 850B
minified and brotlied, use fetch
like a charm
# npm
npm i x-fetch
# yarn
yarn add x-fetch
# pnpm
pnpm add x-fetch
# bun
bun add x-fetch
import { ApiMethod, createXFetch, interceptors, xfetch } from 'x-fetch'
// plain url, GET method
await xfetch('url')
// with options, `method`, `body`, `query`, etc.
await xfetch('url', {
method: ApiMethod.POST, // or 'POST'
// plain object or array, or BodyInit
body: {},
// URLSearchParametersOptions
query: {
key: 'value',
},
// json: boolean, // whether auto stringify body to json, default true for plain object or array, otherwise false
// type: 'arrayBuffer' | 'blob' | 'json' | 'text' | null, `null` means plain `Response`
})
const interceptor: ApiInterceptor = (req, next) => {
// do something with req
const res = await next(req)
// do something with res
return res
}
// add interceptor
interceptors.use(interceptor)
// remove interceptor
interceptors.eject(interceptor)
// create a new isolated `xfetch` with its own `interceptors`
const { xfetch, interceptors } = createXFetch()
1stG | RxTS | UnTS |
---|---|---|
1stG | RxTS | UnTS |
---|---|---|
Detailed changes for each release are documented in CHANGELOG.md.