As of the json-api 1.0 spec, the formats used here are out-of-date.
A helper to build json-api payload objects from plain json objects, this is it extracts embedded objects into a linked object, leaving links in the containing object with the ids of the embedded objects.
Note: this does not cover the whole json-api spec. However, it should be useful in properly converting embedded objects into linked objects, which can be very useful for mocking api responses.
So basically it takes something like this:
var post = { id: 1, title: 'P1', content: 'blah blah',
author: {id: 1, name: 'kagemusha},
comments: [ {id: 1, content: 'this post rocks'},
{id: 2, content: 'this post sucks'} ]
}
var payload = buildPayload('posts', posts)
and builds:
{ posts: [
{ id: 1, title: 'P1', content: 'blah blah',
links: { author: 1, comments: [1,2]}
}
],
linked: { authors: [
{id: 1, name: 'kagemusha}
],
comments: [
{id: 1, content: 'this post rocks'},
{id: 2, content: 'this post sucks'}
]
}
}
Other features include:
-
polymorphic types (assumes the polymorphic types has a type property)
see
should create polymorphic links with the polymorphicTypes option
test -
adding a link from embedded objects back to their containing object
see
should use optional linkBackTypes to link embedded objects to their parent
test) -
excluding objects from being extracted to linked
see
should not extract excluded keys
test -
adding a parent link to top-level objects
see
should create parent links specified with the parentLink option
test -
ad-hoc mapping of embedded objects using a different type name
see
should map with the mappings property
test
npm test
License: MIT