From 8abff87fa572a68ed6b75797c689f2d3440ed61a Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 01/32] Created a serialaze 'generic' method. --- README.md | 9 +++++++- lib/hook.js | 13 ++++++++++- lib/responses/{json.js => ok.js} | 4 +++- lib/serializer.js | 37 ++++++++++++++++++++++++++++++++ lib/utils/norm-utils.js | 8 ++++++- package.json | 6 +++--- 6 files changed, 70 insertions(+), 7 deletions(-) rename lib/responses/{json.js => ok.js} (95%) create mode 100644 lib/serializer.js diff --git a/README.md b/README.md index af4e5cf..9cfc52b 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,19 @@ This is a [Sails JS](http://sailsjs.org) hook for creating an API which conforms Just lift your app as normal, and your api responses will be formatted in accordance with [jsonapi.org](http://jsonapi.org/format/). +If you need to serialize data for other reasons (eg. send data trough sockets), you can use the serializeData method like this `serializedData = sails.hooks.jsonapi.serializeData(model,data);`. +This method takes 2 arguments +* *model:* The name of the model that you wish to serialize ('model' must be defined on the sails models). +* *data:* The data to be serialized. + #### Options Create a `jsonapi.js` file inside the `config/` directory of your app, and you can set the following options: | Option | Default | Description | |---------------|:---------:|---------------| | `compoundDoc` | `true` | When set to 'true' (default), response will be a [compound document](http://jsonapi.org/format/#document-compound-documents) including related resources. | +| `keyForAttribute` | `dash-case` | A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include: dash-case (default), lisp-case, spinal-case, kebab-case, underscore_case, snake_case, camelCase, CamelCase. | +| `pluralizeType` | `true` | When set to 'true' (default), the type is pluralized. | ### Known Limitations @@ -47,4 +54,4 @@ This is unfinished. So far, the following are not yet implemented: - [ ] Deleting resources - [ ] Deleting relationships -There may be more. Please submit issue reports. Or better yet, pull requests. +There may be more. Please submit issue reports. Or better yet, pull requests. \ No newline at end of file diff --git a/lib/hook.js b/lib/hook.js index 2380df4..1e38c18 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -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) { @@ -33,6 +34,8 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, + pluralizeType : true, + keyForAttribute: 'dash-case', } }, @@ -46,6 +49,10 @@ module.exports = function jsonApiHook(sails) { }); }, + serializeData: function (type,data) { + return serializer(type,data); + }, + /** * Shadow route bindings * @type {Object} @@ -54,19 +61,23 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizePayload(req, next); + next(); } } } diff --git a/lib/responses/json.js b/lib/responses/ok.js similarity index 95% rename from lib/responses/json.js rename to lib/responses/ok.js index dad7fac..a0da1c9 100644 --- a/lib/responses/json.js +++ b/lib/responses/ok.js @@ -72,7 +72,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) { diff --git a/lib/serializer.js b/lib/serializer.js new file mode 100644 index 0000000..00bcb52 --- /dev/null +++ b/lib/serializer.js @@ -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{}; + } +} \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 761e88f..4fcf412 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -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`); + return {}; + } }; diff --git a/package.json b/package.json index d905deb..a29cb4b 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,9 @@ "url": "https://github.com/IanVS/sails-hook-jsonapi" }, "dependencies": { - "jsonapi-serializer": "^3.1.0", - "lodash": "^4.5.0", - "pluralize": "^1.1.2", + "jsonapi-serializer": "^3.2.1", + "lodash": "^4.13.1", + "pluralize": "^1.2.1", "sails-build-dictionary": "^0.10.1" }, "devDependencies": { From 1e68a3a87ef5f3ecb6d80b4abfe7f285b6accd97 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 02/32] fix to to override the input on the config/jsonapi.js file --- lib/hook.js | 2 -- lib/responses/ok.js | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 1e38c18..5f121af 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -34,8 +34,6 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, - pluralizeType : true, - keyForAttribute: 'dash-case', } }, diff --git a/lib/responses/ok.js b/lib/responses/ok.js index a0da1c9..c4b08dc 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -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 }; // Add related model data relationships = modelUtils.getRelationships(req); From 610d202d43aaa047cfd874f8a70804791029eaea Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 22:53:59 -0700 Subject: [PATCH 03/32] fix for web sockets not going --- lib/hook.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 5f121af..4f10a3c 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -59,20 +59,20 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); next(); From 23bfdc06433729e7e28beb84fc2d3b8cc0d2fe7c Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 04/32] Created a serialaze 'generic' method. --- README.md | 9 +++++++- lib/hook.js | 13 ++++++++++- lib/responses/{json.js => ok.js} | 4 +++- lib/serializer.js | 37 ++++++++++++++++++++++++++++++++ lib/utils/norm-utils.js | 8 ++++++- package.json | 6 +++--- 6 files changed, 70 insertions(+), 7 deletions(-) rename lib/responses/{json.js => ok.js} (95%) create mode 100644 lib/serializer.js diff --git a/README.md b/README.md index f986e7c..6134bff 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,19 @@ This is a [Sails JS](http://sailsjs.org) hook for creating an API which conforms Just lift your app as normal, and your api responses will be formatted in accordance with [jsonapi.org](http://jsonapi.org/format/). +If you need to serialize data for other reasons (eg. send data trough sockets), you can use the serializeData method like this `serializedData = sails.hooks.jsonapi.serializeData(model,data);`. +This method takes 2 arguments +* *model:* The name of the model that you wish to serialize ('model' must be defined on the sails models). +* *data:* The data to be serialized. + #### Options Create a `jsonapi.js` file inside the `config/` directory of your app, and you can set the following options: | Option | Default | Description | |---------------|:---------:|---------------| | `compoundDoc` | `true` | When set to 'true' (default), response will be a [compound document](http://jsonapi.org/format/#document-compound-documents) including related resources. | +| `keyForAttribute` | `dash-case` | A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include: dash-case (default), lisp-case, spinal-case, kebab-case, underscore_case, snake_case, camelCase, CamelCase. | +| `pluralizeType` | `true` | When set to 'true' (default), the type is pluralized. | ### Known Limitations @@ -46,4 +53,4 @@ This is unfinished. So far, the following are not yet implemented: - [ ] Updating relationships - [ ] Deleting relationships -There may be more. Please submit issue reports. Or better yet, pull requests. +There may be more. Please submit issue reports. Or better yet, pull requests. \ No newline at end of file diff --git a/lib/hook.js b/lib/hook.js index 2380df4..1e38c18 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -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) { @@ -33,6 +34,8 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, + pluralizeType : true, + keyForAttribute: 'dash-case', } }, @@ -46,6 +49,10 @@ module.exports = function jsonApiHook(sails) { }); }, + serializeData: function (type,data) { + return serializer(type,data); + }, + /** * Shadow route bindings * @type {Object} @@ -54,19 +61,23 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizePayload(req, next); + next(); } } } diff --git a/lib/responses/json.js b/lib/responses/ok.js similarity index 95% rename from lib/responses/json.js rename to lib/responses/ok.js index ff2208f..dfc2714 100644 --- a/lib/responses/json.js +++ b/lib/responses/ok.js @@ -73,7 +73,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 if (modelUtils.isPopulatedRelationship(data, relationship)) { diff --git a/lib/serializer.js b/lib/serializer.js new file mode 100644 index 0000000..00bcb52 --- /dev/null +++ b/lib/serializer.js @@ -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{}; + } +} \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 761e88f..4fcf412 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -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`); + return {}; + } }; diff --git a/package.json b/package.json index 81d7c3c..a29cb4b 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,9 @@ "url": "https://github.com/IanVS/sails-hook-jsonapi" }, "dependencies": { - "jsonapi-serializer": "^3.2.0", - "lodash": "^4.5.0", - "pluralize": "^1.1.2", + "jsonapi-serializer": "^3.2.1", + "lodash": "^4.13.1", + "pluralize": "^1.2.1", "sails-build-dictionary": "^0.10.1" }, "devDependencies": { From 6065732573ddc91e634364897705cb7de886cc41 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 05/32] fix to to override the input on the config/jsonapi.js file --- lib/hook.js | 2 -- lib/responses/ok.js | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 1e38c18..5f121af 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -34,8 +34,6 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, - pluralizeType : true, - keyForAttribute: 'dash-case', } }, diff --git a/lib/responses/ok.js b/lib/responses/ok.js index dfc2714..8a8ada6 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -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 }; // Add related model data relationships = modelUtils.getRelationships(req); From 18dc298890bc93f83913b0c21158704985c8f2e2 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 22:53:59 -0700 Subject: [PATCH 06/32] fix for web sockets not going --- lib/hook.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 5f121af..4f10a3c 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -59,20 +59,20 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); next(); From a832828e2050a4cb5ebc1c15e9d708d99df9c550 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 11:30:12 -0700 Subject: [PATCH 07/32] Cleaning up files --- lib/hook.js | 4 +-- lib/serializer.js | 60 ++++++++++++++++++++--------------------- lib/utils/norm-utils.js | 14 +++++----- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 4f10a3c..bf5d0d3 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -47,8 +47,8 @@ module.exports = function jsonApiHook(sails) { }); }, - serializeData: function (type,data) { - return serializer(type,data); + serializeData: function (type, data) { + return serializer(type, data); }, /** diff --git a/lib/serializer.js b/lib/serializer.js index 00bcb52..14f42d2 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -3,35 +3,35 @@ 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 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{}; - } + 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{}; + } } \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 4fcf412..d96fd64 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -1,11 +1,11 @@ 'use strict'; exports.normalizeData = function (data) { - try{ - data = JSON.parse(JSON.stringify(data)); - return data; - }catch(err){ - console.error(`Unable to parse '${data}', rerurning empty object`); - return {}; - } + try{ + data = JSON.parse(JSON.stringify(data)); + return data; + }catch(err){ + console.error(`Unable to parse '${data}', returning empty object`); + return {}; + } }; From f9116736d687115c5c29ee2f18cac7ff7a513e29 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 11:47:41 -0700 Subject: [PATCH 08/32] Some more cleanup --- lib/responses/ok.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 8a8ada6..89ecbdd 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -24,12 +24,12 @@ function normalizeResArgs( args ) { }; if (isNumeric(args[0])) { return { - statusCode: args[0], + statusCode: args[0], other: args[1] }; } else return { - statusCode: args[1], + statusCode: args[1], other: args[0] }; } @@ -44,10 +44,10 @@ module.exports = function json() { var args = normalizeResArgs(arguments); var data = args.other; var statusCode = args.statusCode; - var Model, - modelName, - opts, - relationships, + var Model, + modelName, + opts, + relationships, jsonApiRes; // Get access to `req`, `res`, & `sails` @@ -62,8 +62,8 @@ module.exports = function json() { sails.log.verbose('[jsonapi] modelName ::', modelName); Model = sails.models[modelName]; opts = { - attributes: modelUtils.getAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, + attributes: modelUtils.getAttributes(Model), + keyForAttribute:sails.config.jsonapi.keyForAttribute, pluralizeType:sails.config.jsonapi.pluralizeType }; // Add related model data @@ -75,8 +75,8 @@ module.exports = function json() { Model = sails.models[collection]; // Related model attributes opts[alias] = { - attributes: modelUtils.getOwnAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, + attributes: modelUtils.getOwnAttributes(Model), + keyForAttribute:sails.config.jsonapi.keyForAttribute, pluralizeType:sails.config.jsonapi.pluralizeType }; // add models as relationships From 670e70a9f773294406b2823cfeaafc802c2eeab4 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 09/32] Created a serialaze 'generic' method. --- README.md | 9 +++++++- lib/hook.js | 13 ++++++++++- lib/responses/{json.js => ok.js} | 4 +++- lib/serializer.js | 37 ++++++++++++++++++++++++++++++++ lib/utils/norm-utils.js | 8 ++++++- package.json | 6 +++--- 6 files changed, 70 insertions(+), 7 deletions(-) rename lib/responses/{json.js => ok.js} (95%) create mode 100644 lib/serializer.js diff --git a/README.md b/README.md index f986e7c..6134bff 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,19 @@ This is a [Sails JS](http://sailsjs.org) hook for creating an API which conforms Just lift your app as normal, and your api responses will be formatted in accordance with [jsonapi.org](http://jsonapi.org/format/). +If you need to serialize data for other reasons (eg. send data trough sockets), you can use the serializeData method like this `serializedData = sails.hooks.jsonapi.serializeData(model,data);`. +This method takes 2 arguments +* *model:* The name of the model that you wish to serialize ('model' must be defined on the sails models). +* *data:* The data to be serialized. + #### Options Create a `jsonapi.js` file inside the `config/` directory of your app, and you can set the following options: | Option | Default | Description | |---------------|:---------:|---------------| | `compoundDoc` | `true` | When set to 'true' (default), response will be a [compound document](http://jsonapi.org/format/#document-compound-documents) including related resources. | +| `keyForAttribute` | `dash-case` | A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include: dash-case (default), lisp-case, spinal-case, kebab-case, underscore_case, snake_case, camelCase, CamelCase. | +| `pluralizeType` | `true` | When set to 'true' (default), the type is pluralized. | ### Known Limitations @@ -46,4 +53,4 @@ This is unfinished. So far, the following are not yet implemented: - [ ] Updating relationships - [ ] Deleting relationships -There may be more. Please submit issue reports. Or better yet, pull requests. +There may be more. Please submit issue reports. Or better yet, pull requests. \ No newline at end of file diff --git a/lib/hook.js b/lib/hook.js index 2380df4..1e38c18 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -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) { @@ -33,6 +34,8 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, + pluralizeType : true, + keyForAttribute: 'dash-case', } }, @@ -46,6 +49,10 @@ module.exports = function jsonApiHook(sails) { }); }, + serializeData: function (type,data) { + return serializer(type,data); + }, + /** * Shadow route bindings * @type {Object} @@ -54,19 +61,23 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizePayload(req, next); + next(); } } } diff --git a/lib/responses/json.js b/lib/responses/ok.js similarity index 95% rename from lib/responses/json.js rename to lib/responses/ok.js index ff2208f..dfc2714 100644 --- a/lib/responses/json.js +++ b/lib/responses/ok.js @@ -73,7 +73,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 if (modelUtils.isPopulatedRelationship(data, relationship)) { diff --git a/lib/serializer.js b/lib/serializer.js new file mode 100644 index 0000000..00bcb52 --- /dev/null +++ b/lib/serializer.js @@ -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{}; + } +} \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 761e88f..4fcf412 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -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`); + return {}; + } }; diff --git a/package.json b/package.json index 81d7c3c..a29cb4b 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,9 @@ "url": "https://github.com/IanVS/sails-hook-jsonapi" }, "dependencies": { - "jsonapi-serializer": "^3.2.0", - "lodash": "^4.5.0", - "pluralize": "^1.1.2", + "jsonapi-serializer": "^3.2.1", + "lodash": "^4.13.1", + "pluralize": "^1.2.1", "sails-build-dictionary": "^0.10.1" }, "devDependencies": { From a39ca95be32878ef0ebe4b23e5274e4afbeeaae5 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 10/32] fix to to override the input on the config/jsonapi.js file --- lib/hook.js | 2 -- lib/responses/ok.js | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 1e38c18..5f121af 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -34,8 +34,6 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, - pluralizeType : true, - keyForAttribute: 'dash-case', } }, diff --git a/lib/responses/ok.js b/lib/responses/ok.js index dfc2714..8a8ada6 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -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 }; // Add related model data relationships = modelUtils.getRelationships(req); From 66a75112890dd2f464346887b1646cf7ed905c10 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 22:53:59 -0700 Subject: [PATCH 11/32] fix for web sockets not going --- lib/hook.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 5f121af..4f10a3c 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -59,20 +59,20 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); next(); From aafc544ca839a0dff2fb649d1ffce5934ac16d82 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 12/32] Created a serialaze 'generic' method. --- lib/hook.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/hook.js b/lib/hook.js index 4f10a3c..f43d7e1 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -34,6 +34,8 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, + pluralizeType : true, + keyForAttribute: 'dash-case', } }, From 74a11e76c042cadee31f90c93674cfc37bf94fcc Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 13/32] fix to to override the input on the config/jsonapi.js file --- lib/hook.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index f43d7e1..4f10a3c 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -34,8 +34,6 @@ module.exports = function jsonApiHook(sails) { _hookTimeout: 10000, // wait 10 seconds before timing out compoundDoc : true, included : true, - pluralizeType : true, - keyForAttribute: 'dash-case', } }, From ebc65e572dc27135cbf173745af7cf8a03017d66 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 11:30:12 -0700 Subject: [PATCH 14/32] Cleaning up files --- lib/hook.js | 4 +-- lib/serializer.js | 60 ++++++++++++++++++++--------------------- lib/utils/norm-utils.js | 14 +++++----- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 4f10a3c..bf5d0d3 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -47,8 +47,8 @@ module.exports = function jsonApiHook(sails) { }); }, - serializeData: function (type,data) { - return serializer(type,data); + serializeData: function (type, data) { + return serializer(type, data); }, /** diff --git a/lib/serializer.js b/lib/serializer.js index 00bcb52..14f42d2 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -3,35 +3,35 @@ 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 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{}; - } + 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{}; + } } \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 4fcf412..d96fd64 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -1,11 +1,11 @@ 'use strict'; exports.normalizeData = function (data) { - try{ - data = JSON.parse(JSON.stringify(data)); - return data; - }catch(err){ - console.error(`Unable to parse '${data}', rerurning empty object`); - return {}; - } + try{ + data = JSON.parse(JSON.stringify(data)); + return data; + }catch(err){ + console.error(`Unable to parse '${data}', returning empty object`); + return {}; + } }; From 00a921659363dd9b04c69220819e72ebb87c6a43 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 11:47:41 -0700 Subject: [PATCH 15/32] Some more cleanup --- lib/responses/ok.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 8a8ada6..89ecbdd 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -24,12 +24,12 @@ function normalizeResArgs( args ) { }; if (isNumeric(args[0])) { return { - statusCode: args[0], + statusCode: args[0], other: args[1] }; } else return { - statusCode: args[1], + statusCode: args[1], other: args[0] }; } @@ -44,10 +44,10 @@ module.exports = function json() { var args = normalizeResArgs(arguments); var data = args.other; var statusCode = args.statusCode; - var Model, - modelName, - opts, - relationships, + var Model, + modelName, + opts, + relationships, jsonApiRes; // Get access to `req`, `res`, & `sails` @@ -62,8 +62,8 @@ module.exports = function json() { sails.log.verbose('[jsonapi] modelName ::', modelName); Model = sails.models[modelName]; opts = { - attributes: modelUtils.getAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, + attributes: modelUtils.getAttributes(Model), + keyForAttribute:sails.config.jsonapi.keyForAttribute, pluralizeType:sails.config.jsonapi.pluralizeType }; // Add related model data @@ -75,8 +75,8 @@ module.exports = function json() { Model = sails.models[collection]; // Related model attributes opts[alias] = { - attributes: modelUtils.getOwnAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, + attributes: modelUtils.getOwnAttributes(Model), + keyForAttribute:sails.config.jsonapi.keyForAttribute, pluralizeType:sails.config.jsonapi.pluralizeType }; // add models as relationships From 39502f37c378f33b5993295e02933800db4c0fd5 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 14:39:45 -0700 Subject: [PATCH 16/32] Some more cleanup... --- lib/hook.js | 4 ++-- lib/responses/ok.js | 20 ++++++++++---------- lib/serializer.js | 24 ++++++++++++------------ lib/utils/norm-utils.js | 4 ++-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index bf5d0d3..01d792b 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -15,7 +15,7 @@ module.exports = function jsonApiHook(sails) { * @param {Function} next [description] * @api private */ - function addResponseMethods (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) { @@ -25,7 +25,7 @@ module.exports = function jsonApiHook(sails) { res: res, }); }); - }; + } return { diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 89ecbdd..2134880 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -44,10 +44,10 @@ module.exports = function json() { var args = normalizeResArgs(arguments); var data = args.other; var statusCode = args.statusCode; - var Model, - modelName, - opts, - relationships, + var Model, + modelName, + opts, + relationships, jsonApiRes; // Get access to `req`, `res`, & `sails` @@ -62,9 +62,9 @@ module.exports = function json() { sails.log.verbose('[jsonapi] modelName ::', modelName); Model = sails.models[modelName]; opts = { - attributes: modelUtils.getAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, - pluralizeType:sails.config.jsonapi.pluralizeType + attributes: modelUtils.getAttributes(Model), + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType: sails.config.jsonapi.pluralizeType }; // Add related model data relationships = modelUtils.getRelationships(req); @@ -75,9 +75,9 @@ module.exports = function json() { Model = sails.models[collection]; // Related model attributes opts[alias] = { - attributes: modelUtils.getOwnAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, - pluralizeType:sails.config.jsonapi.pluralizeType + attributes: modelUtils.getOwnAttributes(Model), + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType: sails.config.jsonapi.pluralizeType }; // add models as relationships if (modelUtils.isPopulatedRelationship(data, relationship)) { diff --git a/lib/serializer.js b/lib/serializer.js index 14f42d2..1e9e496 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -2,13 +2,14 @@ const JSONAPISerializer = require('jsonapi-serializer').Serializer; -module.exports = function (modelName, data){ +module.exports = function (modelName, data) { + let sails = req._sails; let sailsModel = sails.models[modelName]; - try{ + try { if (!sailsModel || typeof sailsModel === 'undefined') { - throw `Looks like the model '${modelName}', does not exist.`; + throw new Error(`Looks like the model '${modelName}', does not exist.`); } - }catch(err){ + } catch (err) { // throw 1; console.error(err); return {}; @@ -22,16 +23,15 @@ module.exports = function (modelName, data){ }); let Serializer = new JSONAPISerializer(modelName, { attributes, - keyForAttribute:sails.config.jsonapi.keyForAttribute, - pluralizeType:sails.config.jsonapi.pluralizeType + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType: sails.config.jsonapi.pluralizeType }); - try{ + try { data = JSON.parse(JSON.stringify(data)); - return Serializer.serialize(data);; - } - catch(err){ + return Serializer.serialize(data); + } catch (err) { console.error(`Unable to parse '${data}' for model '${modelName}', rerurning empty object \n Attributes: ${attributes}`); - return{}; + return {}; } -} \ No newline at end of file +} diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index d96fd64..29ed488 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -1,10 +1,10 @@ 'use strict'; exports.normalizeData = function (data) { - try{ + try { data = JSON.parse(JSON.stringify(data)); return data; - }catch(err){ + } catch (err) { console.error(`Unable to parse '${data}', returning empty object`); return {}; } From 310fd232852df53e760d9bc77af7969ae5681d36 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 14:49:46 -0700 Subject: [PATCH 17/32] Some more cleanup... --- lib/serializer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index 1e9e496..3a45b6e 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -3,7 +3,7 @@ const JSONAPISerializer = require('jsonapi-serializer').Serializer; module.exports = function (modelName, data) { - let sails = req._sails; + let sails = this.req._sails; let sailsModel = sails.models[modelName]; try { if (!sailsModel || typeof sailsModel === 'undefined') { @@ -23,8 +23,8 @@ module.exports = function (modelName, data) { }); let Serializer = new JSONAPISerializer(modelName, { attributes, - keyForAttribute: sails.config.jsonapi.keyForAttribute, - pluralizeType: sails.config.jsonapi.pluralizeType + keyForAttribute : sails.config.jsonapi.keyForAttribute, + pluralizeType : sails.config.jsonapi.pluralizeType }); try { data = JSON.parse(JSON.stringify(data)); @@ -34,4 +34,4 @@ module.exports = function (modelName, data) { Attributes: ${attributes}`); return {}; } -} +}; From 89212743a6ae4f17340fbbb8985c0671ec64e79c Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 15:03:17 -0700 Subject: [PATCH 18/32] Some more cleanup... --- lib/serializer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index 3a45b6e..99647bc 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -23,8 +23,8 @@ module.exports = function (modelName, data) { }); let Serializer = new JSONAPISerializer(modelName, { attributes, - keyForAttribute : sails.config.jsonapi.keyForAttribute, - pluralizeType : sails.config.jsonapi.pluralizeType + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType : sails.config.jsonapi.pluralizeType }); try { data = JSON.parse(JSON.stringify(data)); From 18dd697bee10060d2a0854ecb1cb70d5f4bc13f5 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Tue, 26 Jul 2016 04:11:53 -0500 Subject: [PATCH 19/32] wait for normalizePayload to call next() --- lib/hook.js | 1 - lib/serializer.js | 3 +-- package.json | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 01d792b..bdee879 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -75,7 +75,6 @@ module.exports = function jsonApiHook(sails) { if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); - next(); } } } diff --git a/lib/serializer.js b/lib/serializer.js index 99647bc..bf45f4c 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -1,9 +1,8 @@ 'use strict'; -const JSONAPISerializer = require('jsonapi-serializer').Serializer; +var JSONAPISerializer = require('jsonapi-serializer').Serializer; module.exports = function (modelName, data) { - let sails = this.req._sails; let sailsModel = sails.models[modelName]; try { if (!sailsModel || typeof sailsModel === 'undefined') { diff --git a/package.json b/package.json index a29cb4b..1bb86df 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "url": "https://github.com/IanVS/sails-hook-jsonapi" }, "dependencies": { - "jsonapi-serializer": "^3.2.1", + "jsonapi-serializer": "^3.3.0", "lodash": "^4.13.1", "pluralize": "^1.2.1", "sails-build-dictionary": "^0.10.1" From 00fd1fa4a597407818c92feb2486353a7f05fbf7 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Thu, 8 Dec 2016 12:21:39 -0600 Subject: [PATCH 20/32] Serializer --- lib/serializer.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/serializer.js b/lib/serializer.js index bf45f4c..47a49d5 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -4,6 +4,9 @@ var JSONAPISerializer = require('jsonapi-serializer').Serializer; module.exports = function (modelName, data) { let sailsModel = sails.models[modelName]; + console.log(sailsModel); + console.log('------------'); + console.log(modelName); try { if (!sailsModel || typeof sailsModel === 'undefined') { throw new Error(`Looks like the model '${modelName}', does not exist.`); From 4e5dafd90780bc89cd6ce1aa4b8d7bb201a194ba Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Tue, 7 Feb 2017 20:55:57 -0600 Subject: [PATCH 21/32] Code cleanup --- lib/responses/ok.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 2134880..6b385a3 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -62,9 +62,9 @@ module.exports = function json() { sails.log.verbose('[jsonapi] modelName ::', modelName); Model = sails.models[modelName]; opts = { - attributes: modelUtils.getAttributes(Model), - keyForAttribute: sails.config.jsonapi.keyForAttribute, - pluralizeType: sails.config.jsonapi.pluralizeType + attributes : modelUtils.getAttributes(Model), + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType : sails.config.jsonapi.pluralizeType }; // Add related model data relationships = modelUtils.getRelationships(req); @@ -75,9 +75,9 @@ module.exports = function json() { Model = sails.models[collection]; // Related model attributes opts[alias] = { - attributes: modelUtils.getOwnAttributes(Model), - keyForAttribute: sails.config.jsonapi.keyForAttribute, - pluralizeType: sails.config.jsonapi.pluralizeType + attributes : modelUtils.getOwnAttributes(Model), + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType : sails.config.jsonapi.pluralizeType }; // add models as relationships if (modelUtils.isPopulatedRelationship(data, relationship)) { From 39acb87badf61094e8f486b2995a61be146c6955 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 8 Feb 2017 00:59:18 -0600 Subject: [PATCH 22/32] Some changes on serializer --- .eslintignore | 2 ++ lib/serializer.js | 1 + 2 files changed, 3 insertions(+) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..289a907 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +node_modules +tests/dummy/node_modules \ No newline at end of file diff --git a/lib/serializer.js b/lib/serializer.js index 47a49d5..413ab51 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -3,6 +3,7 @@ var JSONAPISerializer = require('jsonapi-serializer').Serializer; module.exports = function (modelName, data) { + let sails = this.req._sails; let sailsModel = sails.models[modelName]; console.log(sailsModel); console.log('------------'); From c674306773f482ae137c89374c0473241eb84641 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 23/32] Created a serialaze 'generic' method. --- README.md | 9 +++++++- lib/hook.js | 22 +++++++++++++------ lib/responses/{json.js => ok.js} | 4 +++- lib/serializer.js | 37 ++++++++++++++++++++++++++++++++ lib/utils/norm-utils.js | 8 ++++++- 5 files changed, 71 insertions(+), 9 deletions(-) rename lib/responses/{json.js => ok.js} (96%) create mode 100644 lib/serializer.js diff --git a/README.md b/README.md index bad4620..e220120 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,19 @@ This is a [Sails JS](http://sailsjs.org) hook for creating an API which conforms Just lift your app as normal, and your api responses will be formatted in accordance with [jsonapi.org](http://jsonapi.org/format/). +If you need to serialize data for other reasons (eg. send data trough sockets), you can use the serializeData method like this `serializedData = sails.hooks.jsonapi.serializeData(model,data);`. +This method takes 2 arguments +* *model:* The name of the model that you wish to serialize ('model' must be defined on the sails models). +* *data:* The data to be serialized. + #### Options Create a `jsonapi.js` file inside the `config/` directory of your app, and you can set the following options: | Option | Default | Description | |---------------|:---------:|---------------| | `compoundDoc` | `true` | When set to 'true' (default), response will be a [compound document](http://jsonapi.org/format/#document-compound-documents) including related resources. | +| `keyForAttribute` | `dash-case` | A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include: dash-case (default), lisp-case, spinal-case, kebab-case, underscore_case, snake_case, camelCase, CamelCase. | +| `pluralizeType` | `true` | When set to 'true' (default), the type is pluralized. | ### Known Limitations @@ -45,4 +52,4 @@ This is unfinished. So far, the following are not yet implemented: - [ ] Updating relationships - [ ] Deleting relationships -There may be more. Please submit issue reports. Or better yet, pull requests. +There may be more. Please submit issue reports. Or better yet, pull requests. \ No newline at end of file diff --git a/lib/hook.js b/lib/hook.js index 54882f0..71aaa1a 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -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'); @@ -16,7 +17,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) { @@ -26,10 +27,10 @@ module.exports = function jsonApiHook(sails) { res: res, }); }); - }; + } // binds PATCH routes - var bindPatchRoutes = function () { + let bindPatchRoutes = function () { // https://github.com/balderdashy/sails/blob/v0.12.2-0/lib/hooks/blueprints/index.js#L157-L370 _.each(sails.middleware.controllers, function eachController(controller, controllerId) { var _getRestRoute; @@ -103,9 +104,11 @@ module.exports = function jsonApiHook(sails) { defaults: { __configKey__: { - _hookTimeout: 10000, // wait 10 seconds before timing out - compoundDoc : true, - included : true, + _hookTimeout : 10000, // wait 10 seconds before timing out + compoundDoc : true, + included : true, + pluralizeType : true, + keyForAttribute: 'dash-case', } }, @@ -124,6 +127,10 @@ module.exports = function jsonApiHook(sails) { }); }, + serializeData: function (type, data) { + return serializer(type, data); + }, + /** * Shadow route bindings * @type {Object} @@ -132,17 +139,20 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { + if (req.isSocket) return; addResponseMethods(req, res); normalizePayload(req, next); }, diff --git a/lib/responses/json.js b/lib/responses/ok.js similarity index 96% rename from lib/responses/json.js rename to lib/responses/ok.js index 66cfb3e..da05140 100644 --- a/lib/responses/json.js +++ b/lib/responses/ok.js @@ -73,7 +73,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 if (modelUtils.isPopulatedRelationship(data, relationship)) { diff --git a/lib/serializer.js b/lib/serializer.js new file mode 100644 index 0000000..00bcb52 --- /dev/null +++ b/lib/serializer.js @@ -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{}; + } +} \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 761e88f..4fcf412 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -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`); + return {}; + } }; From fb203e0d013325f78c8a507ba9e37dd56896cbe4 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 24/32] fix to to override the input on the config/jsonapi.js file --- lib/responses/ok.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index da05140..8fbb563 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -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 }; // Add related model data relationships = modelUtils.getRelationships(req); From 30c2afc6636d367e90fd86385485125db3f1c296 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 22:53:59 -0700 Subject: [PATCH 25/32] fix for web sockets not going --- lib/hook.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 71aaa1a..25b97e5 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -139,20 +139,20 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { - if (req.isSocket) return; + if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); }, From af50ac4a2cdfe2bffdd1e6455abc0bf8d9bdeda4 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 26/32] Created a serialaze 'generic' method. --- lib/hook.js | 1 + package.json | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 25b97e5..e6919f3 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -160,6 +160,7 @@ module.exports = function jsonApiHook(sails) { 'PATCH /*': function (req, res, next) { addResponseMethods(req, res); normalizePayload(req, next); + next(); } } } diff --git a/package.json b/package.json index 9e28a02..833ccf6 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,10 @@ "url": "https://github.com/IanVS/sails-hook-jsonapi" }, "dependencies": { - "jsonapi-serializer": "^3.3.0", - "lodash": "^4.5.0", - "pluralize": "^1.1.2", + + "jsonapi-serializer": "^3.2.1", + "lodash": "^4.13.1", + "pluralize": "^1.2.1", "sails-build-dictionary": "^0.10.1" }, "devDependencies": { From a63972d94b6e1e33483d5c31bb16c41bd1d63fb3 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 27/32] fix to to override the input on the config/jsonapi.js file --- lib/responses/ok.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 8fbb563..322772a 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -62,9 +62,9 @@ module.exports = function json() { sails.log.verbose('[jsonapi] modelName ::', modelName); Model = sails.models[modelName]; opts = { - attributes: modelUtils.getAttributes(Model), - keyForAttribute:sails.config.jsonapi.keyForAttribute, - pluralizeType:sails.config.jsonapi.pluralizeType + attributes : modelUtils.getAttributes(Model), + keyForAttribute: sails.config.jsonapi.keyForAttribute, + pluralizeType : sails.config.jsonapi.pluralizeType }; // Add related model data relationships = modelUtils.getRelationships(req); From dc321827b47846270907ed86552727b4af46c6b5 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 11:30:12 -0700 Subject: [PATCH 28/32] Cleaning up files --- lib/serializer.js | 60 ++++++++++++++++++++--------------------- lib/utils/norm-utils.js | 14 +++++----- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index 00bcb52..14f42d2 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -3,35 +3,35 @@ 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 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{}; - } + 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{}; + } } \ No newline at end of file diff --git a/lib/utils/norm-utils.js b/lib/utils/norm-utils.js index 4fcf412..d96fd64 100644 --- a/lib/utils/norm-utils.js +++ b/lib/utils/norm-utils.js @@ -1,11 +1,11 @@ 'use strict'; exports.normalizeData = function (data) { - try{ - data = JSON.parse(JSON.stringify(data)); - return data; - }catch(err){ - console.error(`Unable to parse '${data}', rerurning empty object`); - return {}; - } + try{ + data = JSON.parse(JSON.stringify(data)); + return data; + }catch(err){ + console.error(`Unable to parse '${data}', returning empty object`); + return {}; + } }; From 81e9997f75718e4b5f80053f6cc838856ff51845 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Fri, 22 Jul 2016 11:47:41 -0700 Subject: [PATCH 29/32] Some more cleanup --- lib/responses/ok.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 322772a..706fa9d 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -24,12 +24,12 @@ function normalizeResArgs( args ) { }; if (isNumeric(args[0])) { return { - statusCode: args[0], + statusCode: args[0], other: args[1] }; } else return { - statusCode: args[1], + statusCode: args[1], other: args[0] }; } @@ -44,10 +44,10 @@ module.exports = function json() { var args = normalizeResArgs(arguments); var data = args.other; var statusCode = args.statusCode; - var Model, - modelName, - opts, - relationships, + var Model, + modelName, + opts, + relationships, jsonApiRes; // Get access to `req`, `res`, & `sails` From b7c6ef60b6cc1bdc1e6864c7ffe061ed4f7f3184 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 17:12:47 -0700 Subject: [PATCH 30/32] Created a serialaze 'generic' method. --- lib/hook.js | 21 +++++++++++++++++++++ package.json | 3 +++ 2 files changed, 24 insertions(+) diff --git a/lib/hook.js b/lib/hook.js index e6919f3..80596ca 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -17,7 +17,11 @@ module.exports = function jsonApiHook(sails) { * @param {Function} next [description] * @api private */ +<<<<<<< HEAD function addResponseMethods(req, res) { +======= + function addResponseMethods (req, res) { +>>>>>>> Created a serialaze 'generic' method. // 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) { @@ -127,8 +131,13 @@ module.exports = function jsonApiHook(sails) { }); }, +<<<<<<< HEAD serializeData: function (type, data) { return serializer(type, data); +======= + serializeData: function (type,data) { + return serializer(type,data); +>>>>>>> Created a serialaze 'generic' method. }, /** @@ -139,25 +148,37 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { +<<<<<<< HEAD if (req.isSocket) return next(); +======= + if (req.isSocket) return; +>>>>>>> Created a serialaze 'generic' method. addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { +<<<<<<< HEAD if (req.isSocket) return next(); +======= + if (req.isSocket) return; +>>>>>>> Created a serialaze 'generic' method. addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { +<<<<<<< HEAD if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); }, 'PATCH /*': function (req, res, next) { +======= + if (req.isSocket) return; +>>>>>>> Created a serialaze 'generic' method. addResponseMethods(req, res); normalizePayload(req, next); next(); diff --git a/package.json b/package.json index 833ccf6..ae463d0 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,10 @@ "url": "https://github.com/IanVS/sails-hook-jsonapi" }, "dependencies": { +<<<<<<< HEAD +======= +>>>>>>> Created a serialaze 'generic' method. "jsonapi-serializer": "^3.2.1", "lodash": "^4.13.1", "pluralize": "^1.2.1", From 15d00b391fab4607288575386ea99b5e225e5f5c Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 20 Jul 2016 18:17:54 -0700 Subject: [PATCH 31/32] fix to to override the input on the config/jsonapi.js file --- lib/hook.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 80596ca..7f474c5 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -131,13 +131,8 @@ module.exports = function jsonApiHook(sails) { }); }, -<<<<<<< HEAD serializeData: function (type, data) { return serializer(type, data); -======= - serializeData: function (type,data) { - return serializer(type,data); ->>>>>>> Created a serialaze 'generic' method. }, /** @@ -148,37 +143,25 @@ module.exports = function jsonApiHook(sails) { before: { 'all /*': function (req, res, next) { -<<<<<<< HEAD if (req.isSocket) return next(); -======= - if (req.isSocket) return; ->>>>>>> Created a serialaze 'generic' method. addResponseMethods(req, res); next(); }, 'GET /*': function (req, res, next) { -<<<<<<< HEAD if (req.isSocket) return next(); -======= - if (req.isSocket) return; ->>>>>>> Created a serialaze 'generic' method. addResponseMethods(req, res); normalizeQueryParams(req, res); next(); }, 'POST /*': function (req, res, next) { -<<<<<<< HEAD if (req.isSocket) return next(); addResponseMethods(req, res); normalizePayload(req, next); }, 'PATCH /*': function (req, res, next) { -======= - if (req.isSocket) return; ->>>>>>> Created a serialaze 'generic' method. addResponseMethods(req, res); normalizePayload(req, next); next(); From f3450042384cd12aa3a25efcd5694eadc593f267 Mon Sep 17 00:00:00 2001 From: Mario Alvarado Date: Wed, 8 Feb 2017 02:33:07 -0600 Subject: [PATCH 32/32] Removing extra spaces --- lib/responses/ok.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/responses/ok.js b/lib/responses/ok.js index 706fa9d..da53621 100644 --- a/lib/responses/ok.js +++ b/lib/responses/ok.js @@ -44,10 +44,10 @@ module.exports = function json() { var args = normalizeResArgs(arguments); var data = args.other; var statusCode = args.statusCode; - var Model, - modelName, - opts, - relationships, + var Model, + modelName, + opts, + relationships, jsonApiRes; // Get access to `req`, `res`, & `sails`