Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add meta to Resource Identifier Objects #265

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion browserify/jsonapi-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,15 @@ module.exports = function (jsonapi, data, opts) {

if (valueForRelationship && isFunction(valueForRelationship.then)) {
return valueForRelationship.then(function (value) {
if (relationshipData.meta) {
value.meta = relationshipData.meta
}
return value;
});
} else {
if (relationshipData.meta) {
valueForRelationship.meta = relationshipData.meta
}
return valueForRelationship;
}
});
Expand Down Expand Up @@ -532,7 +538,13 @@ module.exports = function (collectionName, record, payload, opts) {
if (typeof id !== 'undefined') { pushToIncluded(payload, included); }
}

return typeof id !== 'undefined' ? { type: type, id: id } : null;
var json = typeof id !== 'undefined' ? { type: type, id: id } : null;

if (dest && dest.meta) {
json.meta = dest.meta
}

return json
};

this.serializeNested = function (dest, current, attribute, opts) {
Expand Down
20 changes: 16 additions & 4 deletions lib/deserializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ module.exports = function (jsonapi, data, opts) {
return extractIncludes(relationshipData, ancestry);
}))
.then(function (includes) {
if (includes) { dest[keyForAttribute(key)] = includes; }
var filteredIncludes = includes.filter(item => Boolean(item));
if (filteredIncludes.length) { dest[keyForAttribute(key)] = filteredIncludes; }
});
} else {
return extractIncludes(relationship.data, ancestry)
Expand All @@ -134,13 +135,24 @@ module.exports = function (jsonapi, data, opts) {
var valueForRelationship = getValueForRelationship(relationshipData,
included);

if (valueForRelationship && isFunction(valueForRelationship.then)) {
if (!valueForRelationship) {
return null
}

if (isFunction(valueForRelationship.then)) {
return valueForRelationship.then(function (value) {
if (relationshipData.meta) {
value.meta = relationshipData.meta
}
return value;
});
} else {
return valueForRelationship;
}

if (relationshipData.meta) {
valueForRelationship.meta = relationshipData.meta
}

return valueForRelationship;
});
}

Expand Down
19 changes: 15 additions & 4 deletions lib/serializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ module.exports = function (collectionName, record, payload, opts) {
}
}

function getType(str, attrVal) {
function getType(str, attrVal, attrOpts) {
var type;
attrVal = attrVal || {};
attrOpts = attrOpts || {};

if (isFunction(opts.typeForAttribute)) {
if (isFunction(attrOpts.typeForAttribute)) {
type = attrOpts.typeForAttribute(str, attrVal);
}

if (type === undefined && isFunction(opts.typeForAttribute)) {
type = opts.typeForAttribute(str, attrVal);
}

Expand Down Expand Up @@ -190,7 +195,7 @@ module.exports = function (collectionName, record, payload, opts) {
this.serializeRef = function (dest, current, attribute, opts) {
var that = this;
var id = getRef(current, dest, opts);
var type = getType(attribute, dest);
var type = getType(attribute, dest, opts);

var relationships = [];
var includedAttrs = [];
Expand Down Expand Up @@ -230,7 +235,13 @@ module.exports = function (collectionName, record, payload, opts) {
if (typeof id !== 'undefined') { pushToIncluded(payload, included); }
}

return typeof id !== 'undefined' ? { type: type, id: id } : null;
var json = typeof id !== 'undefined' ? { type: type, id: id } : null;

if (dest && dest.meta) {
json.meta = dest.meta
}

return json
};

this.serializeNested = function (dest, current, attribute, opts) {
Expand Down
43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-serializer",
"version": "3.6.7",
"name": "@freshfx/jsonapi-serializer",
"version": "3.7.1",
"description": "A Node.js framework agnostic library for serializing your data to JSON API",
"main": "index.js",
"scripts": {
Expand Down
21 changes: 10 additions & 11 deletions test/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ describe('JSON API Deserializer', function () {
.deserialize(dataSet, function (err, json) {
expect(json).to.be.an('object');

expect(json).to.have.key('id', 'first-name', 'last-name',
expect(json).to.have.key('id', 'first-name', 'last-name',
'username', 'images');

expect(json.images).to.be.an('array').with.length(2)
Expand Down Expand Up @@ -553,7 +553,7 @@ describe('JSON API Deserializer', function () {
id: '2',
type: 'stores',
attributes: {
name: 'Fashionable Clothes'
name: 'Fashionable Clothes'
},
relationships: {
deals: {
Expand Down Expand Up @@ -619,7 +619,7 @@ describe('JSON API Deserializer', function () {
name: 'Twin Pines Mall',
id: '1',
stores: [
{
{
name: 'Tasty Food',
id: '1',
deals: [
Expand All @@ -634,10 +634,10 @@ describe('JSON API Deserializer', function () {
id: '2',
stores: [
{ name: 'Tasty Food', id: '1' }
]
}
]
}
]
}, {
}, {
name: 'Fashionable Clothes',
id: '2',
deals: [
Expand All @@ -649,10 +649,10 @@ describe('JSON API Deserializer', function () {
]
}
]
}, {
}, {
name: 'Readable Books',
id: '3'
}
}
],
deals: [
{
Expand Down Expand Up @@ -696,7 +696,7 @@ describe('JSON API Deserializer', function () {
}
]
}
]
]
});

done(null, json);
Expand Down Expand Up @@ -1180,8 +1180,7 @@ describe('JSON API Deserializer', function () {
expect(json).eql({
id: '54735750e16638ba1eee59cb',
'first-name': 'Sandro',
'last-name': 'Munda',
'addresses': []
'last-name': 'Munda'
});
done(null, json);
});
Expand Down