Adjust base Redirect for easier TS typing.

This commit is contained in:
Bauke 2022-10-18 21:43:23 +02:00
parent 67a2c7567f
commit e260419f87
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 18 additions and 5 deletions

View File

@ -1,18 +1,31 @@
import {HostnameParameters} from './hostname.js'; export const matcherTypes = ['hostname'] as const;
import {SimpleParameters} from './simple.js'; 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 = { export type Matcher = {
matchType: 'hostname'; matcherType: MatcherType;
toMatch: string; toMatch: string;
}; };
export type RedirectParameters = {
type: RedirectType;
};
export abstract class Redirect<P extends RedirectParameters> { export abstract class Redirect<P extends RedirectParameters> {
constructor(public parameters: P & Matcher) {} constructor(public parameters: P & Matcher) {}
public isMatch(url: URL): boolean { public isMatch(url: URL): boolean {
if (this.parameters.matchType === 'hostname') { if (this.parameters.matcherType === 'hostname') {
return url.hostname === this.parameters.toMatch; return url.hostname === this.parameters.toMatch;
} }