Refactored resource config as a DI in Resources

refs https://github.com/TryGhost/Toolbox/issues/503
refs https://github.com/TryGhost/Toolbox/issues/406

- In Ghost 5.x we dropped multi-versioned API, which means there's no need to track resource configs dynamically as there can only be one version
- Along with removed "initResourceConfig" refactored the "config" file itself to be injected into Resource's constructor - allows for easier testing.
This commit is contained in:
Naz 2023-01-20 15:50:13 +08:00
parent 3061fb2b3b
commit d44386dae3
No known key found for this signature in database
2 changed files with 6 additions and 20 deletions

View File

@ -21,10 +21,11 @@ class Resources {
* @param {Object} options
* @param {Object} [options.resources] - resources to initialize with instead of fetching them from the database
* @param {Object} [options.queue] - instance of the Queue class
* @param {Object[]} [options.resourcesConfig] - resource config used when handling resource events and fetching
*/
constructor({resources = {}, queue} = {}) {
constructor({resources = {}, queue, resourcesConfig = []} = {}) {
this.queue = queue;
this.resourcesConfig = [];
this.resourcesConfig = resourcesConfig;
this.data = resources;
this.listeners = [];
@ -47,20 +48,6 @@ class Resources {
events.on(eventName, listener);
}
/**
* @description Initialize the resource config. We currently fetch the data straight via the the model layer,
* but because Ghost supports multiple API versions, we have to ensure we load the correct data.
*
* @TODO: https://github.com/TryGhost/Ghost/issues/10360
*/
initResourceConfig() {
if (!_.isEmpty(this.resourcesConfig)) {
return;
}
this.resourcesConfig = require('./config');
}
/**
* @description Helper function to initialize data fetching.
*/
@ -440,7 +427,6 @@ class Resources {
this.listeners = [];
this.data = {};
this.resourcesConfig = null;
}
/**

View File

@ -8,6 +8,7 @@ const Queue = require('./Queue');
const Urls = require('./Urls');
const Resources = require('./Resources');
const urlUtils = require('../../../shared/url-utils');
const resourcesConfig = require('./config');
/**
* The url service class holds all instances in a centralized place.
@ -17,7 +18,7 @@ const urlUtils = require('../../../shared/url-utils');
class UrlService {
/**
*
* @param {Object} options
* @param {Object} [options]
* @param {Object} [options.cache] - cache handler instance
* @param {Function} [options.cache.read] - read cache by type
* @param {Function} [options.cache.write] - write into cache by type
@ -35,6 +36,7 @@ class UrlService {
// Way too many tests fail if the initialization is removed so leaving it as is for time being
this.urls = new Urls();
this.resources = new Resources({
resourcesConfig: resourcesConfig,
queue: this.queue
});
@ -321,12 +323,10 @@ class UrlService {
if (persistedUrls && persistedResources) {
this.urls.urls = persistedUrls;
this.resources.data = persistedResources;
this.resources.initResourceConfig();
this.resources.initEventListeners();
this._onQueueEnded('init');
} else {
this.resources.initResourceConfig();
this.resources.initEventListeners();
await this.resources.fetchResources();
// CASE: all resources are fetched, start the queue