Ghost/core/server/controllers/frontend/fetch-data.js

134 lines
3.9 KiB
JavaScript
Raw Normal View History

/**
* # Fetch Data
* Dynamically build and execute queries on the API for channels
*/
var api = require('../../api'),
_ = require('lodash'),
Promise = require('bluebird'),
queryDefaults,
defaultPostQuery = {};
// The default settings for a default post query
queryDefaults = {
type: 'browse',
resource: 'posts',
options: {}
};
// Default post query needs to always include author & tags
_.extend(defaultPostQuery, queryDefaults, {
options: {
include: 'author,tags,fields'
}
});
/**
* ## Fetch Posts Per page
* Grab the postsPerPage setting from the database
*
* @param {Object} options
* @returns {Object} postOptions
*/
function fetchPostsPerPage(options) {
options = options || {};
return api.settings.read('postsPerPage').then(function then(response) {
var postsPerPage = parseInt(response.settings[0].value);
// No negative posts per page, must be number
if (!isNaN(postsPerPage) && postsPerPage > 0) {
options.limit = postsPerPage;
}
// Ensure the options key is present, so this can be merged with other options
return {options: options};
});
}
/**
* @typedef query
* @
*/
/**
* ## Process Query
* Takes a 'query' object, ensures that type, resource and options are set
* Replaces occurrences of `%s` in options with slugParam
* Converts the query config to a promise for the result
*
* @param {{type: String, resource: String, options: Object}} query
* @param {String} slugParam
* @returns {Promise} promise for an API call
*/
function processQuery(query, slugParam) {
query = _.cloneDeep(query);
// Ensure that all the properties are filled out
_.defaultsDeep(query, queryDefaults);
// Replace any slugs
_.each(query.options, function (option, name) {
query.options[name] = _.isString(option) ? option.replace(/%s/g, slugParam) : option;
});
// Return a promise for the api query
return api[query.resource][query.type](query.options);
}
/**
* ## Fetch Data
* Calls out to get posts per page, builds the final posts query & builds any additional queries
* Wraps the queries using Promise.props to ensure it gets named responses
* Does a first round of formatting on the response, and returns
*
* @param {Object} channelOptions
* @param {String} slugParam
* @returns {Promise} response
*/
function fetchData(channelOptions, slugParam) {
// Temporary workaround to make RSS work, moving towards dynamic channels will provide opportunities to
// improve this, I hope :)
function handlePostsPerPage(channelOptions) {
if (channelOptions.isRSS) {
return Promise.resolve({options: channelOptions.postOptions});
} else {
return fetchPostsPerPage(channelOptions.postOptions);
}
}
return handlePostsPerPage(channelOptions).then(function fetchData(pageOptions) {
var postQuery,
props = {};
// All channels must have a posts query, use the default if not provided
postQuery = _.defaultsDeep({}, pageOptions, defaultPostQuery);
props.posts = processQuery(postQuery, slugParam);
_.each(channelOptions.data, function (query, name) {
props[name] = processQuery(query, slugParam);
});
return Promise.props(props).then(function formatResponse(results) {
var response = _.cloneDeep(results.posts);
delete results.posts;
// process any remaining data
if (!_.isEmpty(results)) {
response.data = {};
_.each(results, function (result, name) {
if (channelOptions.data[name].type === 'browse') {
response.data[name] = result;
} else {
response.data[name] = result[channelOptions.data[name].resource];
}
});
}
return response;
});
});
}
module.exports = fetchData;