-
Notifications
You must be signed in to change notification settings - Fork 14
Created a 'generic' serialize method, useful to use with web sockets. #15
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
8abff87
1e68a3a
610d202
23bfdc0
6065732
18dc298
d3e7317
a832828
f911673
670e70a
a39ca95
66a7511
aafc544
74a11e7
ebc65e5
00a9216
17c219f
39502f3
310fd23
8921274
18dd697
00fd1fa
4e5dafd
39acb87
c674306
fb203e0
30c2afc
af50ac4
a63972d
dc32182
81e9997
b7c6ef6
15d00b3
acbdc08
f345004
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| module.exports = function jsonApiHook(sails) { | ||
| var moduleUtils = require('./utils/module-utils'); | ||
| var serializer = require('./serializer'); | ||
| var _ = require('lodash'); | ||
| var normalizeQueryParams = require('./requests/query-params'); | ||
| var normalizePayload = require('./requests/payload'); | ||
|
|
@@ -14,7 +15,7 @@ module.exports = function jsonApiHook(sails) { | |
| * @param {Function} next [description] | ||
| * @api private | ||
| */ | ||
| var addResponseMethods = function (req, res) { | ||
| function addResponseMethods (req, res) { | ||
| // Attach custom responses to `res` object | ||
| // Provide access to `req` and `res` in each of their `this` contexts. | ||
| _.each(sails.middleware.jsonapi.responses, function eachMethod(responseFn, name) { | ||
|
|
@@ -46,6 +47,10 @@ module.exports = function jsonApiHook(sails) { | |
| }); | ||
| }, | ||
|
|
||
| serializeData: function (type,data) { | ||
| return serializer(type,data); | ||
| }, | ||
|
|
||
| /** | ||
| * Shadow route bindings | ||
| * @type {Object} | ||
|
|
@@ -54,19 +59,23 @@ module.exports = function jsonApiHook(sails) { | |
| before: { | ||
|
|
||
| 'all /*': function (req, res, next) { | ||
| if (req.isSocket) return next(); | ||
| addResponseMethods(req, res); | ||
| next(); | ||
| }, | ||
|
|
||
| 'GET /*': function (req, res, next) { | ||
| if (req.isSocket) return next(); | ||
| addResponseMethods(req, res); | ||
| normalizeQueryParams(req, res); | ||
| next(); | ||
| }, | ||
|
|
||
| 'POST /*': function (req, res, next) { | ||
| if (req.isSocket) return next(); | ||
| addResponseMethods(req, res); | ||
| normalizePayload(req, next); | ||
| next(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There shouldn't be a call to |
||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,9 @@ module.exports = function json() { | |
| sails.log.verbose('[jsonapi] modelName ::', modelName); | ||
| Model = sails.models[modelName]; | ||
| opts = { | ||
| attributes: modelUtils.getAttributes(Model) | ||
| attributes: modelUtils.getAttributes(Model), | ||
| keyForAttribute:sails.config.jsonapi.keyForAttribute, | ||
| pluralizeType:sails.config.jsonapi.pluralizeType | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a space after colon. |
||
| }; | ||
| // Add related model data | ||
| relationships = modelUtils.getRelationships(req); | ||
|
|
@@ -72,7 +74,9 @@ module.exports = function json() { | |
| Model = sails.models[collection]; | ||
| // Related model attributes | ||
| opts[alias] = { | ||
| attributes: modelUtils.getOwnAttributes(Model) | ||
| attributes: modelUtils.getOwnAttributes(Model), | ||
| keyForAttribute:sails.config.jsonapi.keyForAttribute, | ||
| pluralizeType:sails.config.jsonapi.pluralizeType | ||
| }; | ||
| // add models as relationships | ||
| modelUtils.getRef(Model, function (ref) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 'use strict'; | ||
|
|
||
| const JSONAPISerializer = require('jsonapi-serializer').Serializer; | ||
|
|
||
| module.exports = function (modelName, data){ | ||
| let sailsModel = sails.models[modelName]; | ||
| try{ | ||
| if (!sailsModel || typeof sailsModel === 'undefined') { | ||
| throw `Looks like the model '${modelName}', does not exist.`; | ||
| } | ||
| }catch(err){ | ||
| // throw 1; | ||
| console.error(err); | ||
| return {}; | ||
| } | ||
|
|
||
| let attributes = []; | ||
| Object.keys(sailsModel.definition).forEach(function (key) { | ||
| if (!sailsModel.definition[key].primaryKey && !sailsModel.definition[key].alias) { | ||
| attributes.push(key); | ||
| } | ||
| }); | ||
| let Serializer = new JSONAPISerializer(modelName, { | ||
| attributes, | ||
| keyForAttribute:sails.config.jsonapi.keyForAttribute, | ||
| pluralizeType:sails.config.jsonapi.pluralizeType | ||
| }); | ||
| try{ | ||
| data = JSON.parse(JSON.stringify(data)); | ||
| return Serializer.serialize(data);; | ||
| } | ||
| catch(err){ | ||
| console.error(`Unable to parse '${data}' for model '${modelName}', rerurning empty object \n | ||
| Attributes: ${attributes}`); | ||
| return{}; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please intend by 2 spaces instead of tab. It's helpful to use an editor with editorconfig support. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| 'use strict'; | ||
|
|
||
| exports.normalizeData = function (data) { | ||
| return JSON.parse(JSON.stringify(data)); | ||
| try{ | ||
| data = JSON.parse(JSON.stringify(data)); | ||
| return data; | ||
| }catch(err){ | ||
| console.error(`Unable to parse '${data}', rerurning empty object`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: "returning". |
||
| return {}; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a space after comma