npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dyna-fetch

v3.1.4

Published

Fetch method with retry, cancel, abort, timeout features using Axios.

Downloads

4

Readme

About

dyna-fetch wrapps the axios and adds some sugar on it like:

  • pre-flight to bypass CORS
  • timeout
  • cancel
  • retry
  • abort

Written in Typescript, it is universal and runs everywhere browser/nodeJs/.

Usage

import {dynaFetch} from 'dyna-fetch';

const myRequest = dynaFetch({
    url: 'https://example.com/api?client=3002',
    requestConfig: {        // (optional)
        data: {},           // (optional) Request body
    },
    preFlight: true,        // (optional) try to bypass CORS
    timeout: 20000,         // (optional) wait for max 20 seconds
    retryMaxTimes: 3,       // (optional) retry max 3 times
    retryTimeout: 1000,     // (optional) wait for 1sec for each retry
    retryRandomFactor: 1,   // (optional) timeout factor
    onRetry: () => console.log('retrying...'), // (optional) 
});

myRequest
    .then((response: AxiosResponse) => {
        // ...
    })
    .catch((error: IError | AxiosError) => {
        // ...
    });

// later you can abort it, rejects the promise
myRequest.abort();

// later you can cancel it, rejects the promise
myRequest.cancel('Fetch is canceled');

Arguments

interface IDynaFetchConfig {
  url: string;
  requestConfig?: AxiosRequestConfig;         // help: https://github.com/axios/axios#axios-api
  preFlight?: boolean;                        // default: false, skip CORS with pre-flight OPTIONS request (the server should support this)
  retry?: (error: AxiosError) => boolean;     // default: () => true; Validate the error. Return true to retry or false to return the error.
  cancelOnRetry?: boolean;                    // default: false; if true, in case of retry timeout the current request will be XHR canceled.
  retryMaxTimes?: number;                     // default: 0
  retryTimeout?: number;                      // default: 0, in ms
  retryRandomFactor?: number;                 // default is 1, finalTimeout = retryTimeout * random(0, timeoutRandomFactor)
  onRetry?: () => void;                       // callback called on each retry
}

dynaFetch API

The dynaFetch() returns a promise object together with a small API object.

TS the type of th result is IDynaFetchHandler.

{
  abort: () => void;
  cancel: (message?: string) => void;
}

Abort

During the execution of the request, you can abort the request.

Note: The request is not a cancelled! The abort just swallows the response and rejects the promise.

Cancel

Cancel the XHR request.

Cancel rejects the promise.

Errors

In case of a rejected request, if the error has to do with features of the dyna-fetch, the IError will be returned.

Otherwise, the axios's error will be returned.

IError

interface IError {
  code?: number | string; // code
  section?: string;       // section of the application (dynaFetch in this case)
  message?: string;       // a meaningful dev/debug message
  error?: any;            // nested error
}

error.code 5017

In case of timeout.

error.code 5019

In case of abort() call.

Change log

v1.0.0

v2.0.0

  • Working with axios
  • Pre-flight requests with CORS
  • Retry with random factor

v3.0.0

  • New prop retry?: (error: AxiosError) => boolean;. Validate the error to retry or not.
  • The returned error is not an IError that wraps the AxiosError but the AxiosError directly. Still the IError returned on 5017 & 5019.

v3.1.0

  • Cancel request on demand
  • Cancel request on retry

v3.1.2

  • Cancel request on retry only then the cancelOnRetry: true config prop