Skip to content

Commit fceb50a

Browse files
author
Joel Alejandro Villarreal Bertoldi
authored
Merge pull request #2 from joelalejandro/fix/issue-1
Adds support for GET methods
2 parents 7209d94 + 5aeed1e commit fceb50a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
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
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "feathers-hooks-jsonapify",
3-
"version": "0.1.0",
3+
"version": "0.1.3",
44
"description": "Feathers hook for outputting data in a JSON-API-compliant way.",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)