From 9f50e941eb118402a525074457eebdeed3c100d8 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 21 Apr 2021 14:57:07 +0100 Subject: [PATCH] Added default API version to config refs: https://github.com/TryGhost/Team/issues/527 refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203 - We have default API versions littered all over the codebase. When we updated to Ghost v4 we realised just how many and how much of a pain in the ass this is to manage. - This creates a config value we can use. It's in overrides for the time being because we usually default to that until there is a usecase for it being overridable. If there is one, cool, change it! - The main motivation for adding this now and only using it in boot and urlUtils is as part of work to decouple the theme service into logical compontents, because the engines system inside of themes has its own default, and this is one cause of tight coupling - Expectation is that we'll slowly roll out use of the new default, hopefully without requiring config in any additional places (e.g. passing the version in from the boot file) --- core/boot.js | 7 ++++--- core/shared/config/overrides.json | 1 + core/shared/url-utils.js | 4 +--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/boot.js b/core/boot.js index 9f6a6e7849..264c8d2d2e 100644 --- a/core/boot.js +++ b/core/boot.js @@ -134,6 +134,9 @@ async function initExpressApps() { async function initServices({config}) { debug('Begin: initServices'); + const defaultApiVersion = config.get('api:versions:default'); + debug(`Default API Version: ${defaultApiVersion}`); + debug('Begin: Dynamic Routing'); // Dynamic routing is generated from the routes.yaml file, which is part of the settings service // When Ghost's DB and core are loaded, we can access this file and call routing.bootstrap.start @@ -170,9 +173,7 @@ async function initServices({config}) { appService.init(), limits.init(), scheduling.init({ - // NOTE: When changing API version need to consider how to migrate custom scheduling adapters - // that rely on URL to lookup persisted scheduled records (jobs, etc.). Ref: https://github.com/TryGhost/Ghost/pull/10726#issuecomment-489557162 - apiUrl: urlUtils.urlFor('api', {version: 'v4', versionType: 'admin'}, true) + apiUrl: urlUtils.urlFor('api', {version: defaultApiVersion, versionType: 'admin'}, true) }) ]); debug('End: Services'); diff --git a/core/shared/config/overrides.json b/core/shared/config/overrides.json index 7809f9b6ba..b26fd6aba5 100644 --- a/core/shared/config/overrides.json +++ b/core/shared/config/overrides.json @@ -62,6 +62,7 @@ "api": { "versions": { "all": ["v2", "v3", "v4", "canary"], + "default": "v4", "canary": { "admin": "canary/admin", "content": "canary/content" diff --git a/core/shared/url-utils.js b/core/shared/url-utils.js index eebe4d7b37..da74e3c015 100644 --- a/core/shared/url-utils.js +++ b/core/shared/url-utils.js @@ -1,13 +1,11 @@ const UrlUtils = require('@tryghost/url-utils'); const config = require('./config'); -const DEFAULT_GHOST_API_VERSION = 'v4'; - const urlUtils = new UrlUtils({ url: config.get('url'), adminUrl: config.get('admin:url'), apiVersions: config.get('api:versions'), - defaultApiVersion: DEFAULT_GHOST_API_VERSION, + defaultApiVersion: config.get('api:versions:default'), slugs: config.get('slugs').protected, redirectCacheMaxAge: config.get('caching:301:maxAge'), baseApiPath: '/ghost/api',