Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bezrukavyi/json-api-transform
Suitable tools for transform json api response
https://github.com/bezrukavyi/json-api-transform
json-api normalizr redux-reducers
Last synced: about 2 months ago
JSON representation
Suitable tools for transform json api response
- Host: GitHub
- URL: https://github.com/bezrukavyi/json-api-transform
- Owner: bezrukavyi
- License: mit
- Created: 2018-04-22T13:31:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-04-23T19:44:46.000Z (over 6 years ago)
- Last Synced: 2024-10-04T10:35:46.722Z (3 months ago)
- Topics: json-api, normalizr, redux-reducers
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## Json Api Transform
Suitable tools for transform [json api](http://jsonapi.org/) response## Installation
npm install json-api-transform --save## Usage
In ```reducer.js``````javascript
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 to state
`insert` will return new state with response entities### Slice from state
`slice` will return new state without response entities### Clear state
`clear` will return new state without entities by special types### Custom transform function
```javascript
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:
1. `state` - default object
2. `entities` - array of normalized objects
3. `type` - type(string) of current entitiesAll data from response will be normalized by `normalizr` function. Example:
```javascript
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
```json
{
"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"]
}
}
}
```## License
The components is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).