Moved getApiVersion to a new shared "bridge" class

refs: bf0823c9a2

- Added a new bridge class that lives in shared. This should eventually be responsible for all cross-communication between the frontend and the server
- Having all the gnarly shared bits in one place should help us refactor more easily
- For now it also reduces requires between the core/server and core/frontend folders that are meant to be separate
- All calls to getApiVersion have also been renamed to getFrontendApiVersion, as this is different to the "default" API version
- Slowly getting to the point where frontend/services/themes can be moved to server/services/themes :)
This commit is contained in:
Hannah Wolfe 2021-04-23 14:19:18 +01:00
parent f21e689d1b
commit d3f20c52fd
8 changed files with 38 additions and 23 deletions

View File

@ -143,9 +143,9 @@ async function initServices({config}) {
// However this _must_ happen after the express Apps are loaded, hence why this is here and not in initFrontend
// Routing is currently tightly coupled between the frontend and backend
const routing = require('./frontend/services/routing');
const themeService = require('./frontend/services/themes');
// We pass the themeService API version here, so that the frontend services are slightly less tightly-coupled
routing.bootstrap.start(themeService.getApiVersion());
const bridge = require('./shared/bridge');
// We pass the frontend API version here, so that the frontend services are slightly less tightly-coupled
routing.bootstrap.start(bridge.getFrontendApiVersion());
const settings = require('./server/services/settings');
const frontendSettings = require('./frontend/services/settings');
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');

View File

@ -2,7 +2,7 @@ const _ = require('lodash');
const debug = require('ghost-ignition').debug('frontend:services:settings:validate');
const {i18n} = require('../proxy');
const errors = require('@tryghost/errors');
const themeService = require('../themes');
const bridge = require('../../../shared/bridge');
const _private = {};
let RESOURCE_CONFIG;
@ -421,7 +421,7 @@ module.exports = function validate(object) {
object.taxonomies = {};
}
const apiVersion = themeService.getApiVersion();
const apiVersion = bridge.getFrontendApiVersion();
debug('api version', apiVersion);

View File

@ -4,7 +4,6 @@ const {i18n: commonI18n} = require('../proxy');
const logging = require('../../../shared/logging');
const errors = require('@tryghost/errors');
const themeLoader = require('./loader');
const active = require('../theme-engine/active');
const activate = require('./activate');
const validate = require('./validate');
const i18n = require('./i18n');
@ -73,16 +72,6 @@ module.exports = {
});
},
getJSON: require('./to-json'),
getActive: active.get,
getApiVersion: function getApiVersion() {
if (this.getActive()) {
return this.getActive().engine('ghost-api');
} else {
// @TODO: refactor so we don't have to require config here?
const config = require('../../../shared/config');
return config.get('api:versions:default');
}
},
activate: function (themeName) {
const loadedTheme = list.get(themeName);

View File

@ -52,8 +52,8 @@ class Resources {
return this.resourceConfig;
}
const themeService = require('../themes');
this.resourcesAPIVersion = themeService.getApiVersion();
const bridge = require('../../../shared/bridge');
this.resourcesAPIVersion = bridge.getFrontendApiVersion();
this.resourcesConfig = require(`./configs/${this.resourcesAPIVersion}`);
}

View File

@ -1,5 +1,5 @@
const ghostVersion = require('../../../lib/ghost-version');
const themeService = require('../../../../frontend/services/themes');
const bridge = require('../../../../shared/bridge');
// ### GhostLocals Middleware
// Expose the standard locals that every request will need to have available
@ -13,7 +13,7 @@ module.exports = function ghostLocals(req, res, next) {
// relative path from the URL
res.locals.relativeUrl = req.path;
// make ghost api version available for the theme + routing
res.locals.apiVersion = themeService.getApiVersion();
res.locals.apiVersion = bridge.getFrontendApiVersion();
next();
};

View File

@ -6,6 +6,7 @@ const {URL} = require('url');
const errors = require('@tryghost/errors');
// App requires
const bridge = require('../../../shared/bridge');
const config = require('../../../shared/config');
const constants = require('@tryghost/constants');
const storage = require('../../adapters/storage');
@ -192,7 +193,7 @@ module.exports = function setupSiteApp(options = {}) {
module.exports.reload = () => {
// https://github.com/expressjs/express/issues/2596
router = siteRoutes({start: themeService.getApiVersion()});
router = siteRoutes({start: bridge.getFrontendApiVersion()});
Object.setPrototypeOf(SiteRouter, router);
// re-initialse apps (register app routers, because we have re-initialised the site routers)

25
core/shared/bridge.js Normal file
View File

@ -0,0 +1,25 @@
// @TODO: refactor constructor pattern so we don't have to require config here?
const config = require('./config');
const themeEngine = require('../frontend/services/theme-engine');
class Bridge {
constructor() {
}
getActiveTheme() {
return themeEngine.getActive();
}
getFrontendApiVersion() {
if (this.getActiveTheme()) {
return this.getActiveTheme().engine('ghost-api');
} else {
return config.get('api:versions:default');
}
}
}
const bridge = new Bridge();
module.exports = bridge;

View File

@ -1,7 +1,7 @@
const should = require('should');
const sinon = require('sinon');
const ghostLocals = require('../../../../../core/server/web/parent/middleware/ghost-locals');
const themeService = require('../../../../../core/frontend/services/themes');
const bridge = require('../../../../../core/shared/bridge');
describe('Theme Handler', function () {
let req;
@ -13,7 +13,7 @@ describe('Theme Handler', function () {
res = sinon.spy();
next = sinon.spy();
sinon.stub(themeService, 'getActive').callsFake(() => {
sinon.stub(bridge, 'getActiveTheme').callsFake(() => {
return {
engine() {
return 'v3';