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

0.1.13 • Public • Published

vufetch

npm version A fetch wrapper, supports timeout, interceptor and retry

installation

npm install vufetch --save  

usage

import * as vufetch from 'vufetch'

const myFetch = vufetch.create({
    // common options
    baseURL: 'https://www.example.com',
    timeout: 10000,  // default 1 min
    // interceptors
    onRequest(options) {
        return options
    },
    onRequestError(err) {
        return Promise.reject(err)
    },
    onResponse(data) {
        // handle data
        // ...
        return data
    },
    onResponseError(err) {
        return Promise.reject(err)
    },
})
myFetch.get('/users', {
    retry: true,
})
.then(console.log)
.catch(err => {
    // we use AbortController API to abort request to fit timeout, so you can use error.aborted to judge whether the request is timeout
    if (err.aborted) {
        // ...
    }
    console.log(err)
})

License

MIT

Package Sidebar

Install

npm i vufetch

Weekly Downloads

4

Version

0.1.13

License

none

Unpacked Size

16.1 kB

Total Files

7

Last publish

Collaborators

  • vinsurs