Moved validation dependency to constructor param

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

- Having no external dependencies makes the class way easier to test
This commit is contained in:
Naz 2021-11-25 13:20:29 +04:00 committed by naz
parent e6c9bcdf22
commit edfe81e11c
2 changed files with 11 additions and 7 deletions

View File

@ -7,8 +7,6 @@ const logging = require('@tryghost/logging');
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const validation = require('./validation');
const messages = {
jsonParse: 'Could not parse JSON: {context}.',
yamlParse: 'Could not parse YAML: {context}.',
@ -22,7 +20,7 @@ const messages = {
* @typedef {Object} RedirectConfig
* @property {String} from - Defines the relative incoming URL or pattern (regex)
* @property {String} to - Defines where the incoming traffic should be redirected to, which can be a static URL, or a dynamic value using regex (example: "to": "/$1/")
* @property {boolean} permanent - Can be defined with true for a permanent HTTP 301 redirect, or false for a temporary HTTP 302 redirect
* @property {boolean} [permanent] - Can be defined with true for a permanent HTTP 301 redirect, or false for a temporary HTTP 302 redirect
*/
/**
@ -138,14 +136,18 @@ class CustomRedirectsAPI {
/**
* @param {object} config
* @param {string} config.basePath
* @param {Function} config.validate - validates redirects configuration
* @param {IRedirectManager} config.redirectManager
*/
constructor({basePath, redirectManager}) {
constructor({basePath, validate, redirectManager}) {
/** @private */
this.basePath = basePath;
/** @private */
this.redirectManager = redirectManager;
/**private */
this.validate = validate;
}
async init() {
@ -159,7 +161,7 @@ class CustomRedirectsAPI {
const content = await readRedirectsFile(filePath);
const ext = path.extname(filePath);
const redirects = parseRedirectsFile(content, ext);
validation.validate(redirects);
this.validate(redirects);
this.redirectManager.removeAllRedirects();
for (const redirect of redirects) {
@ -234,7 +236,7 @@ class CustomRedirectsAPI {
const content = await readRedirectsFile(filePath);
const parsed = parseRedirectsFile(content, ext);
validation.validate(parsed);
this.validate(parsed);
if (ext === '.json') {
await fs.writeFile(this.createRedirectsFilePath('.json'), JSON.stringify(parsed), 'utf-8');

View File

@ -3,6 +3,7 @@ const urlUtils = require('../../../shared/url-utils');
const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
const CustomRedirectsAPI = require('./api');
const validation = require('./validation');
let customRedirectsAPI;
let redirectManager;
@ -18,7 +19,8 @@ module.exports = {
customRedirectsAPI = new CustomRedirectsAPI({
basePath: config.getContentPath('data'),
redirectManager
redirectManager,
validate: validation.validate.bind(validation)
});
return customRedirectsAPI.init();