From 93096b839abdb530397f1ae048e1e8b64923d4de Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 20 Oct 2022 16:22:57 +0200 Subject: [PATCH] Simplify Redirect to a basic abstract class. Initially I was expecting the Redirect class to be more complicated for more intricate functionality. But as I've been adding the options UI, I think keeping it as simple as possible will be better and easier to work with. --- source/redirect/base.ts | 18 ++++------- source/redirect/exports.ts | 5 +-- source/redirect/hostname.ts | 17 ++-------- source/redirect/simple.ts | 17 ++-------- tests/redirect.test.ts | 34 ++++++-------------- tests/snapshots/tests/redirect.test.ts.md | 8 ++--- tests/snapshots/tests/redirect.test.ts.snap | Bin 522 -> 512 bytes 7 files changed, 27 insertions(+), 72 deletions(-) diff --git a/source/redirect/base.ts b/source/redirect/base.ts index bc5fa3c..ae20038 100644 --- a/source/redirect/base.ts +++ b/source/redirect/base.ts @@ -14,16 +14,14 @@ export function narrowRedirectType(value: string): value is RedirectType { return redirectTypes.includes(value as RedirectType); } -export type Matcher = { - matcherType: MatcherType; - toMatch: string; -}; - export type RedirectParameters = { + matcherType: MatcherType; + matcherValue: string; redirectType: RedirectType; + redirectValue: string; }; -export abstract class Redirect

