Added default API version to config

refs: https://github.com/TryGhost/Team/issues/527
refs: bf0823c9a2

- 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)
This commit is contained in:
Hannah Wolfe 2021-04-21 14:57:07 +01:00
parent c687df21e1
commit 9f50e941eb
3 changed files with 6 additions and 6 deletions

View File

@ -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');

View File

@ -62,6 +62,7 @@
"api": {
"versions": {
"all": ["v2", "v3", "v4", "canary"],
"default": "v4",
"canary": {
"admin": "canary/admin",
"content": "canary/content"

View File

@ -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',