Skip to content

Commit 3b1fc5a

Browse files
committed
Merge pull request #22 from SeyZ/top-level-links-for-single-resource
Fix top level links with single resource
2 parents e3ba445 + f8633dd commit 3b1fc5a

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

lib/collection-serializer.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
'use strict';
2-
var _ = require('lodash');
32
var SerializerUtils = require('./serializer-utils');
43

5-
function CollectionSerializer(collectionName, records, opts) {
6-
var payload = { data: [] };
7-
8-
function getLinks(links) {
9-
return _.mapValues(links, function (value) {
10-
if (_.isFunction(value)) {
11-
return value(records);
12-
} else {
13-
return value;
14-
}
15-
});
16-
}
17-
18-
if (opts.topLevelLinks) { payload.links = getLinks(opts.topLevelLinks); }
4+
function CollectionSerializer(payload, collectionName, records, opts) {
5+
payload.data = [];
196

207
records.forEach(function (record) {
218
var serializerUtils = new SerializerUtils(collectionName, record, payload,

lib/resource-serializer.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict';
22
var SerializerUtils = require('./serializer-utils');
33

4-
function ResourceSerializer(collectionName, record, opts) {
5-
var payload = { data: {} };
6-
7-
var serializerUtils = new SerializerUtils(collectionName, record, payload,
8-
opts);
9-
if (opts.links) { payload.links = opts.links; }
10-
payload.data = serializerUtils.perform(record);
4+
function ResourceSerializer(payload, collectionName, record, opts) {
5+
payload.data = new SerializerUtils(collectionName, record, payload, opts)
6+
.perform(record);
117

128
return payload;
139
}

lib/serializer.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@ var _ = require('lodash');
33
var CollectionSerializer = require('./collection-serializer');
44
var ResourceSerializer = require('./resource-serializer');
55

6-
module.exports = function (collectionName, data, opts) {
7-
if (_.isArray(data)) {
8-
return new CollectionSerializer(collectionName, data, opts);
6+
module.exports = function (collectionName, records, opts) {
7+
var payload = {};
8+
9+
function getLinks(links) {
10+
return _.mapValues(links, function (value) {
11+
if (_.isFunction(value)) {
12+
return value(records);
13+
} else {
14+
return value;
15+
}
16+
});
17+
}
18+
19+
if (opts.topLevelLinks) { payload.links = getLinks(opts.topLevelLinks); }
20+
21+
if (_.isArray(records)) {
22+
return new CollectionSerializer(payload, collectionName, records, opts);
923
} else {
10-
return new ResourceSerializer(collectionName, data, opts);
24+
return new ResourceSerializer(payload, collectionName, records, opts);
1125
}
26+
27+
return payload;
1228
};

test/serializer.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ describe('Options', function () {
147147
address: { 'zip_code': 42912 }
148148
});
149149

150-
console.log(require('util').inspect(json, { depth: null }));
151-
152150
done(null, json);
153151
});
154152
});
@@ -731,7 +729,7 @@ describe('JSON API Serializer', function () {
731729
});
732730
});
733731

734-
describe('Top level links', function () {
732+
describe('Top level links with an array of resources', function () {
735733
it('should be set', function (done) {
736734
var dataSet = [{
737735
id: '54735750e16638ba1eee59cb',
@@ -754,6 +752,29 @@ describe('JSON API Serializer', function () {
754752
});
755753
});
756754

755+
describe('Top level links with a single resource', function () {
756+
it('should be set', function (done) {
757+
var dataSet = {
758+
id: '54735750e16638ba1eee59cb',
759+
firstName: 'Sandro',
760+
lastName: 'Munda',
761+
};
762+
763+
var json = new JsonApiSerializer('users', dataSet, {
764+
topLevelLinks: {
765+
self: 'http://localhost:3000/api/users'
766+
},
767+
attributes: ['firstName', 'lastName'],
768+
});
769+
770+
expect(json).to.have.property('links').eql({
771+
self: 'http://localhost:3000/api/users'
772+
});
773+
774+
done(null, json);
775+
});
776+
});
777+
757778
describe('Top level links (Function)', function () {
758779
it('should be set', function (done) {
759780
var dataSet = [{

0 commit comments

Comments
 (0)