62c1ce128e
no issue - author_id is converted to author for API responses but was never converted back for requests
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
// # API Utils
|
|
// Shared helpers for working with the API
|
|
var when = require('when'),
|
|
_ = require('lodash'),
|
|
errors = require('../errors'),
|
|
utils;
|
|
|
|
utils = {
|
|
/**
|
|
* ### Check Object
|
|
* Check an object passed to the API is in the correct format
|
|
*
|
|
* @param {Object} object
|
|
* @param {String} docName
|
|
* @returns {Promise(Object)} resolves to the original object if it checks out
|
|
*/
|
|
checkObject: function (object, docName) {
|
|
if (_.isEmpty(object) || _.isEmpty(object[docName]) || _.isEmpty(object[docName][0])) {
|
|
return when.reject(new errors.BadRequestError('No root key (\'' + docName + '\') provided.'));
|
|
}
|
|
|
|
// convert author property to author_id to match the name in the database
|
|
// TODO: rename object in database
|
|
if (docName === 'posts') {
|
|
if (object.posts[0].hasOwnProperty('author')) {
|
|
object.posts[0].author_id = object.posts[0].author;
|
|
delete object.posts[0].author;
|
|
}
|
|
}
|
|
return when.resolve(object);
|
|
}
|
|
};
|
|
|
|
module.exports = utils; |