Suitable tools for transform json api response
npm install json-api-transform --save
In reducer.js
import { insert, clear, slice } from 'json-api-transform'
const reducer = (state = {}, { type, entityTypes, payload }) => {
switch (type) {
case 'FETCH_ANY_ENTITY_SUCCESS':
case 'FETCH_ANY_ENTITIES_SUCCESS':
case 'UPDATE_ANY_ENTITY_SUCCESS':
case 'CREATE_ANY_ENTITY_SUCCESS': {
return insert(state, payload.data)
}
case 'DESTROY_ANY_ENTITY_SUCCESS': {
case 'DESTROY_ANY_ENTITIES_SUCCESS': {
return slice(state, payload.data)
}
case 'CLEAR_ANY_ENTITIES': {
return clear(state, entityTypes)
}
default:
return state
}
}
export default reducer
All functions are immutable
insert
will return new state with response entities
slice
will return new state without response entities
clear
will return new state without entities by special types
import { transform } from 'json-api-transform'
const transformerFunction = (state, currentEntitiesArray, currentEntitiesTypeString) => {
return state
}
myCustomTransform = (state, data) => transform(state, data, transformerFunction)
transformerFunction
is function that takes 3 arguments:
state
- default objectentities
- array of normalized objectstype
- type(string) of current entities
All data from response will be normalized by normalizr
function. Example:
import { normalizr } from 'json-api-transform'
data = {
"data":[
{
"id":"1",
"type":"projects",
"attributes":{
"title":"Project 1",
},
"relationships":{
"tasks":{
"data":[
{
"id":"1",
"type":"tasks"
}
]
}
}
}
],
"included":[
{
"id":"1",
"type":"tasks",
"attributes":{
"title":"Task 1.1",
"project-id":1
}
}
]
}
normalizedData = normalizr(data)
Return next
{
"entities":{
"projects":{
"byId":{
"1":{ "id":"1", "type":"projects", "title":"Project 1", "tasks":["1"] }
},
"allIds":["1"]
},
"tasks":{
"byId":{
"1":{ "id":"1", "type":"tasks", "title":"Task 1.1", "projectId":1 }
},
"allIds":["1"]
}
}
}
The components is available as open source under the terms of the MIT License.