0.1.16 • Published 4 years ago

redux-middleware-fetch v0.1.16

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

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

  • typesArray: Array of dispatch action, support every types (eg. String, Symbol), action will be called on REQUEST_SUCCESS, (REQUEST_FAILED), (REQUEST_SENT).
  • entrypointString: API resource url, fetch will add host declared in global API_HOST constant automatically.
  • authBoolean: If true, JWT Token will add to fetch request in headers Authorization value. Token should be storage in localStorage.accessToken.
  • jsonBoolean: If true, body will be JSON stringified text. (Don't use json, urlEncoded and formData at same time)
  • formDataBoolean: If true, body will append to FormData by assigned keys. (Don't use json, urlEncoded and formData at same time)
  • urlEncodedBoolean: If true, body will be x-www-form-urlencoded and stringify by querystring module. (Don't use json, urlEncoded and formData at same time)
  • bodyObject: Key-value paired post data.
  • methodString: HTTP Method, default: GET.
  • onSuccessFunction: Success callback function, you can do something without store dispatch (eg. dispatch redirect react-router-redux action).
  • onFailedFunction: Failed callback function.
  • headersObject: 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
0.1.16

4 years ago

0.1.15

5 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

8 years ago