1.1.5 • Published 6 years ago

redux-normalize-axios-middleware v1.1.5

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

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],
  }
}
1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago