Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flexible-redux-api-middleware

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flexible-redux-api-middleware

A redux middleware for api response, error handling.

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Redux Api Middleware

A redux api middleware for handling api call and response.

Installation

yarn add redux-api-middleware

Setup

import { applyMiddleware, createStore } from 'redux';

// api middleware with default api client
import { apiMiddleware } from 'redux-api-middleware'
const store = createStore(
  reducer,
  applyMiddleware(apiMiddleware),
);

Or you can create an api middleware with a custom api client

import { applyMiddleware, createStore } from 'redux';

// api middleware with a custom api client
import { apiMiddlewareFactory } from 'redux-api-middleware'

const api = (options) => {
  return fetch(options)
}

const store = createStore(
  reducer,
  applyMiddleware(apiMiddlewareFactory(api)),
);

Usage

Import
import { asyncActionType } from 'redux-api-middleware';
Constant
// create an async action type
const FETCH_USER_PROFILE = asyncActionType('FETCH_USER_PROFILE');
Action
const fetchUserProfile = (id) => ({
  type: 'API',
  payload: {
    url: 'https://example.com/user/',
    method: 'GET',
    data: {
      id,
    },
    next: FETCH_USER_PROFILE,
  },
});
Reducer
const initState = {
  error: null,
  pending: false,
};

const user = (state = initState, action) => {
  switch (action.type) {
    case FETCH_USER_PROFILE.PENDING: {
      return {
        pending: true,
      };
    }
    case FETCH_USER_PROFILE.SUCCESS: {
      return {
        ...action.payload,
        pending: false,
      };
    }
    case FETCH_USER_PROFILE.ERROR: {
      return {
        error: action.payload,
        pending: false,
      };
    }
    default:
      return state;
  }
};

Keywords

FAQs

Package last updated on 19 Nov 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc