Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

tenacious-fetch

2.1.0 • Public • Published

tenacious-fetch

package version package downloads standard-readme compliant package license make a pull request

Tiny fetch API wrapper to add support for retries with linear & exponential backoff and timeouts

Table of Contents

Install

This project uses node and npm.

$ npm install tenacious-fetch
# OR 
$ yarn add tenacious-fetch

Usage

import tenaciousFetch from 'tenacious-fetch'
 
const url = 'https://jsonplaceholder.typicode.com/posts/1'
const normalFetchConfig = {
    method: 'GET',
    headers: {
      "Content-Type": "application/json charset=UTF-8"
    },
    // Others...
}
 
 
const additionalTenaciousFetchConfig = {
    fetcher: window.fetch,  // Fetch implementation to use, default is window.fetch
    retries: 3,             // Number of retries, default is 1
    retryDelay: 1000 * 3,   // Delay in ms before retrying, default is 1000ms
    retryStatus = [],       // Status codes of response that should trigger retry e.g. [500, 404] or just "500". 
                            // defaults to empty array
    timeout = 1000 * 15,    // Timeout in ms before throwing a timeout error for the request.
                            // Defaults to no timeout (undefined).
    factor: .5              // If factor is given, exponential backoff will be performed for retries, otherwise
                            // linear backoff is used  
}
 
const config = Object.assign({}, normalFetchConfig, additionalTenaciousFetchConfig)
 
tenaciousFetch(url, config).then(console.log).catch(console.error)

Contribute

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am 'Add some feature'
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT

install

npm i tenacious-fetch

Downloadsweekly downloads

176

version

2.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability