2022-10-20 20:54:02 +00:00
|
|
|
export const matcherTypes = ['hostname', 'regex'] as const;
|
2022-11-02 10:10:29 +00:00
|
|
|
export const redirectTypes = ['hostname', 'regex', '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];
|
|
|
|
|
2022-10-19 11:31:53 +00:00
|
|
|
export function narrowMatcherType(value: string): value is MatcherType {
|
2022-10-18 20:21:55 +00:00
|
|
|
return matcherTypes.includes(value as MatcherType);
|
2022-10-18 19:43:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 20:21:55 +00:00
|
|
|
export function narrowRedirectType(value: string): value is RedirectType {
|
|
|
|
return redirectTypes.includes(value as RedirectType);
|
2022-10-18 19:43:23 +00:00
|
|
|
}
|
2022-10-05 17:08:17 +00:00
|
|
|
|
2022-10-18 19:43:23 +00:00
|
|
|
export type RedirectParameters = {
|
2022-10-26 12:47:42 +00:00
|
|
|
enabled: boolean;
|
2022-10-27 19:13:02 +00:00
|
|
|
id: number;
|
2022-10-20 14:22:57 +00:00
|
|
|
matcherType: MatcherType;
|
|
|
|
matcherValue: string;
|
2022-10-18 20:21:55 +00:00
|
|
|
redirectType: RedirectType;
|
2022-10-20 14:22:57 +00:00
|
|
|
redirectValue: string;
|
2022-10-18 19:43:23 +00:00
|
|
|
};
|
|
|
|
|
2022-10-20 14:22:57 +00:00
|
|
|
export abstract class Redirect {
|
2022-10-27 19:13:02 +00:00
|
|
|
public static idString(id: number): string {
|
|
|
|
return `redirect:${id}`;
|
2022-10-19 19:10:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 19:13:02 +00:00
|
|
|
constructor(public parameters: RedirectParameters) {}
|
2022-10-19 19:10:41 +00:00
|
|
|
|
2022-10-27 19:13:02 +00:00
|
|
|
public idString(): string {
|
|
|
|
return Redirect.idString(this.parameters.id);
|
2022-10-19 19:10:41 +00:00
|
|
|
}
|
2022-10-12 22:04:51 +00:00
|
|
|
|
|
|
|
public isMatch(url: URL): boolean {
|
2022-10-18 19:43:23 +00:00
|
|
|
if (this.parameters.matcherType === 'hostname') {
|
2022-10-18 20:23:41 +00:00
|
|
|
const hostname = url.hostname.startsWith('www.')
|
|
|
|
? url.hostname.slice(4)
|
|
|
|
: url.hostname;
|
2022-10-20 14:22:57 +00:00
|
|
|
return hostname === this.parameters.matcherValue;
|
2022-10-12 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:54:02 +00:00
|
|
|
if (this.parameters.matcherType === 'regex') {
|
|
|
|
const regex = new RegExp(this.parameters.matcherValue, 'gi');
|
|
|
|
return regex.test(url.href);
|
|
|
|
}
|
|
|
|
|
2022-10-12 22:04:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-05 17:08:17 +00:00
|
|
|
|
|
|
|
public abstract redirect(url: URL | string): URL;
|
|
|
|
}
|