2014-06-03 08:05:25 -05:00
|
|
|
// # API Utils
|
|
|
|
// Shared helpers for working with the API
|
2014-08-17 01:17:23 -05:00
|
|
|
var Promise = require('bluebird'),
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
_ = require('lodash'),
|
2014-12-10 08:28:16 -05:00
|
|
|
path = require('path'),
|
2014-06-20 04:15:01 -05:00
|
|
|
errors = require('../errors'),
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
utils;
|
|
|
|
|
|
|
|
utils = {
|
2014-06-03 08:05:25 -05:00
|
|
|
/**
|
|
|
|
* ### 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
|
|
|
|
*/
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
checkObject: function (object, docName) {
|
|
|
|
if (_.isEmpty(object) || _.isEmpty(object[docName]) || _.isEmpty(object[docName][0])) {
|
2014-07-24 17:07:29 -05:00
|
|
|
return errors.logAndRejectError(new errors.BadRequestError('No root key (\'' + docName + '\') provided.'));
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
}
|
2014-07-18 03:48:48 -05:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.resolve(object);
|
2014-12-10 08:28:16 -05:00
|
|
|
},
|
|
|
|
checkFileExists: function (options, filename) {
|
|
|
|
return options[filename] && options[filename].type && options[filename].path;
|
|
|
|
},
|
|
|
|
checkFileIsValid: function (file, types, extensions) {
|
|
|
|
var type = file.type,
|
|
|
|
ext = path.extname(file.name).toLowerCase();
|
|
|
|
|
|
|
|
if (_.contains(types, type) && _.contains(extensions, ext)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
module.exports = utils;
|