0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/services/routing/controllers/static.js
cotko 65a66ac007 Included relations if static resource is post | page (#10148)
refs #10082

- this is a requirement if a static route represents a single resource

e.g. `data: page.team`

- the page resource will no longer live on it's original static url
- instead, it now lives somewhere else
- that means the whole site needs to act the same than the original static url
  - the resource does not contain any relations
  - we don't forward the correct context (page, post, user?)
- we override the `include` property for now
  - need to wait for more use cases or bug reports for this controller
- more changes will follow asap
2018-12-03 19:26:21 +01:00

57 lines
2.1 KiB
JavaScript

const _ = require('lodash'),
Promise = require('bluebird'),
debug = require('ghost-ignition').debug('services:routing:controllers:static'),
helpers = require('../helpers');
function processQuery(query, locals) {
const api = require('../../../api')[locals.apiVersion];
query = _.cloneDeep(query);
// 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, {
include: 'author,authors,tags'
});
}
// Return a promise for the api query
return (api[query.alias] || api[query.resource])[query.type](query.options);
}
module.exports = function staticController(req, res, next) {
debug('staticController', res.routerOptions);
let props = {};
_.each(res.routerOptions.data, function (query, name) {
props[name] = processQuery(query, res.locals);
});
return Promise.props(props)
.then(function handleResult(result) {
let response = {};
if (res.routerOptions.data) {
response.data = {};
_.each(res.routerOptions.data, function (config, name) {
if (config.type === 'browse') {
response.data[name] = result[name];
} else {
response.data[name] = result[name][config.alias] || result[name][config.resource];
}
});
}
// @TODO: get rid of this O_O
_.each(response.data, function (data) {
helpers.secure(req, data);
});
helpers.renderer(req, res, helpers.formatResponse.entries(response));
})
.catch(helpers.handleError(next));
};