Socket
Socket
Sign inDemoInstall

redux-normalize-axios-middleware

Package Overview
Dependencies
4
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redux-normalize-axios-middleware

Normalize data using redux-axios-middleware and normalizr schema


Version published
Maintainers
1
Created

Readme

Source

Redux Normalize Axios Middleware

Normalize data using redux-axios-middleware and normalizr schema

Installation

yarn add redux-normalize-axios-middleware normalizr

Usage

Add normalizeAxiosMiddleware

import normalizeAxiosMiddleware from 'redux-normalize-axios-middleware'

const store = createStore(testReducer, applyMiddleware(
  axiosMiddleware(axiosClient),
  normalizeAxiosMiddleware
))

Add normalizr schema to your action:

import { schema } from 'normalizr'

const fetchPosts = () => ({
  type: 'FETCH_POSTS',
  payload: {
    request: {
      url: API_URL
    }
  },
  normalize: {
    schema: new schema.Entity('posts'),
  }
})

This will normalize data and include ids of items:

{
  data: {
    posts: {
      items: { 1: { id: 1, title: 'title' } },
      allIds: [1],
    }
  },
  ...remainingAxiosData
}

If your data is nested you can use path option to specify what should be normalized. The remaining part of data outside of path will be omitted. path uses lodash/get format:

import { schema } from 'normalizr'

const fetchPosts = () => ({
  type: 'FETCH_POSTS',
  payload: {
    request: {
      url: API_URL
    }
  },
  normalize: {
    schema: [new schema.Entity('posts')],
    path: 'allPosts'
  }
})

This will normalize:

{
  allPosts: [{
    id: 1,
    title: 'title'
  }],
  someOtherData: 'lorem ipsum',
}

To:

{
  posts: {
    items: { 1: { id: 1, title: 'title' } },
    allIds: [1],
  }
}

FAQs

Last updated on 15 May 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc