0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Simplified DynamicRedirectManager's constructor interface

refs https://linear.app/tryghost/issue/CORE-84/have-a-look-at-the-eggs-redirects-refactor-branch

- In most of the packages we follow the pattern of passing in a single "options" object into a constructor and desructuring those the object into parameter, like this example: 077c83dc2d/packages/limit-service/lib/limit-service.js (L19-L26)
This commit is contained in:
Naz 2021-10-12 13:51:20 +02:00
parent 73e11690c1
commit 80f2a001ec
2 changed files with 7 additions and 6 deletions

View file

@ -6,10 +6,11 @@ class DynamicRedirectManager {
/**
* @param {object} config
* @param {number} config.permanentMaxAge
* @param {object} config.urlUtils
*/
constructor(config, urlUtils) {
constructor({permanentMaxAge, urlUtils}) {
/** @private */
this.config = config;
this.permanentMaxAge = permanentMaxAge;
/** @private */
this.urlUtils = urlUtils;
/** @private */
@ -55,7 +56,7 @@ class DynamicRedirectManager {
const {fromRegex, to, options: {permanent}} = this.redirects[redirectId];
this.router.get(fromRegex, (req, res) => {
const maxAge = permanent ? this.config.permanentMaxAge : 0;
const maxAge = permanent ? this.permanentMaxAge : 0;
const toURL = parseURL(to);
const toURLParams = parseQuerystring(toURL.query);
const currentURL = parseURL(req.url);

View file

@ -12,8 +12,8 @@ const urlUtils = {
};
describe('DynamicRedirectManager', function () {
it('Prioritises the query params of the redirect', function () {
const manager = new DynamicRedirectManager({permanentMaxAge: 100}, urlUtils);
it('Prioritizes the query params of the redirect', function () {
const manager = new DynamicRedirectManager({permanentMaxAge: 100, urlUtils});
manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
@ -45,7 +45,7 @@ describe('DynamicRedirectManager', function () {
});
it('Allows redirects to be removed', function () {
const manager = new DynamicRedirectManager({permanentMaxAge: 100}, urlUtils);
const manager = new DynamicRedirectManager({permanentMaxAge: 100, urlUtils});
const id = manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
manager.removeRedirect(id);