zap-fetch
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

Ndst

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 💛

Dependents (0)

Package Sidebar

Install

npm i zap-fetch

Weekly Downloads

0

Version

0.0.0

License

MIT

Unpacked Size

20.9 kB

Total Files

8

Last publish

Collaborators

  • malezjaa