2022-10-18 19:43:23 +00:00
|
|
|
export const matcherTypes = ['hostname'] as const;
|
|
|
|
export const redirectTypes = ['hostname', 'simple'] as const;
|
2022-10-05 17:08:17 +00:00
|
|
|
|
2022-10-18 19:43:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-10-05 17:08:17 +00:00
|
|
|
|
2022-10-12 22:04:51 +00:00
|
|
|
export type Matcher = {
|
2022-10-18 19:43:23 +00:00
|
|
|
matcherType: MatcherType;
|
2022-10-12 22:04:51 +00:00
|
|
|
toMatch: string;
|
|
|
|
};
|
|
|
|
|
2022-10-18 19:43:23 +00:00
|
|
|
export type RedirectParameters = {
|
|
|
|
type: RedirectType;
|
|
|
|
};
|
|
|
|
|
2022-10-05 17:08:17 +00:00
|
|
|
export abstract class Redirect<P extends RedirectParameters> {
|
2022-10-12 22:04:51 +00:00
|
|
|
constructor(public parameters: P & Matcher) {}
|
|
|
|
|
|
|
|
public isMatch(url: URL): boolean {
|
2022-10-18 19:43:23 +00:00
|
|
|
if (this.parameters.matcherType === 'hostname') {
|
2022-10-12 22:04:51 +00:00
|
|
|
return url.hostname === this.parameters.toMatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-05 17:08:17 +00:00
|
|
|
|
|
|
|
public abstract redirect(url: URL | string): URL;
|
2022-10-18 20:00:42 +00:00
|
|
|
|
|
|
|
public abstract get redirectValue(): string;
|
|
|
|
|
|
|
|
public abstract set redirectValue(value: string);
|
2022-10-05 17:08:17 +00:00
|
|
|
}
|