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.
This commit is contained in:
Bauke 2022-10-20 16:22:57 +02:00
parent d25a844fac
commit 93096b839a
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
7 changed files with 27 additions and 72 deletions

View File

@ -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<P extends RedirectParameters> {
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<P extends RedirectParameters> {
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<P extends RedirectParameters> {
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);
}

View File

@ -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<P extends Redirects['parameters']>(
parameters: P,
export function parseRedirect(
parameters: RedirectParameters,
id: string,
): Redirects | undefined {
const redirectType = parameters?.redirectType;

View File

@ -1,22 +1,9 @@
import {Redirect} from './base.js';
export type HostnameParameters = {
hostname: string;
redirectType: 'hostname';
};
export class HostnameRedirect extends Redirect<HostnameParameters> {
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;
}
}

View File

@ -1,20 +1,7 @@
import {Redirect} from './base.js';
export type SimpleParameters = {
target: string;
redirectType: 'simple';
};
export class SimpleRedirect extends Redirect<SimpleParameters> {
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);
}
}

View File

@ -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<Redirects['parameters']> = [
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<RedirectParameters>]> = [
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);
}
});

View File

@ -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',
},
}