Last active
August 17, 2016 20:59
-
-
Save sescobb27/ce0bbe86db395f1464cc to your computer and use it in GitHub Desktop.
JSON API Serializer and 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
// JSON API deserializer as express middleware | |
app.use((req, res, next) => { | |
if (req.headers['Content-Type'] === 'application/json' || _.isEmpty(req.body)) { | |
return next(); | |
} | |
var model = new JSONAPIDeSerializer({}, req.body); | |
// Only pick not (null or undenfined) attributes | |
model = _.pick(model, (attr) => { | |
return attr !== null && attr !== undefined; | |
}); | |
req.model = model; | |
next(); | |
}); |
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
var Express = require('express'); | |
var Order = require('ec-domain').Order; | |
var debug = require('debug')('ec-api:orders'); | |
var _ = require('lodash'); | |
var Q = require('q'); | |
var relationships = require('./relationships'); | |
var Serializer = require('../serializers'); | |
var OrdersSerializer = require('../serializers/orders_serializer'); | |
var app = new Express(); | |
app.use('/:id/relationships', relationships); | |
/** | |
* Routes | |
*/ | |
app.route('/:id?') | |
.all((req, res, next) => { | |
next(); | |
}) | |
/** | |
* POST | |
*/ | |
.post((req, res, next) => { | |
var newOrder = req.model; | |
_.merge(newOrder, Serializer.normalize(req.body.data.relationships)); | |
return Order.create(newOrder).then((order) => { | |
debug('order created', order.id); | |
const jsonapi = new OrdersSerializer(order.toObject()).serialize(); | |
return res.status(201).send(jsonapi); | |
}, next); | |
}); | |
module.exports = app; |
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
var _ = require('lodash'); | |
// relationships comes in { plan: { id: '1-shirt' } } | |
// but because we are using Mongoose, id doesn't allow us to use plain JS objects | |
// so we need to extract ids | |
function normalize(relationships) { | |
var normalizedData = {}; | |
relationships = relationships || {}; | |
Object.keys(relationships).forEach((k) => { | |
let key = _.camelCase(k); | |
let relationData; | |
if (_.isArray(relationships[k])) { | |
if (_.isEmpty(relationships[k])) { | |
relationData = []; | |
} else { | |
relationData = relationships[k].data; | |
} | |
} else { | |
relationData = relationships[k] ? relationships[k].data : null; | |
} | |
if (!relationData) { | |
return; | |
} | |
if (_.isArray(relationData)) { | |
normalizedData[key] = relationData.map((relationObject) => relationObject.id); | |
} else if (relationData.id) { | |
normalizedData[key] = relationData.id; | |
} else { | |
normalizedData[key] = null; | |
} | |
}); | |
return normalizedData; | |
} | |
module.exports.normalize = normalize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment