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

redux-xhr-middleware

v0.7.3

Published

Async XHR middleware for Redux

Downloads

7

Readme

redux-xhr-middleware Build Status

Async XHR middleware for Redux

A light-weight middleware for Redux to do promise-based XHR requests. XHR headers and API gateway uri are configurable.

Installation

yarn add redux-xhr-middleware

1. Plug into your Redux store

import {createStore, applyMiddleware} from 'redux';
import xhrMiddlewareCreator from 'redux-xhr-middleware';
import rootReducer from '../myRouteReducer';

const createReduxStore = initialState => {
  const xhrMiddleware = xhrMiddlewareCreator();
  let enhancer = applyMiddleware(xhrMiddleware);
  return createStore(
    rootReducer,
    initialState,
    enhancer
  );
};

const store = createReduxStore();

2. Use in your action creators to initiate XHR

Your action creator needs to return an object with types and xhr properties, both must be provided.

const GET_AUTHORS_REQUEST = 'GET_AUTHORS_REQUEST';
const GET_AUTHORS_SUCCESS = 'GET_AUTHORS_SUCCESS';
const GET_AUTHORS_FAIL = 'GET_AUTHORS_FAIL';

const getAuthors = (authorId = '') => {
  const url = siteId ? `/api/authors/${authorId}` : '/api/authors';
  return {
    types: [GET_AUTHORS_REQUEST, GET_AUTHORS_SUCCESS, GET_AUTHORS_FAIL],
    xhr: {
      url,
      method: 'GET'
    }
  };
};

3. Finally, handle your actions in reducer:

import merge from 'lodash/merge';

  // ...
  [GET_AUTHORS_REQUEST]: state => Object.assign({}, state, {
    fetchError: null,
    authors: [], // * see note below
    isGetting: true
  }),
  [GET_AUTHORS_SUCCESS]: (state, {payload}) => merge({}, state, {
    isGetting: false,
    authors: payload  // * see note below
  }),
  [GET_AUTHORS_FAIL]: (state, {payload}) => Object.assign({}, initialState, {
    getError: payload,
    isGetting: false
  }),
  // ...
  // * note: If you use normalizr, you will not need to store entity data in these reducers,
  // your dedicated 'entities' normalising reducer will keep that data.

Configuring XHR middleware

You can specify an options object to the XHR middleware creator:

const options = {
  apiHeaders: {},
  apiGateway: ''
};
const xhrMiddleware = xhrMiddlewareCreator(options);

apiHeaders can add/override any XHR header other than Accept and Content-Type headers, which are set as application/json by default.

apiGateway is a string as prefix to the paths, in case the API gateway is different to the application server, e.g. apiGateway could be https://myGateway.com:8081.

Example:

const options = {
  apiHeaders: {
    Authorization:  'my token'
  },
  apiGateway: '/api'
};

Customising header on a single XHR request

In your action creator, you can customise the HTTP header for your request.

const CREATE_AUTHORS_REQUEST = 'CREATE_AUTHORS_REQUEST';
const CREATE_AUTHORS_SUCCESS = 'CREATE_AUTHORS_SUCCESS';
const CREATE_AUTHORS_FAIL = 'CREATE_AUTHORS_FAIL';

const createAuthor = (payload) => {
  const url = '/api/authors';
  return {
    types: [CREATE_AUTHORS_REQUEST, CREATE_AUTHORS_SUCCESS, CREATE_AUTHORS_FAIL],
    xhr: {
      url,
      method: 'POST',
      headers: {
        'Accept': 'text/plain'
      },
      data: JSON.stringify(payload)
    }
  };
};