From e260419f8749dc5ff631b356d66a3b39afe30beb Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 18 Oct 2022 21:43:23 +0200 Subject: [PATCH] Adjust base Redirect for easier TS typing. --- source/redirect/base.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/source/redirect/base.ts b/source/redirect/base.ts index f1da008..c3ce809 100644 --- a/source/redirect/base.ts +++ b/source/redirect/base.ts @@ -1,18 +1,31 @@ -import {HostnameParameters} from './hostname.js'; -import {SimpleParameters} from './simple.js'; +export const matcherTypes = ['hostname'] as const; +export const redirectTypes = ['hostname', 'simple'] as const; -export type RedirectParameters = HostnameParameters | SimpleParameters; +export type MatcherType = typeof matcherTypes[number]; +export type RedirectType = typeof redirectTypes[number]; + +export function narrowMatchType(type: string): type is MatcherType { + return matcherTypes.includes(type as MatcherType); +} + +export function narrowRedirectType(type: string): type is RedirectType { + return redirectTypes.includes(type as RedirectType); +} export type Matcher = { - matchType: 'hostname'; + matcherType: MatcherType; toMatch: string; }; +export type RedirectParameters = { + type: RedirectType; +}; + export abstract class Redirect

{ constructor(public parameters: P & Matcher) {} public isMatch(url: URL): boolean { - if (this.parameters.matchType === 'hostname') { + if (this.parameters.matcherType === 'hostname') { return url.hostname === this.parameters.toMatch; }