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

v0.1.16

Published

Redux Whatwg Fetch Middleware

Downloads

18

Readme

redux-middleware-fetch

Redux Whatwg Fetch Middleware

Usage

Bind Middleware

// ...
import thunk from 'redux-thunk';
import {
  createStore,
  applyMiddleware,
} from 'redux';
import fetchMiddleware from 'redux-middleware-fetch';

const store = createStore(reducer, defaultState, applyMiddleware(
  thunk,
  fetchMiddleware,
));

// ...

Call on action

import { API_REQUEST } from 'redux-middleware-fetch';
import { replace } from 'react-router-redux';

export const PRODUCT_FETCHED = Symbol('PRODUCT_FETCHED');
export const PRODUCT_CREATED = Symbol('PRODUCT_CREATED');
export const PRODUCT_CREATE_FAILED = Symbol('PRODUCT_CREATE_FAILED');
export const PRODUCT_CREATING = Symbol('PRODUCT_CREATING');

export const fetchProducts = () => ({
  [API_REQUEST]: {
    types: [
      PRODUCT_FETCHED,
    ],
    entrypoint: '/products',
    auth: true,
    method: 'GET',
  },
});

// Thunkify Action
export const createProducts = (product) => ({
  dispatch => dispatch({
    [API_REQUEST]: {
      types: [
        PRODUCT_CREATED,
        PRODUCT_CREATE_FAILED,
        PRODUCT_CREATING,
      ],
      entrypoint: '/products',
      auth: true,
      json: true,
      body: product
      method: 'POST',
      onSuccess: () => dispatch(replace('/products')),
    },
  });
});

Customize Storage

In default, redux-middleware-fetch use localStorage for storage accessToken (in key accessToken). You can change any other storage system implemented getItem and setItem functions for it.

import {
  SimpleStorage,
  setStorage,
} from 'redux-middleware-fetch';

const storage = new SimpleStorage();

setStorage(storage);

Options

  • types[Array]: Array of dispatch action, support every types (eg. String, Symbol), action will be called on [REQUEST_SUCCESS, (REQUEST_FAILED), (REQUEST_SENT)].
  • entrypoint[String]: API resource url, fetch will add host declared in global API_HOST constant automatically.
  • auth[Boolean]: If true, JWT Token will add to fetch request in headers Authorization value. Token should be storage in localStorage.accessToken.
  • json[Boolean]: If true, body will be JSON stringified text. (Don't use json, urlEncoded and formData at same time)
  • formData[Boolean]: If true, body will append to FormData by assigned keys. (Don't use json, urlEncoded and formData at same time)
  • urlEncoded[Boolean]: If true, body will be x-www-form-urlencoded and stringify by querystring module. (Don't use json, urlEncoded and formData at same time)
  • body[Object]: Key-value paired post data.
  • method[String]: HTTP Method, default: GET.
  • onSuccess[Function]: Success callback function, you can do something without store dispatch (eg. dispatch redirect react-router-redux action).
  • onFailed[Function]: Failed callback function.
  • headers[Object]: Custom headers in object.

Event Listener

import {
  requestListener,
  EVENT_REQUESTED,
  EVENT_REQUEST_SUCCESSED,
  EVENT_REQUEST_FAILED,
} from 'redux-middleware-fetch';

requestListener.on(EVENT_REQUESTED, (reqOptions) => {
  // do something on request sent
});

requestListener.on(EVENT_REQUEST_SUCCESSED, (response) => {
  // do something on request successed
});

requestListener.on(EVENT_REQUEST_FAILED, (response) => {
  // do something on request failed
});

To-Do

  • [ ] Mocks Service / API Simulator