JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 39
  • Score
    100M100P100Q44500F
  • License MIT

Api middleware for React Native.

Package Exports

  • redux-api-middleware-native

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redux-api-middleware-native) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Redux api middleware native

Build Status npm version npm

Api middleware for redux compatible with native and web apps.

Install

npm install --save redux-api-middleware-native

Adding the middleware to redux store

import { createStore, applyMiddleware, combineReducers } from 'redux';
import apiMiddleware from 'redux-api-middleware-native';
import reducers from './reducers';

const reducer = combineReducers(reducers);
const initialState = {};

const store = createStore(reducer, initialState, applyMiddleware(
    apiMiddleware,
));

Usage

import { CALL_API } from 'redux-api-middleware-native';

function action() {
    return {
        [CALL_API]: {
            endpoint: 'http://www.example.com/resource',
            method: "POST",
            headers: {
                'Content-Type': 'application/json'
            },
            body: {
                'username' : 'npm-user',
                'password' : 'test'
            },
            types: ['SUCCESS', 'FAILURE', 'ERROR'],
            meta: {
                id: 'Data to reducer'
            }
        }
    }
}

Custom payload / meta handler

import { CALL_API } from 'redux-api-middleware-native';

function action() {
    return {
        [CALL_API]: {
            endpoint: 'http://www.example.com/resource',
            method: "POST",
            headers: {
                'Content-Type': 'application/json'
            },
            body: {
                'username' : 'npm-user',
                'password' : 'test'
            },
            types: [{
              type: 'SUCCESS',
              payload: (action, state, res) => {
                  return res.json().then((json) => {
                      // Inserting a header in response
                      json.token = res.headers.get('Authorization');
                      return json;
                  });
              },
              meta: (action, state) => {
                  return action.meta;
              }
            }, 'FAILURE', 'ERROR'],
            meta: {
                id: 'Data to reducer'
            }
        }
    }
}

Responses

SUCCESS

Action {
    type = types[0]
    payload = JSON parsed response
    error = false
    meta = Any data that you sent
}

FAILURE

Type failure means your request not get HTTP status code 200 without any other errors.

Action {
    type = types[1]
    payload = JSON parsed response
    error = true
    meta = Any data that you sent
}

ERROR

Type error means we got exception on some point of code (ex. response parsing).

Action {
    type = types[2]
    payload = ERROR object
    error = true
    meta = Any data that you sent
}