Skip to content

briojs/zap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

⚡ Zap

This is a modified version of ofetch. Mostly added method for every method, and context is returned instead of response itself.

npm version npm downloads bundle size license

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 💛

About

⚡Fetch wrapper made for simplicity

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

 
 
 

Contributors