All Projects → zslucky → redux-fetch-middleware

zslucky / redux-fetch-middleware

Licence: MIT license
The simplest middleware using fetch api for redux to send request

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-fetch-middleware

react-express-mongodb
基于react全家桶+antd design+webpack2+node+express+mongodb开发的前后台博客系统
Stars: ✭ 26 (-27.78%)
Mutual labels:  fetch
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-58.33%)
Mutual labels:  fetch
git-cheatsheet
One stop guide to help solve all your doubts related to Git & GitHub.
Stars: ✭ 31 (-13.89%)
Mutual labels:  fetch
xhttp
Tiny shortcuts for using the native fetch API. Provides a fluent builder-style API for request building and response reading.
Stars: ✭ 31 (-13.89%)
Mutual labels:  fetch
json
Converts camelCase JavaScript objects to JSON snake_case and vise versa.
Stars: ✭ 16 (-55.56%)
Mutual labels:  fetch
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (+22.22%)
Mutual labels:  fetch
gotify-push
Chrome Extension for Send Push Notification 🔔 to gotify/server ☁
Stars: ✭ 32 (-11.11%)
Mutual labels:  fetch
cetch
c sysfetch
Stars: ✭ 23 (-36.11%)
Mutual labels:  fetch
frontend-tutorial
🎨 一个后端程序员的前端技术总结
Stars: ✭ 122 (+238.89%)
Mutual labels:  fetch
sapper-httpclient
An isomorphic http client for Sapper
Stars: ✭ 48 (+33.33%)
Mutual labels:  fetch
parcel-plugin-goodie-bag
provides the Promise and fetch goodies needed for IE(11) support w/ parcel bundle loading
Stars: ✭ 15 (-58.33%)
Mutual labels:  fetch
fetch
A fetch API polyfill for React Native with text streaming support.
Stars: ✭ 27 (-25%)
Mutual labels:  fetch
airbud
Retrieving stuff from the web is unreliable. Airbud adds retries for production, and fixture support for test.
Stars: ✭ 15 (-58.33%)
Mutual labels:  fetch
beccaccino
Beccaccino is an easy, sexy, reliable, framework agnostic http client for redux that is ⚡️beccaccino fast!
Stars: ✭ 13 (-63.89%)
Mutual labels:  fetch
gtni
Install your all npm dependencies recursively with gtni while you are doing git clone, fetch or pull
Stars: ✭ 17 (-52.78%)
Mutual labels:  fetch
sysfex
Another system information fetching tool written in C++
Stars: ✭ 107 (+197.22%)
Mutual labels:  fetch
legible
the cleanest way to make http requests in js / node
Stars: ✭ 49 (+36.11%)
Mutual labels:  fetch
disfetch
Yet another *nix distro fetching program, but less complex.
Stars: ✭ 45 (+25%)
Mutual labels:  fetch
mey
A react package that exports hooks for handling the request lifecycle.
Stars: ✭ 18 (-50%)
Mutual labels:  fetch
fitch.js
A lightweight Promise based HTTP client, using Fetch API.
Stars: ✭ 35 (-2.78%)
Mutual labels:  fetch

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].