Coder Social home page Coder Social logo

denis-zavgorodny / retry-my-fetch Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 319 KB

Tiny decorator for Fetch API which allows resending (retrying) your requests in case if that fails

TypeScript 30.25% JavaScript 69.75%
fetch js ts refetch retry-pattern retrying retry

retry-my-fetch's Introduction

retry-my-fetch

It's a decorator for Fetch API (fetch, isomorphic-fetch, cross-fetch, etc.) which allows resending (retrying) your requests in case if that fails.

npm package

npm i retry-my-fetch

Quick start

Simple retry

import retryMyFetch from 'retry-my-fetch';

const config = {
  maxTryCount: 5,
};
const fetchWithRetry = retryMyFetch(fetch, config);
fetchWithRetry('/').then(console.log);

Retry with timeout

import retryMyFetch from 'retry-my-fetch';

const config = {
  timeout: 3000, // it should call next retry after 3 sec
  maxTryCount: 5,
};
const fetchWithRetry = retryMyFetch(fetch, config);
fetchWithRetry('/').then(console.log);

Update JWT token and retry

import retryMyFetch from 'retry-my-fetch';

const config = {
  beforeRefetch: async (url, fetchOptions, statusCode, retryConter) => {
    // do something, i.e. refresh JWT token
    const newAccessToken = await getToken(); // some async function
    // update fetch options
    const newFetchOptions = {
      ...fetchOptions,
      headers: new Headers({
        Authorization: `Bearer ${newAccessToken}`,
      }),
    };

    // return new options in order to retry call with new options
    return newFetchOptions;
  },
  maxTryCount: 5,
};
const fetchWithRetry = retryMyFetch(fetch, config);
const options = {
  headers: new Headers({
    Authorization: `Bearer ${someOldToken}`,
  }),
};
fetchWithRetry('/', options).then(console.log);

Use AbortController

The AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired.

import retryMyFetch from 'retry-my-fetch';

const beforeRefetch = async (url, fetchOptions, statusCode, retryConter, isRejected) => {
  const token = getCurrentToken();
  if (!isRejected) {
    // refresh JWT token
    const freshAccessToken = await getToken(); // some async function
  }
  // update and return new options in order to retry call with new options
  return {
    ...fetchOptions,
    headers: new Headers({
      Authorization: `Bearer ${isRejected ? token : freshAccessToken}`,
    }),
  };
};

// prepare configuration
const config = {
  useAbortController: true,
  beforeRefetch,
  maxTryCount: 5,
};

// get decorated fetch
const fetchWithRetry = retryMyFetch(fetch, config);
const options = {
  headers: new Headers({
    Authorization: `Bearer ${someOldToken}`,
  }),
};

// do request with decorated fetch
fetchWithRetry('/', options).then(console.log);

See src/interfaces.ts for further details.

Do not retry if statuses

import retryMyFetch from 'retry-my-fetch';

const config = {
  doNotRetryIfStatuses: [400], // number[]
};
const fetchWithRetry = retryMyFetch(fetch, config);
fetchWithRetry('/').then(console.log);

retry-my-fetch's People

Contributors

denis-zavgorodny avatar stas-raranetskyi avatar

Watchers

 avatar  avatar

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.