2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('services:routing:controllers:static');
|
2020-04-29 18:44:27 +03:00
|
|
|
const helpers = require('../helpers');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2018-10-17 10:23:59 +03:00
|
|
|
function processQuery(query, locals) {
|
2021-07-15 17:56:18 +03:00
|
|
|
const api = require('../../proxy').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.
|
2019-11-25 14:34:37 +03:00
|
|
|
if (_.get(query, 'resource') === 'posts' || _.get(query, 'resource') === 'pages') {
|
2018-12-03 21:26:21 +03:00
|
|
|
_.extend(query.options, {
|
2019-09-10 12:41:42 +03:00
|
|
|
include: 'authors,tags'
|
2018-12-03 21:26:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-14 06:04:04 +03:00
|
|
|
Object.assign(query.options, {
|
|
|
|
context: {
|
2020-08-03 14:58:55 +03:00
|
|
|
member: locals.member
|
2019-10-14 06:04:04 +03:00
|
|
|
}
|
|
|
|
});
|
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));
|
|
|
|
};
|