2018-06-24 01:32:08 +03:00
|
|
|
const _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
debug = require('ghost-ignition').debug('services:routing:controllers:static'),
|
2019-02-26 08:19:05 +03:00
|
|
|
helpers = require('../helpers'),
|
2019-06-19 12:30:28 +03:00
|
|
|
config = require('../../../../server/config');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2018-10-17 10:23:59 +03:00
|
|
|
function processQuery(query, locals) {
|
2019-06-19 12:30:28 +03:00
|
|
|
const api = require('../../../../server/api')[locals.apiVersion];
|
2018-06-24 01:32:08 +03:00
|
|
|
query = _.cloneDeep(query);
|
|
|
|
|
2018-12-03 21:26:21 +03:00
|
|
|
// CASE: If you define a single data key for a static route (e.g. data: page.team), this static route will represent
|
|
|
|
// the target resource. That means this static route has to behave the same way than the original resource url.
|
|
|
|
// e.g. the meta data package needs access to the full resource including relations.
|
|
|
|
// We override the `include` property for now, because the full data set is required anyway.
|
|
|
|
if (_.get(query, 'resource') === 'posts') {
|
|
|
|
_.extend(query.options, {
|
2019-04-22 00:55:22 +03:00
|
|
|
// @TODO: Remove "author" when we drop v0.1
|
2018-12-03 21:26:21 +03:00
|
|
|
include: 'author,authors,tags'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-26 08:19:05 +03:00
|
|
|
if (config.get('enableDeveloperExperiments')) {
|
|
|
|
Object.assign(query.options, {
|
|
|
|
context: {
|
|
|
|
members: locals.member
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-02-25 19:03:27 +03:00
|
|
|
|
2019-01-04 23:59:39 +03:00
|
|
|
return api[query.controller][query.type](query.options);
|
2018-06-24 01:32:08 +03:00
|
|
|
}
|
|
|
|
|
2019-04-22 00:55:22 +03:00
|
|
|
/**
|
|
|
|
* @description Static route controller.
|
|
|
|
* @param {Object} req
|
|
|
|
* @param {Object} res
|
|
|
|
* @param {Function} next
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2018-06-24 01:32:08 +03:00
|
|
|
module.exports = function staticController(req, res, next) {
|
2018-06-26 02:12:50 +03:00
|
|
|
debug('staticController', res.routerOptions);
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
let props = {};
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
_.each(res.routerOptions.data, function (query, name) {
|
2018-10-17 10:23:59 +03:00
|
|
|
props[name] = processQuery(query, res.locals);
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.props(props)
|
|
|
|
.then(function handleResult(result) {
|
|
|
|
let response = {};
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
if (res.routerOptions.data) {
|
2018-06-24 01:32:08 +03:00
|
|
|
response.data = {};
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
_.each(res.routerOptions.data, function (config, name) {
|
2019-02-11 14:43:01 +03:00
|
|
|
response.data[name] = result[name][config.resource];
|
|
|
|
|
2018-06-24 01:32:08 +03:00
|
|
|
if (config.type === 'browse') {
|
2019-02-11 14:43:01 +03:00
|
|
|
response.data[name].meta = result[name].meta;
|
2019-04-22 00:55:22 +03:00
|
|
|
// @TODO: remove in Ghost 3.0 (see https://github.com/TryGhost/Ghost/issues/10434)
|
2019-02-11 14:43:01 +03:00
|
|
|
response.data[name][config.resource] = result[name][config.resource];
|
2018-06-24 01:32:08 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-22 00:55:22 +03:00
|
|
|
// @TODO: See helpers/secure for more context.
|
2018-06-24 01:32:08 +03:00
|
|
|
_.each(response.data, function (data) {
|
|
|
|
helpers.secure(req, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
helpers.renderer(req, res, helpers.formatResponse.entries(response));
|
|
|
|
})
|
|
|
|
.catch(helpers.handleError(next));
|
|
|
|
};
|