{ +export abstract class Redirect { public static generateId(): string { const alphabet = 'abcdefghijklmnopqrstuvwxyz'; const nanoid = customAlphabet(`${alphabet}${alphabet.toUpperCase()}`, 20); @@ -32,7 +30,7 @@ export abstract class Redirect

{ public id: string; - constructor(public parameters: P & Matcher, id?: string) { + constructor(public parameters: RedirectParameters, id?: string) { this.id = id ?? Redirect.generateId(); } @@ -41,15 +39,11 @@ export abstract class Redirect

{ const hostname = url.hostname.startsWith('www.') ? url.hostname.slice(4) : url.hostname; - return hostname === this.parameters.toMatch; + return hostname === this.parameters.matcherValue; } return false; } public abstract redirect(url: URL | string): URL; - - public abstract get redirectValue(): string; - - public abstract set redirectValue(value: string); } diff --git a/source/redirect/exports.ts b/source/redirect/exports.ts index 5a09d65..b58f158 100644 --- a/source/redirect/exports.ts +++ b/source/redirect/exports.ts @@ -1,3 +1,4 @@ +import {RedirectParameters} from './base.js'; import {HostnameRedirect} from './hostname.js'; import {SimpleRedirect} from './simple.js'; @@ -7,8 +8,8 @@ export * from './simple.js'; export type Redirects = HostnameRedirect | SimpleRedirect; -export function parseRedirect

( - parameters: P, +export function parseRedirect( + parameters: RedirectParameters, id: string, ): Redirects | undefined { const redirectType = parameters?.redirectType; diff --git a/source/redirect/hostname.ts b/source/redirect/hostname.ts index 809075d..3596d80 100644 --- a/source/redirect/hostname.ts +++ b/source/redirect/hostname.ts @@ -1,22 +1,9 @@ import {Redirect} from './base.js'; -export type HostnameParameters = { - hostname: string; - redirectType: 'hostname'; -}; - -export class HostnameRedirect extends Redirect { +export class HostnameRedirect extends Redirect { public redirect(url: URL | string): URL { const redirected = new URL(url); - redirected.hostname = this.parameters.hostname; + redirected.hostname = this.parameters.redirectValue; return redirected; } - - public get redirectValue(): string { - return this.parameters.hostname; - } - - public set redirectValue(value: string) { - this.parameters.hostname = value; - } } diff --git a/source/redirect/simple.ts b/source/redirect/simple.ts index ffb76d6..6b580d8 100644 --- a/source/redirect/simple.ts +++ b/source/redirect/simple.ts @@ -1,20 +1,7 @@ import {Redirect} from './base.js'; -export type SimpleParameters = { - target: string; - redirectType: 'simple'; -}; - -export class SimpleRedirect extends Redirect { +export class SimpleRedirect extends Redirect { public redirect(): URL { - return new URL(this.parameters.target); - } - - public get redirectValue(): string { - return this.parameters.target; - } - - public set redirectValue(value: string) { - this.parameters.target = value; + return new URL(this.parameters.redirectValue); } } diff --git a/tests/redirect.test.ts b/tests/redirect.test.ts index d387f6f..3f8f1ba 100644 --- a/tests/redirect.test.ts +++ b/tests/redirect.test.ts @@ -15,26 +15,26 @@ import { SimpleRedirect, } from '../source/redirect/exports.js'; -const hostnameParameters: HostnameRedirect['parameters'] = { - hostname: 'example.org', +const hostnameParameters: RedirectParameters = { matcherType: 'hostname', - toMatch: 'example.com', + matcherValue: 'example.com', redirectType: 'hostname', + redirectValue: 'example.org', }; -const simpleParameters: SimpleRedirect['parameters'] = { +const simpleParameters: RedirectParameters = { matcherType: 'hostname', - target: 'https://example.org/simple', - toMatch: 'example.com', + matcherValue: 'example.com', redirectType: 'simple', + redirectValue: 'https://example.org/simple', }; test('parseRedirect', (t) => { - const samples: Array = [ + const samples: RedirectParameters[] = [ { test: 'Invalid parameters', - } as unknown as Redirects['parameters'], - undefined as unknown as Redirects['parameters'], + } as unknown as RedirectParameters, + undefined as unknown as RedirectParameters, hostnameParameters, simpleParameters, ]; @@ -56,7 +56,7 @@ test('Redirect.redirect', (t) => { const hostnameRedirect = new HostnameRedirect(hostnameParameters); const simpleRedirect = new SimpleRedirect(simpleParameters); - const samples: Array<[string, Redirect]> = [ + const samples: Array<[string, Redirect]> = [ ['https://example.com', hostnameRedirect], ['https://example.com/path#hash?query=test', hostnameRedirect], ['https://example.com', simpleRedirect], @@ -106,17 +106,3 @@ test('Narrow match & redirect types', (t) => { t.true(matcherTypes.every((value) => narrowMatcherType(value))); t.true(redirectTypes.every((value) => narrowRedirectType(value))); }); - -test('Redirect getters & setters', (t) => { - const samples: Array<[Redirects, string]> = [ - [new HostnameRedirect(hostnameParameters), hostnameParameters.hostname], - [new SimpleRedirect(simpleParameters), simpleParameters.target], - ]; - - for (const [redirect, value] of samples) { - t.is(redirect.redirectValue, value); - const newValue = `${value} test`; - redirect.redirectValue = newValue; - t.is(redirect.redirectValue, newValue); - } -}); diff --git a/tests/snapshots/tests/redirect.test.ts.md b/tests/snapshots/tests/redirect.test.ts.md index a58fc1c..8c8ccfe 100644 --- a/tests/snapshots/tests/redirect.test.ts.md +++ b/tests/snapshots/tests/redirect.test.ts.md @@ -11,10 +11,10 @@ Generated by [AVA](https://avajs.dev). HostnameRedirect { id: 'id', parameters: { - hostname: 'example.org', matcherType: 'hostname', + matcherValue: 'example.com', redirectType: 'hostname', - toMatch: 'example.com', + redirectValue: 'example.org', }, } @@ -24,9 +24,9 @@ Generated by [AVA](https://avajs.dev). id: 'id', parameters: { matcherType: 'hostname', + matcherValue: 'example.com', redirectType: 'simple', - target: 'https://example.org/simple', - toMatch: 'example.com', + redirectValue: 'https://example.org/simple', }, } diff --git a/tests/snapshots/tests/redirect.test.ts.snap b/tests/snapshots/tests/redirect.test.ts.snap index b8e6b6bf5960e9a7413a764841676ea1678174e0..779bb659f69e43a66bdb2f365178b6c7d8dd2fad 100644 GIT binary patch literal 512 zcmV+b0{{I%RzVP_w51kcQ}9 zXdqjDwmz-c%)U-LvRV~w>FI?y2h$p?| zV0BB>8K)J-FLyYVa>3tRk4d|qNfE~_j1!MZ>cHr#{fhtRzV$K@`X5wN%xwHXOuB;^L%!Txba4;vy0pIEYNUqr2>O zw=<7uoQQt}CyDeR{tSPE_*1;KyHc}TC3>*Q%goNadGCGRcjorEY`c|Hb$ut$LR&Z< zmrCpi!;+$cma1`2nJ&`4VQ}OJV5C_e^FPQ!dYQ&`vLmul$P^&I=_;&o=Q9F80W#a= z6Po7pc?LraQVd4OJ2eJr2E7GHSw_p0!7#uj#f#Q*R1pP4A(fxAw@G`?WN&1Hz}OOm zxx+M&ie>E4GG@)N%4lIgTyV#;#f&Sfn(u(av0@6j*YHH_yyiP7L+YP6_o2JxhmO@y zaUEgvvaruLY_8O#-qJ`3r&Wm(jca6wkrKnZmLKhJIyAn~;gR;9$X?0%NrUy@>+t8^ zX4D6y=9)n+s{*6n2@}y%rDCx~d{}Sw*yBv^s^ZMlsIGRZu^)7IXw-c=!Fbf*Y MJAvf|zODuU0GC$+Gynhq