Giter Club home page Giter Club logo

tenacious-fetch's Introduction

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
    onRetry: ({retriesLeft, retryDelay, response}) => console.log(retriesLeft, retryDelay, response),
    retryStatus = [],           // Status codes of response that should trigger retry e.g. [500, 404] or just "500". 
                                // defaults to empty array
    retryOnFatalError = true    // If there a fatal request (no response status), we can choose
                                // to retry or not
    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

tenacious-fetch's People

Contributors

ccope avatar dependabot[bot] avatar imgbotapp avatar lotarwalace avatar pedreviljoen avatar renovate-bot avatar snyk-bot avatar tiaanduplessis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tenacious-fetch's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

how to get response in "onRetry" call?

Hi @tiaanduplessis,
Now I have this line in my API call for log how many times an API called:

onRetry: ({retriesLeft, retryDelay}) => console.warn(`get users: ${retriesLeft} ${retryDelay}`)

It's work well but is it possible to get response object on each onRetry call?
For example I want to now which status code return and troubleshoot my API and find why onRetry called 5 times?

Is it possible now?

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

run a function on each retry

@tiaanduplessis is it possible to run a function on each retry?
For example I want to print a simple log about number of retry like:

console.log(`${numberOfRetry} unsuccessful API call... (retry soon - please wait)`);
1 unsuccessful API call... (retry soon - please wait)
2 unsuccessful API call... (retry soon - please wait)
3 unsuccessful API call... (retry soon - please wait)
4 unsuccessful API call... (retry soon - please wait)
5 unsuccessful API call... (retry soon - please wait)
...

For example we can provide this as some function like:

onError(errorCode) -> for call when we has error (and access response error code - 404, 500, ...)
onTimeout -> for call when fetch request is timeout

Modify headers on each retry

Is there any way to modify the headers before each retry?

I have an auth token which expires and must be refreshed, but having tenacioius-fetch retry on 401 seems to be sending the same old token. It would be super helpful if we can replace headers on each retry.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.