json-api-merge - JavaScript library with JSON:API algorithm for merging included...
source link: https://github.com/char0n/json-api-merge
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
JSON API Merge
json-api-merge
is a JSON:API specific algorithm for merging included resources into original data.
Getting Started
Installation
npm i @char0n/json-api-merge
or
yarn add @char0n/json-api-merge
Usage
ES2O19
const jsonApiData = { data: { id: 1, type: 'resource', attributes: { name: 'Resource name', }, relationships: { related: { data: { id: 2, type: 'related_resource', }, }, }, }, included: [ { id: 2, type: 'related_resource', attributes: { name: 'Related resource name', }, }, ], };
import jsonApiMerge from '@char0n/json-api-merge' jsonApiMerge(jsonApiData.included, jsonApiData.data)
Node
const jsonApiMerge = require('@char0n/json-api-merge'); jsonApiMerge(jsonApiData.included, jsonApiData.data);
Result would be following data structure.
const jsonApiData = { id: 1, type: 'resource', attributes: { name: 'Resource name', }, relationships: { related: { data: { id: 2, type: 'related_resource', attributes: { name: 'Related resource name', }, }, }, }, }
The library can also process data in list format and can transform this:
const jsonApiData = { data: [ { id: 1, type: 'resource', attributes: { name: 'Resource name', }, relationships: { related: { data: { id: 2, type: 'related_resource', }, }, }, } ], included: [ { id: 2, type: 'related_resource', attributes: { name: 'Related resource name', }, }, ], };
into this:
const jsonApiData = [ { id: 1, type: 'resource', attributes: { name: 'Resource name', }, relationships: { related: { data: { id: 2, type: 'related_resource', attributes: { name: 'Related resource name', }, }, }, }, } ];
Motivation
I was looking for a simple way how to merge the included
into data
without compromising data
structures. All other libraries that I tested were opionated about how the resulting merge should look like.
This library has no opinion and simply merged the included
into data
. It does nothing else.
Requirements
Peer dependencies of Ramda and RamdaAdjunct .
- ramda >= 0.19.0 <= 0.26.1
- ramda-adjunct >=0.3.0
Contributing
If you want to contribute to this project, please consult the CONTRIBUTING.md guidelines.
Obtaining project copy
$ git clone https://github.com/char0n/json-api-merge $ npm i
Running tests
$ npm run test
Running tests in browser
$ npm run test:web
Running code coverage numbers
$ npm run coverage
Running linter
We're using eslint and airbnb codestyle rules with prettier integrated as an eslint plugin.
$ npm run lint
Typescript support
Although json-api-merge is written in ES2019, we also support Typescript . When json-api-merge gets imported into a Typescript project, typings are automatically imported and used.
Author
char0n (Vladimir Gorej)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK