0.2.3 • Published 4 years ago

fail4ward-retry v0.2.3

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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.

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 git@github.com: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 git@github.com:ardydedase/fail4ward-retry.git
  1. Install dependencies

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

    npm run build
  3. Formatting and linting.

    npm run lint
    npm run format
  4. Run tests

    npm test

Credits

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago