zap-fetch
v0.0.0
Published
Fetch wrapper made for simplicity
Downloads
2
Readme
Ndst
Install
# npm
npm install -D zap-fetch
# yarn
yarn add -D zap-fetch
# pnpm
pnpm install -D zap-fetch
# bun
bun install -D zap-fetch
Simple usage
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')
Body
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',
})
Sending data
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,
},
})
Errors
You can catch the error manually.
import {$zap} from 'zap-fetch'
const ctx = await $zap.get('/todos/1').catch((error) => {
console.error(error)
})
Hooks/Interceptors
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 💛