Ghost/core/frontend/services/rendering/format-response.js
Hannah Wolfe 08479f3816
Moved routing helpers to rendering service
- The helpers folder was full of things used for rendering pages
- It belongs as its own service so that we can see what it really does
2022-04-05 20:12:20 +01:00

61 lines
1.4 KiB
JavaScript

const _ = require('lodash');
const {prepareContextResource} = require('../proxy');
/**
* @description Formats API response into handlebars/theme format.
*
* @return {Object} containing page variables
*/
function formatPageResponse(result) {
const response = {};
if (result.posts) {
response.posts = result.posts;
prepareContextResource(response.posts);
}
if (result.meta && result.meta.pagination) {
response.pagination = result.meta.pagination;
}
_.each(result.data, function (data, name) {
prepareContextResource(data);
if (data.meta) {
// Move pagination to be a top level key
response[name] = data;
response[name].pagination = data.meta.pagination;
delete response[name].meta;
} else {
// This is a single object, don't wrap it in an array
response[name] = data[0];
}
});
return response;
}
/**
* @description Format a single resource for handlebars.
*
* @TODO
* In the future, we should return {page: entry} or {post:entry).
* But for now, we would break the themes if we just change it.
*
* @see https://github.com/TryGhost/Ghost/issues/10042.
*
* @return {Object} containing page variables
*/
function formatResponse(post) {
prepareContextResource(post);
return {
post: post
};
}
module.exports = {
entries: formatPageResponse,
entry: formatResponse
};