Added JSDoc to adapter options resolver

refs https://github.com/TryGhost/Toolbox/issues/384

- Added jsdoc for intellisence/typechecking
- Cleaned up the naming of returned values to resemble the usecases a bit better
This commit is contained in:
Naz 2022-09-06 16:07:29 +08:00
parent 28791bd6bf
commit a96a7340c0
No known key found for this signature in database
3 changed files with 45 additions and 34 deletions

View File

@ -26,8 +26,8 @@ module.exports = {
getAdapter(name) {
const adapterServiceConfig = getAdapterServiceConfig(config);
const {adapterName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
return adapterManager.getAdapter(name, adapterName, adapterConfig);
return adapterManager.getAdapter(name, adapterClassName, adapterConfig);
}
};

View File

@ -1,28 +1,39 @@
module.exports = function resolveAdapterOptions(name, adapterServiceConfig) {
/**
* Resolves full configuration for an adapter. Combining base class configurations
* along with feature-specific ones
*
* @param {String} name
* @param {Object} adapterServiceConfig
*
* @returns {{adapterClassName: String, adapterConfig: Object}}
*/
const resolveAdapterOptions = (name, adapterServiceConfig) => {
const [adapterType, feature] = name.split(':');
const adapterSettings = adapterServiceConfig[adapterType];
let adapterName;
let adapterClassName;
let adapterConfig;
const hasFeatureConfig = feature && adapterSettings[feature];
if (hasFeatureConfig && adapterSettings[adapterSettings[feature]]) {
// CASE: load resource-specific adapter when there is an adapter feature
// name (String) specified as well as custom feature config
adapterName = adapterSettings[feature];
adapterConfig = adapterSettings[adapterName];
adapterClassName = adapterSettings[feature];
adapterConfig = adapterSettings[adapterClassName];
} else if (hasFeatureConfig && adapterSettings[feature].adapter) {
// CASE: load resource-specific adapter when there is an adapter feature
// name (Object) specified as well as custom feature config
adapterName = adapterSettings[feature].adapter;
const commonAdapterConfig = {...adapterSettings[adapterName]};
adapterClassName = adapterSettings[feature].adapter;
const commonAdapterConfig = {...adapterSettings[adapterClassName]};
const featureAdapterConfig = {...adapterSettings[feature]};
delete featureAdapterConfig.adapter;
adapterConfig = {...commonAdapterConfig, ...featureAdapterConfig};
} else {
adapterName = adapterSettings.active;
adapterConfig = adapterSettings[adapterName];
adapterClassName = adapterSettings.active;
adapterConfig = adapterSettings[adapterClassName];
}
return {adapterName, adapterConfig};
return {adapterClassName, adapterConfig};
};
module.exports = resolveAdapterOptions;

View File

@ -12,9 +12,9 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
adapterName.should.equal('Memory');
adapterClassName.should.equal('Memory');
should.equal(adapterConfig, undefined);
});
@ -29,9 +29,9 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
adapterName.should.equal('cloud-storage');
adapterClassName.should.equal('cloud-storage');
adapterConfig.should.deepEqual({
custom: 'configValue'
});
@ -52,9 +52,9 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
adapterName.should.equal('local-storage');
adapterClassName.should.equal('local-storage');
adapterConfig.should.deepEqual({
custom: 'localStorageConfig'
});
@ -76,17 +76,17 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(name, adapterServiceConfig);
adapterName.should.equal('cloud-storage');
adapterClassName.should.equal('cloud-storage');
adapterConfig.should.deepEqual({
custom: 'configValue'
});
});
it('combines configurations from shared adapter and feature adapter instances', function () {
const primaryAdapterName = 'cache:images';
const secondaryAdapterName = 'cache:settings';
const primaryadapterClassName = 'cache:images';
const secondaryadapterClassName = 'cache:settings';
const adapterServiceConfig = {
cache: {
Redis: {
@ -103,17 +103,17 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(primaryAdapterName, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(primaryadapterClassName, adapterServiceConfig);
adapterName.should.equal('Redis');
adapterClassName.should.equal('Redis');
adapterConfig.should.deepEqual({
commonConfigValue: 'common_config_value',
adapterConfigValue: 'images_redis_value'
});
const {adapterName: secondAdapterName, adapterConfig: secondAdapterConfig} = resolveAdapterOptions(secondaryAdapterName, adapterServiceConfig);
const {adapterClassName: secondadapterClassName, adapterConfig: secondAdapterConfig} = resolveAdapterOptions(secondaryadapterClassName, adapterServiceConfig);
secondAdapterName.should.equal('Redis');
secondadapterClassName.should.equal('Redis');
secondAdapterConfig.should.deepEqual({
commonConfigValue: 'common_config_value',
adapterConfigValue: 'settings_redis_value'
@ -121,8 +121,8 @@ describe('Adapter Manager: options resolver', function () {
});
it('combines empty configuration from shared adapter and feature adapter instances', function () {
const primaryAdapterName = 'cache:images';
const secondaryAdapterName = 'cache:settings';
const primaryadapterClassName = 'cache:images';
const secondaryadapterClassName = 'cache:settings';
const adapterServiceConfig = {
cache: {
images: {
@ -136,23 +136,23 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(primaryAdapterName, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(primaryadapterClassName, adapterServiceConfig);
adapterName.should.equal('Redis');
adapterClassName.should.equal('Redis');
adapterConfig.should.deepEqual({
adapterConfigValue: 'images_redis_value'
});
const {adapterName: secondAdapterName, adapterConfig: secondAdapterConfig} = resolveAdapterOptions(secondaryAdapterName, adapterServiceConfig);
const {adapterClassName: secondadapterClassName, adapterConfig: secondAdapterConfig} = resolveAdapterOptions(secondaryadapterClassName, adapterServiceConfig);
secondAdapterName.should.equal('Redis');
secondadapterClassName.should.equal('Redis');
secondAdapterConfig.should.deepEqual({
adapterConfigValue: 'settings_redis_value'
});
});
it('gives priority to a feature config over a common adapter config', function () {
const primaryAdapterName = 'cache:images';
const primaryadapterClassName = 'cache:images';
const adapterServiceConfig = {
cache: {
Redis: {
@ -167,9 +167,9 @@ describe('Adapter Manager: options resolver', function () {
}
};
const {adapterName, adapterConfig} = resolveAdapterOptions(primaryAdapterName, adapterServiceConfig);
const {adapterClassName, adapterConfig} = resolveAdapterOptions(primaryadapterClassName, adapterServiceConfig);
adapterName.should.equal('Redis');
adapterClassName.should.equal('Redis');
adapterConfig.should.deepEqual({
commonConfigValue: 'common_config_value',
adapterConfigValue: 'images_redis_value',