🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

redux-api-middleware-native

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-api-middleware-native

Api middleware for React Native.

latest
Source
npmnpm
Version
2.0.10
Version published
Maintainers
1
Created
Source

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
}

Keywords

React

FAQs

Package last updated on 11 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts