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

redux-call-api

v0.0.1

Published

Nice way to perform async api calls in Redux app

Readme

redux-call-api

Make easy async api calls with redux.

Install it

npm i -S redux-call-api

Test it

git clone https://github.com/saintcrawler/redux-call-api.git && cd redux-call-api

npm i && npm test

Use it

First, you need to write a config object.

const config = {
  provider: { 
    // `provider` is a required object. It must contain at least `request` function    
    request: (reqObj) => {
      // `reqObj` is created by you (see below)
      // you may do whatever you want here, but must return a promise object
    }
  },
  actions: {
    // `actions` is a required object. It must contain keys, matching `type` prop of redux actions.
    'FOO': {
      makeApiObj: (action) => {
        // again, do whatever you want, but return an object with at least `request` prop.
        return {
          request: {
            // required. This is the `reqObj` which will be passed into `provider.request` function (see above)
            // you may construct it as you want
            url: `/foo/${action.payload.id}/`,
            method: 'post',
            header: ['baz', 'bar']
          }
        }
      }
    }
  },
  // here go optional hook functions
  
  beforeRequest: (apiObj, dispatch, getState) => {
    // `apiObj` is the object returned by `makeApiObj`, pre-process it as you want
    // do not return anything, just modify `apiObj`'s keys
  },
  onSuccess: (response, apiObj, dispatch, getState) => {
    // `response` is whatever `provider.request` returns when resolves
  },
  onFailure: (response, apiObj, dispatch, getState) => {
    // `response` is whatever `provider.request` returns when rejects
  }
};

Then, create a CallApi object with config and just call request function.

import CallApi from 'redux-call-api'

const api = new CallApi(config);

const action = {
  type: 'FOO',
  payload: {id: 11}
};

api.request(action, dispatch, getState); // you can obtain `dispatch` and `getState` from redux store

Making CallApi more useful

With the config object like above, request just calls makeApiObj function, gets request object and calls config.provider.request function with it. Not very useful for our redux app. That's why you probably want to use BasicConfig constructor to create your config object. But it's not mandatory, it's just config with convenient hooks already being set up.

import CallApi, {BasicConfig} from 'redux-call-api'

const actions = {
  'FOO': {    
    makeApiObj: function() {
      return {
        // required
        request: {},
        // now, this is required, too
        actionTypes: {
          request: 'FOO_REQUEST',
          success: 'FOO_SUCCESS',
          failure: 'FOO_FAILURE',
        },

        // optional per-action hooks, will be called only for that action
        beforeRequest: (apiObj, dispatch, getState) => {},
        onSuccess: (response, apiObj, dispatch, getState) => {},
        onFailure: (response, apiObj, dispatch, getState) => {}
      }
    }
  }
};

const config = new BasicConfig(actions, provider); // see above for details about `provider`

const api = new CallApi(config);

Now, when you call api.request({type: 'FOO'}, dispatch, getState), the following will happen:

  1. 'FOO_REQUEST' action will be dispatched
  2. 'FOO_SUCCESS' action will be dispatched if config.provider.request resolves. It will be equal:
{
  type: 'FOO_SUCCESS',
  payload: response.data,
  meta: metaInfo // response object without `data` key
}
  1. 'FOO_FAILURE' action will be dispatched if config.provider.request rejects. It will be equal:
{
  type: 'FOO_FAILURE',
  payload: response.data || response,
  error: true,
  meta: metaInfo // response object without `data` key
}

What about provider?

Personally, I use axios as my provider, but you can use any other promise-based solution.

import axios from 'axios'

const provider = {
  request: (reqObj) => {
    return axios(reqObj); // that's easy
  }
}

How do I call api.request function automatically?

You can use redux-trigger-middleware. Make a trigger config:

const triggerConfig = {
  actions: {
    // trigger: 'LOAD_FOO',
    // or you can write a predicate, which will trigger on any action type that begins with 'LOAD'
    trigger: (action) => /^LOAD/.test(action.type),
    handler: (action, dispatch, getState) => api.request(action, dispatch, getState)
  }
};

See also

Some use cases

License

ISC