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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@travi/redux-fetch-middleware

v4.0.0

Published

redux middleware to enable async data fetching as the result of a dispatched action

Readme

redux-fetch-middleware

middleware to enable async data fetching as the result of a dispatched action

Node CI Workflow Status

Usage

npm license node

Installation

$ npm install @travi/redux-fetch-middleware --save

Include as a middleware for redux

As a single middleware

import {createStore, applyMiddleware} from 'redux';
import fetchMiddlewareFactory from '@travi/redux-fetch-middleware';
import reducer from './reducer';

export default function ({session}) {
  return createStore(reducer, applyMiddleware(fetchMiddlewareFactory(session)));
}

With additional middlewares

import {createStore, applyMiddleware, compose} from 'redux';
import fetchMiddlewareFactory from '@travi/redux-fetch-middleware';
import reducer from './reducer';
import DevTools from './DevTools'

export default function ({session}) {
  return createStore(reducer, compose(applyMiddleware(fetchMiddlewareFactory(session)), DevTools.instrument()));
}

Make the session data available to your fetch methods

Be sure to export a createFetcher named function that takes a session object

export function createFetcher(session) {
  const authToken = session.auth.token;

  return {
    async fetchFoo() {
      return await getFoo(authToken);
    },
    async fetchBar() {
      return await getBar(authToken);
    }
  };
}

Triggering by dispatching an action

Dispatch an action that does not define type, but instead contains:

  • fetch: a function that takes a reference to the client module to give access to the methods that do the actual data fetching
  • initial: the action type that should be dispatched when the data fetch is initiated
  • success: the action type that should be dispatched when the data has been successfully received. the received resource will be passed in the resource attribute of this action.
  • failure: the action type that should be dispatched when the fetch result in an error. the resulting error will be passed as the error attribute of this action.
  • data: the data that you would like access to in your dispatched methods. the resulting data will be passed as base level attributes to the resource.
  • retry (optional): a predicate function that enables instructing the middleware to retry (or poll) the fetch under certain conditions.
    • The predicate function is expected to be implemented in error-first style, meaning that the error will be provided as the first argument in the failure scenario, and the response will be provided as the second argument in the success scenario
    • The predicate function should return a boolean to instruct the middleware whether to repeat the call again or not
    • When the function is not provided, the default will be the same as if the predicate returned false, so the fetch will not be repeated by default
    • When a retry is requested, the repeated request will be delayed by three seconds

As an action creator

export function loadFoo(id) {
    return {
        fetch: (client) => client.getFoo(id),
        retry: (err, response) => {
          if (err) return true;
          return (response && 'in-progress' === response['completion-status']);
        },
        initiate: LOAD_FOO,
        success: FOO_LOADED,
        failure: FOO_LOAD_FAILED,
        data: {id, foo:'bar'}
    };
}

A corresponding reducer

export default function reducer(state, action) {
    switch (action.type) {
    case LOAD_FOO:
        return state.merge({loading: true, loaded: false, foo: {}});
    case FOO_LOADED:
        return state.merge({loading: false, loaded: true, foo: action.resource});
    case FOO_LOAD_FAILED:
        return state.merge({loading: false, error: action.error});
    default:
        return state;
    }
}

Register your data-fetcher factory with @travi/ioc

This enables you to register different data-fetchers in different contexts, server vs browser

register('fetcher-factory', fetcherFactory);

Contributing

Commitizen friendly semantic-release Renovate

Dependencies

$ nvm install
$ npm install

Verification

$ npm test