Ghost/core/frontend/services/proxy.js
Hannah Wolfe 409dc3b534
Added frontend key to ghost_head for portal (#14782)
refs: https://github.com/TryGhost/Team/issues/1599
refs: f3d5d9cf6b

- this commit adds the concept of a frontend data service, intended for passing data to the frontend from the server in a clean way. This is the start of a new & improved pattern, to hopefully reduce coupling
- the newly added internal frontend key is then exposed through this pattern so that the frontend can make use of it
- the first use case is so that portal can use it to talk to the content API instead of having weird endpoints for portal
- this key will also be used by other internal scripts in future, it's public and therefore safe to expose, but it's meant for internal use only and therefore is not exposed in a generic way e.g. as a helper
2022-05-11 17:34:31 +01:00

60 lines
1.9 KiB
JavaScript

// This file contains everything that the helpers and frontend apps require from the core of Ghost
const settingsCache = require('../../shared/settings-cache');
const config = require('../../shared/config');
// Require from the handlebars framework
const {SafeString} = require('./handlebars');
let _dataService = {};
module.exports = {
getFrontendKey: () => {
return _dataService.getFrontendKey();
},
/**
* Section two: data manipulation
* Stuff that modifies API data (SDK layer)
*/
metaData: require('../meta'),
socialUrls: require('@tryghost/social-urls'),
blogIcon: require('../../server/lib/image').blogIcon,
// Used by router service and {{get}} helper to prepare data for optimal usage in themes
prepareContextResource(data) {
(Array.isArray(data) ? data : [data]).forEach((resource) => {
// feature_image_caption contains HTML, making it a SafeString spares theme devs from triple-curlies
if (resource.feature_image_caption) {
resource.feature_image_caption = new SafeString(resource.feature_image_caption);
}
});
},
/**
* Section three: Core API
* Parts of Ghost core that the frontend currently needs
*/
// Config! Keys used:
// isPrivacyDisabled & referrerPolicy used in ghost_head
config: {
get: config.get.bind(config),
isPrivacyDisabled: config.isPrivacyDisabled.bind(config)
},
// TODO: Only expose "get"
settingsCache: settingsCache,
// TODO: Expose less of the API to make this safe
api: require('../../server/api').endpoints,
// Labs utils for enabling/disabling helpers
labs: require('../../shared/labs'),
// URGH... Yuk (unhelpful comment :D)
urlService: require('../../server/services/url'),
urlUtils: require('../../shared/url-utils')
};
module.exports.init = ({dataService}) => {
_dataService = dataService;
};