Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
696 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
module.exports = require('./lib/serializer'); | ||
module.exports = { | ||
Serializer: require('./lib/serializer'), | ||
Deserializer: require('./lib/deserializer'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
'use strict'; | ||
var P = require('bluebird'); | ||
var _ = require('lodash'); | ||
var Inflector = require('./inflector'); | ||
|
||
module.exports = function (jsonapi, data, opts) { | ||
function isComplexType(obj) { | ||
return _.isArray(obj) || _.isPlainObject(obj); | ||
} | ||
|
||
function getValueForRelationship(relationshipData, included) { | ||
if (opts && opts[relationshipData.type]) { | ||
var valueForRelationshipFct = opts[relationshipData.type] | ||
.valueForRelationship; | ||
|
||
return valueForRelationshipFct(relationshipData, included); | ||
} else { | ||
return included; | ||
} | ||
} | ||
|
||
function findIncluded(relationshipData) { | ||
return new P(function (resolve) { | ||
if (!jsonapi.included) { resolve(null); } | ||
|
||
var included = _.findWhere(jsonapi.included, { | ||
id: relationshipData.id, | ||
type: relationshipData.type | ||
}); | ||
|
||
if (included) { | ||
return P | ||
.all([extractAttributes(included), extractRelationships(included)]) | ||
.spread(function (attributes, relationships) { | ||
resolve(_.extend(attributes, relationships)); | ||
}); | ||
} else { | ||
return resolve(null); | ||
} | ||
}); | ||
} | ||
|
||
function keyForAttribute(attribute) { | ||
if (_.isPlainObject(attribute)) { | ||
return _.transform(attribute, function (result, value, key) { | ||
if (isComplexType(value)) { | ||
result[keyForAttribute(key)] = keyForAttribute(value); | ||
} else { | ||
result[keyForAttribute(key)] = value; | ||
} | ||
}); | ||
} else if (_.isArray(attribute)) { | ||
return attribute.map(function (attr) { | ||
if (isComplexType(attr)) { | ||
return keyForAttribute(attr); | ||
} else { | ||
return attr; | ||
} | ||
}); | ||
} else { | ||
if (_.isFunction(opts.keyForAttribute)) { | ||
return opts.keyForAttribute(attribute); | ||
} else { | ||
return Inflector.caserize(attribute, opts); | ||
} | ||
} | ||
} | ||
|
||
function extractAttributes(from) { | ||
var dest = keyForAttribute(from.attributes); | ||
dest.id = from.id; | ||
|
||
return dest; | ||
} | ||
|
||
function extractRelationships(from) { | ||
if (!from.relationships) { return; } | ||
|
||
var dest = {}; | ||
|
||
return P | ||
.each(Object.keys(from.relationships), function (key) { | ||
var relationship = from.relationships[key]; | ||
|
||
if (_.isArray(relationship.data)) { | ||
return P | ||
.map(relationship.data, function (relationshipData) { | ||
return extractIncludes(relationshipData); | ||
}) | ||
.then(function (includes) { | ||
if (includes) { dest[key] = includes; } | ||
}); | ||
} else { | ||
return extractIncludes(relationship.data) | ||
.then(function (include) { | ||
if (include) { dest[key] = include; } | ||
}); | ||
} | ||
}) | ||
.thenReturn(dest); | ||
} | ||
|
||
function extractIncludes(relationshipData) { | ||
return findIncluded(relationshipData) | ||
.then(function (included) { | ||
var valueForRelationship = getValueForRelationship(relationshipData, | ||
included); | ||
|
||
if (valueForRelationship && _.isFunction(valueForRelationship.then)) { | ||
return valueForRelationship.then(function (value) { | ||
return value; | ||
}); | ||
} else { | ||
return valueForRelationship; | ||
} | ||
}); | ||
} | ||
|
||
this.perform = function () { | ||
return P | ||
.all([extractAttributes(data), extractRelationships(data)]) | ||
.spread(function (attributes, relationships) { | ||
return _.extend(attributes, relationships); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var P = require('bluebird'); | ||
var DeserializerUtils = require('./deserializer-utils'); | ||
|
||
module.exports = function (opts) { | ||
if (!opts) { opts = {}; } | ||
|
||
this.deserialize = function (jsonapi, callback) { | ||
function collection() { | ||
return P | ||
.map(jsonapi.data, function (d) { | ||
return new DeserializerUtils(jsonapi, d, opts).perform(); | ||
}) | ||
.then(function (result) { | ||
callback(null, result); | ||
}); | ||
} | ||
|
||
function resource() { | ||
return new DeserializerUtils(jsonapi, jsonapi.data, opts) | ||
.perform() | ||
.then(function (result) { | ||
callback(null, result); | ||
}); | ||
} | ||
|
||
if (_.isArray(jsonapi.data)) { | ||
return collection(); | ||
} else { | ||
return resource(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
var Inflector = require('inflected'); | ||
|
||
module.exports = { | ||
caserize: function (attribute, opts) { | ||
attribute = Inflector.underscore(attribute); | ||
|
||
switch (opts.keyForAttribute) { | ||
case 'dash-case': | ||
case 'lisp-case': | ||
case 'spinal-case': | ||
case 'kebab-case': | ||
return Inflector.dasherize(attribute); | ||
case 'underscore_case': | ||
case 'snake_case': | ||
return attribute; | ||
case 'CamelCase': | ||
return Inflector.camelize(attribute); | ||
case 'camelCase': | ||
return Inflector.camelize(attribute, false); | ||
default: | ||
return Inflector.dasherize(attribute); | ||
} | ||
}, | ||
pluralize: function (type) { | ||
return Inflector.pluralize(type); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.