Skip to content

Commit

Permalink
Merge pull request #21 from dynamiccast/remove-cast-id
Browse files Browse the repository at this point in the history
Upgrade json-api-serializer to have built-in id casting to string
  • Loading branch information
dynamiccast authored Jul 11, 2016
2 parents e6b6858 + 710baee commit d8a7901
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 38 deletions.
35 changes: 1 addition & 34 deletions lib/api/services/JsonApiService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require("lodash");
const cleanObject = require('clean-object');
const JSONAPISerializer = require('json-api-serializer');
const jsonApiValidator = require('../../context-aware-jsonapi-validator/validator');
const Serializer = new JSONAPISerializer();
Expand All @@ -10,23 +9,6 @@ var createRecord = require('../blueprints/create');
var destroyOneRecord = require('../blueprints/destroy');
var updateOneRecord = require('../blueprints/update');

function deepMap(obj, iterator) {
return _.transform(obj, function(result, val, key) {
result[key] = _.isObject(val) ?
deepMap(val, iterator) :
iterator.call(this, val, key, obj);
});
}

function _setIdTypeToString(object) {

if (typeof object['id'] === "number") {
object['id'] = object['id'].toString();
}

return object;
}

module.exports = {

findRecords: findRecords,
Expand Down Expand Up @@ -107,22 +89,7 @@ module.exports = {
id: 'id'
});

var dataToSerialize = null;

// JSON API specifies resource IDs MUST be strings
// Let's convert all Sails integer index to strings
if (data instanceof Array) {
dataToSerialize = [];

data.forEach(function(resource) {
var object = _setIdTypeToString(resource);
dataToSerialize.push(object);
});
} else if (typeof data === "object") {
dataToSerialize = _setIdTypeToString(data);
}

var returnedValue = cleanObject(Serializer.serialize(modelName, dataToSerialize));
var returnedValue = Serializer.serialize(modelName, data);
delete returnedValue.jsonapi; // Let's ignore the version for now

return returnedValue;
Expand Down
1 change: 0 additions & 1 deletion lib/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

var _ = require('lodash');
var util = require('util');
var nodePath = require('path');
var pluralize = require('pluralize');
var BlueprintController = {
create : require('./api/blueprints/create')
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
},
"homepage": "https://github.com/dynamiccast/sails-json-api-blueprints#readme",
"dependencies": {
"clean-object": "^1.0.2",
"json-api-serializer": "1.0.0",
"json-api-serializer": "1.1.0",
"jsonapi-validator": "^2.0.0",
"lodash": "3.10.1",
"path": "^0.12.7",
"pluralize": "^2.0.0",
"util": "^0.10.3"
},
Expand Down

0 comments on commit d8a7901

Please sign in to comment.