0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00
ghost/core/frontend/services/routing/controllers/static.js
Naz Gargol 7dc38e2078
🔥 Removed V1 code/references in frontend resources/routing layer (#11087)
no issue

- Removed v1 'author' leftover in include statement for preview controller
- Removed v1 'author' leftover in include statement for preview controller
- Removed v1 'author' leftover in include statement in entry lookup routing helper
- Migrated related test to use v2 API controller
- Removed v0.1 routing confif
- Removed v0.1 url config
- Fixed tests that had to do with url's in resources after removing v0.1 resources from URL cache
- Removed v1 'author' leftover in include statement in static routing helper
- Modified the test to use v2 API
- Removed v1 specific condition with 'page' in context helper
- Fixed dynamic routing spec after theme switch to v2. All tested users have to have at least one published post to be shown as an author
- Fixed URL Service spec to use theme engine v2
2019-09-10 11:41:42 +02:00

74 lines
2.6 KiB
JavaScript

const _ = require('lodash'),
Promise = require('bluebird'),
debug = require('ghost-ignition').debug('services:routing:controllers:static'),
helpers = require('../helpers'),
config = require('../../../../server/config');
function processQuery(query, locals) {
const api = require('../../../../server/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: 'authors,tags'
});
}
if (config.get('enableDeveloperExperiments')) {
Object.assign(query.options, {
context: {
members: locals.member
}
});
}
return api[query.controller][query.type](query.options);
}
/**
* @description Static route controller.
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @returns {Promise}
*/
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) {
response.data[name] = result[name][config.resource];
if (config.type === 'browse') {
response.data[name].meta = result[name].meta;
// @TODO: remove in Ghost 3.0 (see https://github.com/TryGhost/Ghost/issues/10434)
response.data[name][config.resource] = result[name][config.resource];
}
});
}
// @TODO: See helpers/secure for more context.
_.each(response.data, function (data) {
helpers.secure(req, data);
});
helpers.renderer(req, res, helpers.formatResponse.entries(response));
})
.catch(helpers.handleError(next));
};