Skip to content

Commit 8551978

Browse files
committed
added support for GET method
1 parent e0ad0c2 commit 8551978

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22
const JSONAPISerializer = require('jsonapi-serializer').Serializer;
33

44
function jsonapify(data, model, selfUrl) {
5+
const idAttribute = Object.keys(model.attributes).filter(function(attribute) {
6+
return model.attributes[attribute].primaryKey;
7+
})[0];
8+
9+
const attributes = Object.keys(model.attributes).filter(function(attribute) { return attribute !== idAttribute; });
10+
11+
const idParameter = !Array.isArray(data) ? ('/' + data[idAttribute]) : '';
12+
513
return new JSONAPISerializer(model.name, data, {
614
topLevelLinks: {
7-
self: selfUrl
15+
self: '/' + selfUrl + idParameter
816
},
9-
attributes: Object.keys(model.attributes).filter(function(attribute) {
10-
return !model.attributes[attribute].primaryKey;
11-
})
17+
attributes: attributes
1218
});
1319
}
1420

1521
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
1622
return function (hook) {
17-
hook.result = jsonapify(hook.result.data, hook.service.Model, hook.path);
23+
if (hook.method === 'find') {
24+
hook.result = jsonapify(hook.result.data, hook.service.Model, hook.path);
25+
} else if (hook.method === 'get') {
26+
hook.result = jsonapify(hook.result.toJSON(), hook.service.Model, hook.path);
27+
}
1828
return Promise.resolve(hook);
1929
};
2030
};

0 commit comments

Comments
 (0)