From 5888dfdc97dfd6d11812bb1028ef2bda431f6fd8 Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 13 Oct 2022 00:04:51 +0200 Subject: [PATCH] Add Matcher to Redirects. --- source/redirect/base.ts | 15 ++++++++++++++- source/redirect/exports.ts | 3 +-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/source/redirect/base.ts b/source/redirect/base.ts index 376199b..76f0549 100644 --- a/source/redirect/base.ts +++ b/source/redirect/base.ts @@ -2,8 +2,21 @@ import {HostnameParameters} from './hostname.js'; export type RedirectParameters = HostnameParameters; +export type Matcher = { + matchType: 'hostname'; + toMatch: string; +}; + export abstract class Redirect

{ - 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; } diff --git a/source/redirect/exports.ts b/source/redirect/exports.ts index 0b5e5f2..6a71e66 100644 --- a/source/redirect/exports.ts +++ b/source/redirect/exports.ts @@ -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

( +export function parseRedirect

( parameters: P, ): Redirects | undefined { if (parameters.type === 'hostname') {