SerialiSON
Resolve every link of a {json:api} document
Usage
Install SerialiSON and its dependencies:
npm install serialison
If you want to use SerialiSON in a browser, compile it with npm run build
and link your HTML document to one the *.js
files in the client
directory.
Require the constructor, instanciate it with your document and call the resolve()
method:
var SerialiSON = require('serialison');
var myDocument = {
"posts": {
"id": "1",
"title": "Rails is Omakase",
"links": {
"author": {
"id": "9",
"type": "people"
},
"comments": ["1", "2", "3"]
}
},
"linked": {
"people": [{
"id": "9",
"name": "@d2h"
}],
"comments": [{
"id": "1",
"body": "Mmmmmakase"
}, {
"id": "2",
"body": "I prefer unagi"
}, {
"id": "3",
"body": "What's Omakase?"
}]
}
};
var resolver = new SerialiSON(myDocument);
var resolvedDocument = resolver.resolve();
The resolvedDocument
variable will contain the following structure:
{
"posts": {
"id": "1",
"title": "Rails is Omakase",
"author": {
"id": "9",
"name": "@d2h"
},
"comments": [{
"id": "1",
"body": "Mmmmmakase"
}, {
"id": "2",
"body": "I prefer unagi"
}, {
"id": "3",
"body": "What's Omakase?"
}]
}
}
Options
You can pass options to the constructor:
new SerialiSON(myDocument, {
});
The available options with their default values (syntax based on JSDoc):
{
throwErrorsForDuplicateIDs: true,
throwErrorsForDuplicateUrlTemplates: true,
maxNestingDepth: 4,
topLevelProperties: ['meta', 'links', 'linked'],
stripTopLinkingProperties: true,
stripLinksProperty: true,
mainDocumentTransformers: [],
resourceTransformers: []
}
Testing
To run the tests, use the following command:
npm test