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 b8e6b6b..779bb65 100644 Binary files a/tests/snapshots/tests/redirect.test.ts.snap and b/tests/snapshots/tests/redirect.test.ts.snap differ