Package Exports
- sarala-json-api-data-formatter
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sarala-json-api-data-formatter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
sarala-json-api-data-formatter
A fluent, framework-agnostic, JavaScript library, that can be used simply, to transform standard JSON API responses to simple JSON objects and vice versa.
Install
$ npm i sarala-json-api-data-formatter --save
$ yarn add sarala-json-api-data-formatter
JSON-API response data sample
const data = {
"data": [
{
"type": "posts",
"id": "1",
"attributes": {
"slug": "sarala-json-api-data-formatter",
"title": "Sarala json-api data formatter",
"subtitle": "A fluent, framework-agnostic, JavaScript library, that can be used simply, to transform standard JSON API responses to simple JSON objects and vice versa.",
"published_at": "2018-01-21"
},
"relationships": {
"tags": {
"data": [
{
"type": "tags",
"id": "1"
},
{
"type": "tags",
"id": "2"
}
]
}
}
}
],
"included": [
{
"type": "tags",
"id": "1",
"attributes": {
"name": "json-api"
}
},
{
"type": "tags",
"id": "2",
"attributes": {
"name": "transform"
}
}
]
};
Simple object data sample
const simpleObject = [
{
"id": "1",
"type": "posts",
"slug": "sarala-json-api-data-formatter",
"title": "Sarala json-api data formatter",
"subtitle": "A fluent, framework-agnostic, JavaScript library, that can be used simply, to transform standard JSON API responses to simple JSON objects and vice versa.",
"published_at": "2018-01-21",
"relationships": [
"tags"
],
"tags": [
{
"id": "1",
"type": "tags",
"name": "json-api"
},
{
"id": "2",
"type": "tags",
"name": "transform"
}
]
}
];
Deserialize
import { Formatter } from "sarala-json-api-data-formatter";
const formatter = new Formatter();
let data = this.deserialize(data);
Serialize
import { Formatter } from "sarala-json-api-data-formatter";
const formatter = new Formatter();
let data = this.serialize(data);
Filter relationships
Deserialize only root objects and skip all relationships
let data = this.includeOnly([]).deserialize(data);
Deserialize only specific relationships
When post has tags and comments, following will deserialize only root object and comments. Tags will be skipped.
let data = this.includeOnly(['comments']).deserialize(data);
Filter fields
Deserialize only specified fields
let data = this.filterFields({
posts: ['title', 'subtitle'],
tags: ['name']
}).deserialize(data);