Simplified knowSettings usage

refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- 'knowSettings' was based on a "configurable" array of settings that might be configured in Ghost. The multitude never happened! The only setting the frontend takes care of is routes.yaml file (redirects is also kind of a setting but is a separate concept for now).
- Having just one type of file to deal with allows to simplify implementation significantly, which helps before a big refactor
This commit is contained in:
Naz 2021-09-23 14:23:11 +02:00 committed by naz
parent 010db90a51
commit 93af11bdec
4 changed files with 10 additions and 58 deletions

View File

@ -151,7 +151,7 @@ async function initDynamicRouting() {
// We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled
const apiVersion = bridge.getFrontendApiVersion();
const routeSettings = frontendSettings.get('routes');
const routeSettings = frontendSettings.get();
debug(`Frontend API Version: ${apiVersion}`);
routing.bootstrap.start(apiVersion, routeSettings);

View File

@ -1,11 +1,8 @@
const _ = require('lodash');
const crypto = require('crypto');
const debug = require('@tryghost/debug')('frontend:services:settings:index');
const SettingsLoader = require('./loader');
const ensureSettingsFile = require('./ensure-settings');
const errors = require('@tryghost/errors');
/**
* md5 hashes of default settings
*/
@ -29,35 +26,14 @@ module.exports = {
},
/**
* Global place to switch on more available settings.
*/
knownSettings: function knownSettings() {
return ['routes'];
},
/**
* Getter for YAML settings.
* Example: `settings.get('routes').then(...)`
* will return an Object like this:
* Getter for routes YAML setting.
* Example: `settings.get().then(...)`
* will return a JSON Object like this:
* {routes: {}, collections: {}, resources: {}}
* @param {String} setting type of supported setting.
* @returns {Object} settingsFile
* @description Returns settings object as defined per YAML files in
* `/content/settings` directory.
* @returns {Object} routes.yaml in JSON format
*/
get: function get(setting) {
const knownSettings = this.knownSettings();
// CASE: this should be an edge case and only if internal usage of the
// getter is incorrect.
if (!setting || _.indexOf(knownSettings, setting) < 0) {
throw new errors.IncorrectUsageError({
message: `Requested setting is not supported: '${setting}'.`,
help: `Please use only the supported settings: ${knownSettings}.`
});
}
return SettingsLoader(setting);
get: function get() {
return SettingsLoader('routes');
},
getDefaulHash: (setting) => {

View File

@ -4,6 +4,6 @@ const frontendSettings = require('../../../frontend/services/settings');
module.exports = function siteRoutes(options = {}) {
debug('site Routes', options);
options.routerSettings = frontendSettings.get('routes');
options.routerSettings = frontendSettings.get();
return routing.bootstrap.init(options);
};

View File

@ -9,15 +9,6 @@ describe('UNIT > Settings Service:', function () {
sinon.restore();
});
describe('knownSettings', function () {
it('returns supported settings files', function () {
const files = settings.knownSettings();
// This test will fail when new settings are added without
// changing this test as well.
files.should.be.an.Array().with.length(1);
});
});
describe('get', function () {
let settingsLoaderStub;
@ -40,33 +31,18 @@ describe('UNIT > Settings Service:', function () {
settingsLoaderStub.returns(settingsStubFile);
settings.__set__('SettingsLoader', settingsLoaderStub);
const result = settings.get('routes');
const result = settings.get();
should.exist(result);
result.should.be.an.Object().with.properties('routes', 'collections', 'resources');
settingsLoaderStub.calledOnce.should.be.true();
});
it('rejects when requested settings type is not supported', function (done) {
settingsLoaderStub.returns(settingsStubFile);
settings.__set__('SettingsLoader', settingsLoaderStub);
try {
settings.get('something');
done(new Error('SettingsLoader should fail'));
} catch (err) {
should.exist(err);
err.message.should.be.eql('Requested setting is not supported: \'something\'.');
settingsLoaderStub.callCount.should.be.eql(0);
done();
}
});
it('passes SettingsLoader error through', function (done) {
settingsLoaderStub.throws(new errors.GhostError({message: 'oops'}));
settings.__set__('SettingsLoader', settingsLoaderStub);
try {
settings.get('routes');
settings.get();
done(new Error('SettingsLoader should fail'));
} catch (err) {
should.exist(err);