GithubHelp home page GithubHelp logo

ardydedase / fail4ward-retry Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 138 KB

Implement Retry Pattern in your NodeJS application by using this NPM package. Implementation inspired by resilience4j.

License: MIT License

TypeScript 100.00%
typescript node typescript-library resilience javascript

fail4ward-retry's Introduction

fail4ward-retry

NPM package to implement Retry Pattern in your nodejs applications. Created from this Cookiecutter Template.

CI npm-publish npm-install-and-use-package

NPM

Features

Specs

Tested on:

  • npm 6.13.4
  • node v10.19.0

Installation

npm install --save fail4ward-retry

Usage

Import what we need.

import { RetryConfigBuilder, RetryConfig, Retry, UntilLimit } from 'fail4ward-retry';

Set the configuration using the RetryConfigBuilder().

const maxAttempts = 5;
const waitDuration = 1000;

const retryConfig: RetryConfig = new RetryConfigBuilder()
        .withMaxAttempts(maxAttempts)
        .withWaitDuration(waitDuration)
        .withStrategy(UntilLimit)
        .build();

Builder properties we are setting

  • withMaxAttempts() number of attempts to retry.
  • withWaitDuration() backoff time in milliseconds.
  • withStrategy() retry strategy to use. This package currently supports UntilLimit.

Decorate the function that calls your service using the retryConfig instantiated with Retry.decoratePromise().

const retry = Retry.With(retryConfig);
const fn = retry.decoratePromise(failingFn);

Below is an example of the failingFn calls an API. Similar functions can be found in the /example and /__tests__ folders.

Click to see failingFn()
async function failingFn() {
  const url = 'http://localhost:8000/error';
  try {
    const res = await fetch(url);
    const {status} = res;
    if (status === 500) {
      throw new Error('server error');
    }
    return res;
  } catch(e) {
    throw new Error(e);
  }
}

Call the function that fetches the response from your API and retrieve the response.

try {
        const res = await fn();
        const retryResponse = await res.json();
        console.log('retryResponse: ', retryResponse);
} catch(e) {
        console.log(e);
}

Run the example

  1. Checkout the repo

     git clone [email protected]:ardydedase/fail4ward-retry.git
    
  2. Change directory to the example folder

     cd example/
    
  3. Install dependencies

     npm install
    
  4. Run a test service

     docker run --name fail-svc -p 8000:8000 -it ardydedase/fail4ward:latest
    
  5. Run the example file

     npm run dev
    

Development

  1. Checkout the repo

     git clone [email protected]:ardydedase/fail4ward-retry.git
    
  2. Install dependencies

     npm install
    
  3. Run build. This will generate the compiled code with type definitions in the dist folder.

     npm run build
    
  4. Formatting and linting.

     npm run lint
     npm run format
    
  5. Run tests

     npm test
    

Credits

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.