Add Matcher to Redirects.

This commit is contained in:
Bauke 2022-10-13 00:04:51 +02:00
parent 8e96a55661
commit 5888dfdc97
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 15 additions and 3 deletions

View File

@ -2,8 +2,21 @@ import {HostnameParameters} from './hostname.js';
export type RedirectParameters = HostnameParameters;
export type Matcher = {
matchType: 'hostname';
toMatch: string;
};
export abstract class Redirect<P extends RedirectParameters> {
constructor(public parameters: P) {}
constructor(public parameters: P & Matcher) {}
public isMatch(url: URL): boolean {
if (this.parameters.matchType === 'hostname') {
return url.hostname === this.parameters.toMatch;
}
return false;
}
public abstract redirect(url: URL | string): URL;
}

View File

@ -1,4 +1,3 @@
import {RedirectParameters} from './base.js';
import {HostnameRedirect} from './hostname.js';
export * from './base.js';
@ -6,7 +5,7 @@ export * from './hostname.js';
export type Redirects = HostnameRedirect;
export function parseRedirect<P extends RedirectParameters>(
export function parseRedirect<P extends Redirects['parameters']>(
parameters: P,
): Redirects | undefined {
if (parameters.type === 'hostname') {