# npm
npm install -D zap-fetch
# yarn
yarn add -D zap-fetch
# pnpm
pnpm install -D zap-fetch
# bun
bun install -D zap-fetch
import {$zap, createZapFetch} from 'zap-fetch'
// Use default fetch
const {body, response} = await $zap.get('https://jsonplaceholder.typicode.com/todos/1')
// Or create your own instance
const zap = createZapFetch({
baseUrl: 'https://jsonplaceholder.typicode.com',
})
const {body, response} = await zap.get('/todos/1')
import {$zap} from 'zap-fetch'
const {body} = await $zap.get('/todos/1')
Zap will automatically parse the response body based on the Content-Type
header.
You can also provide which method to use for parsing the body.
import {$zap} from 'zap-fetch'
const {body} = await $zap.get('/todos/1', {
responseType: 'arrayBuffer',
})
Body will be automatically stringified if it's possible. ReadableStream
, Stream
and Buffer
are also supported.
import {$zap} from 'zap-fetch'
const {body} = await $zap.post('/todos', {
body: {
title: 'foo',
completed: false,
userId: 1,
},
})
You can catch the error manually.
import {$zap} from 'zap-fetch'
const ctx = await $zap.get('/todos/1').catch((error) => {
console.error(error)
})
import {$zap} from 'zap-fetch'
const {body} = await $zap.get('/todos/1', {
onRequest: ({request}) => {
console.log(request)
},
onResponse: ({response}) => {
console.log(response)
},
})
Published under the MIT license.
Made by @malezjaa
and community 💛