Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JSON:API specific algorithm for merging included resources into original data.

License

NotificationsYou must be signed in to change notification settings

swaggerexpert/json-api-merge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

npmversionnpmNode CIDependabot enabledtry on RunKitTidelift

@swaggerexpert/json-api-merge is a JSON:API specific redundant duplication algorithm for merging included resources into original data.

Tidelift Get professionally supported @swaggerexpert/json-api-merge with Tidelift Subscription.

Getting Started

Installation

npm i @swaggerexpert/json-api-merge

or

yarn add @swaggerexpert/json-api-merge

Usage

constjsonApiData={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',},},],};

ES modules

importjsonApiMergefrom'@swaggerexpert/json-api-merge'jsonApiMerge(jsonApiData.included,jsonApiData.data)

CommonJS

constjsonApiMerge=require('@swaggerexpert/json-api-merge');jsonApiMerge(jsonApiData.included,jsonApiData.data);

Result would be following data structure.

{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 inlist format and can transform this:

{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:

[{id:1,type:'resource',attributes:{name:'Resource name',},relationships:{related:{data:{id:2,type:'related_resource',attributes:{name:'Related resource name',},},},},}]

Compound document

To reduce the number of HTTP requests, servers MAY allow responses that include related resourcesalong with the requested primary resources. Such responses are called“compound documents”.

constjsonApiData={data:[{type:'articles',id:'1',attributes:{title:'JSON:API paints my bikeshed!',},links:{self:'http://example.com/articles/1',},relationships:{author:{links:{self:'http://example.com/articles/1/relationships/author',related:'http://example.com/articles/1/author',},data:{type:'people',id:'9'},},comments:{links:{self:'http://example.com/articles/1/relationships/comments',related:'http://example.com/articles/1/comments',},data:[{type:'comments',id:'5'},{type:'comments',id:'12'},],},},},],included:[{type:'people',id:'9',attributes:{firstName:'Dan',lastName:'Gebhardt',twitter:'dgeb',},links:{self:'http://example.com/people/9',},},{type:'comments',id:'5',attributes:{body:'First!',},relationships:{author:{data:{type:'people',id:'2'},},},links:{self:'http://example.com/comments/5',},},{type:'comments',id:'12',attributes:{body:'I like XML better',},relationships:{author:{data:{type:'people',id:'9'},},},links:{self:'http://example.com/comments/12',},},],};

Compound documents can achieve full linkage with the following trick:

constincluded=jsonApiMerge(jsonApiData.included,jsonApiData.included);jsonApiMerge(included,jsonApiData.data);

This operation will generate following compound document with full linkage:

[{type:'articles',id:'1',attributes:{title:'JSON:API paints my bikeshed!',},links:{self:'http://example.com/articles/1',},relationships:{author:{links:{self:'http://example.com/articles/1/relationships/author',related:'http://example.com/articles/1/author',},data:{type:'people',id:'9',attributes:{firstName:'Dan',lastName:'Gebhardt',twitter:'dgeb',},links:{self:'http://example.com/people/9',},},},comments:{links:{self:'http://example.com/articles/1/relationships/comments',related:'http://example.com/articles/1/comments',},data:[{type:'comments',id:'5',attributes:{body:'First!',},relationships:{author:{data:{type:'people',id:'2',},},},links:{self:'http://example.com/comments/5',},},{type:'comments',id:'12',attributes:{body:'I like XML better',},relationships:{author:{data:{type:'people',id:'9',attributes:{firstName:'Dan',lastName:'Gebhardt',twitter:'dgeb',},links:{self:'http://example.com/people/9',},},},},links:{self:'http://example.com/comments/12',},},],},},},];

Motivation

I was looking for a simple way how to merge theincluded intodata without compromising datastructures. All other libraries that I tested were opinionated about how the resulting merge should look like.This library has no opinion and simply merged theincluded intodata. It does nothing else.

Contributing

If you want to contribute to this project, please consult theCONTRIBUTING.md guidelines.

Obtaining project copy

 $ git clone https://github.com/swaggerexpert/json-api-merge $ npm i

Running tests

 $ npm runtest

Running tests in browser

 $ npm run test:web

Running linter

We're usingeslint andairbnb codestyle rules withprettier integrated as an eslint plugin.

 $ npm run lint

Typescript support

Although @swaggerexpert/json-api-merge is written in ES2019, we also supportTypescript.When @swaggerexpert/json-api-merge gets imported into a Typescript project, typings are automatically imported and used.

Author

char0n (Vladimír Gorej)
contact@swaggerexpert.com
https://swaggerexport.com/

About

JSON:API specific algorithm for merging included resources into original data.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp