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

v4.0.2

Published

A middleware for redux that help to fetch data from rest API

Downloads

71

Readme

redux-fetch-middleware

Join the chat at https://gitter.im/zslucky/redux-fetch-middleware Build Status Known Vulnerabilities

A middleware for redux that help to fetch data from rest API and simplify the request flow. Many times we only need to do some simple request, but we need to track the request status, This middleware will automaticly dispatch 3 status.

Changes plesae refer to CHANGELOG.md

In V4 version, we removed isomorphic-fetch dependency, as this is only a polyfill, you can add it everywhere by yourself in your own project if you need it.

Installation

npm i redux-fetch-middleware --save

Or use yarn:

yarn add redux-fetch-middleware

Usage

import restMiddlewareCreator from 'redux-fetch-middleware';
import { applyMiddleware } from 'redux';

const globalRestOptions = {
    suffix: ['REQUEST', 'SUCCESS', 'FAILURE'],
    // if `debug` is true, then in reducer `action.meta.$requestOptions`
    debug: true,
    // Set global value by `responseType`. Available values: json, text, formData, blob, arrayBuffer (fetch methods). Default: json
    responseType: 'text',
    // Example config
    fetchOptions: {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    }
};

const restMiddleware = restMiddlewareCreator(globalRestOptions);

const middleware = [restMiddleware];
applyMiddleware(...middleware);

//...

Configuration

Fetch options and browser support please refer to whatwg-fetch

For global settings

Every options have default value, please kindly reduce configuration. :shipit:

{
    // Suffix will auto append to every action type, then we can dispatch
    // different situation.
    // @Fisrt parameter - 'Request' means when request start.
    // @Second parameter - 'SUCCESS' means when we get response successfully.
    // @Third parameter - 'FAILURE' means when something error.
    // name can be defined by self.
    // Default value is bellow.
    suffix: ['REQUEST', 'SUCCESS', 'FAILURE'],

    // The global fetch settings for our middleware
    fetchOptions: {

        // For detail please relay to whatwg-fetch
        // This is the default header settings.
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    },

    /* TBD ...
     *
     * Here is globle config for each action.
     * Other amazing functions
     *
     */ TBD ...
}

Action usage

{
    // Type name
    type: YOUR_ACTION_TYPE_NAME,
    // Your props transition to reducers
    meta: {
        a: 10,
        b: 20
    },
    // @Param: $payload is the detail ajax request description
    $payload: {
        // Request url
        url: '/api/somewhere',
        // React on response event. If this function return === false, then to SUCCESS reducer data = null
        onResponse: (response, meta, type) {
            if (response.status != 200) {
                return false;
            }
        }
        // React pre fetch edit options. Include merged all options. Return the last options.
        preFetchOptions: (options) {
            // For example - remove Content-Type, in order to browser auto detect and auto write Content-Type value (Required to send file).
            delete options.headers['Content-Type'];
            return options;
        }
        // Method type to parse response. Available values: json, text, formData, blob, arrayBuffer (fetch methods). Default: json
        responseType: 'text',
        // The specific options for current request.
        options: {

            // Same as whatwg-fetch

        }

        /* TBD ...
         *
         * Here is config for single action.
         * Other amazing functions
         *
         */ TBD ...
        }
}

Reducer usage

function yourReducer(state = initialState, action) {
    switch (action.type) {
        case `${YOUR_ACTION_TYPE_NAME}_REQUEST`:
            // Do something when request start ...
            // @response meta is action.meta
            // @response $uid is action.meta.$uid
            // @response $requestOptions is action.meta.$requestOptions (if in config set `debug` is true)

        case `${YOUR_ACTION_TYPE_NAME}_SUCCESS`:
            // Do something ...
            // @response data is action.data
            // @response meta is action.meta
            // @response $uid is action.meta.$uid
            // @response $response is action.meta.$response
            // @response $requestOptions is action.meta.$requestOptions (if in config set `debug` is true)

        case `${YOUR_ACTION_TYPE_NAME}_FAILURE`:
            // Do something other ...
            // @response data is action.err
            // @response meta is action.meta
            // @response $uid is action.meta.$uid
            // @response $response is action.meta.$response
            // @response $requestOptions is action.meta.$requestOptions (if in config set `debug` is true)

        default:
            return state;
    }
}

Migrate from v3 to v4

  1. Remove isomophic-fetch dependency.

Migrate from v2.* to v.3

  1. Replace $props to meta in your action and reducers .
  2. Replace $uid to meta.$uid in your reducers.
  3. Profit!

TO DO List

  1. [ ] Add custom Exception config and response status trace.
  2. [ ] Improve unit test.