Nitrate Processed Mincemeat

    fail4ward-retry
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.3 • Public • Published

    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 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
      
    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

    Keywords

    none

    Install

    npm i fail4ward-retry

    DownloadsWeekly Downloads

    0

    Version

    0.2.3

    License

    ISC

    Unpacked Size

    11.3 kB

    Total Files

    9

    Last publish

    Collaborators

    • ardydedase