Add a regex redirect.

This commit is contained in:
Bauke 2022-11-02 11:10:29 +01:00
parent 459380cb69
commit 2665ee3298
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
6 changed files with 60 additions and 3 deletions

View File

@ -1,5 +1,5 @@
export const matcherTypes = ['hostname', 'regex'] as const;
export const redirectTypes = ['hostname', 'simple'] as const;
export const redirectTypes = ['hostname', 'regex', 'simple'] as const;
export type MatcherType = typeof matcherTypes[number];
export type RedirectType = typeof redirectTypes[number];

View File

@ -1,9 +1,11 @@
import {RedirectParameters} from './base.js';
import {HostnameRedirect} from './hostname.js';
import {RegexRedirect} from './regex.js';
import {SimpleRedirect} from './simple.js';
export * from './base.js';
export * from './hostname.js';
export * from './regex.js';
export * from './simple.js';
export type Redirects = HostnameRedirect | SimpleRedirect;
@ -11,6 +13,7 @@ export type Redirects = HostnameRedirect | SimpleRedirect;
export function parseRedirect(
parameters: RedirectParameters,
): Redirects | undefined {
const matcherType = parameters?.matcherType;
const redirectType = parameters?.redirectType;
if (redirectType === 'hostname') {
@ -20,4 +23,8 @@ export function parseRedirect(
if (redirectType === 'simple') {
return new SimpleRedirect(parameters);
}
if (matcherType === 'regex' && redirectType === 'regex') {
return new RegexRedirect(parameters);
}
}

9
source/redirect/regex.ts Normal file
View File

@ -0,0 +1,9 @@
import {Redirect} from './base.js';
export class RegexRedirect extends Redirect {
public redirect(redirect: URL | string): URL {
const url = redirect instanceof URL ? redirect.href : redirect;
const regex = new RegExp(this.parameters.matcherValue, 'gi');
return new URL(url.replace(regex, this.parameters.redirectValue));
}
}

View File

@ -12,6 +12,7 @@ import {
Redirect,
Redirects,
RedirectParameters,
RegexRedirect,
SimpleRedirect,
} from '../source/redirect/exports.js';
@ -33,6 +34,15 @@ const simpleParameters: RedirectParameters = {
redirectValue: 'https://example.org/simple',
};
const regexParameters: RedirectParameters = {
enabled: true,
id: 3,
matcherType: 'regex',
matcherValue: '(.+)\\.com',
redirectType: 'regex',
redirectValue: '$1.org',
};
test('parseRedirect', (t) => {
const samples: RedirectParameters[] = [
{
@ -41,6 +51,7 @@ test('parseRedirect', (t) => {
undefined as unknown as RedirectParameters,
hostnameParameters,
simpleParameters,
regexParameters,
];
for (const sample of samples) {
@ -58,18 +69,21 @@ test('parseRedirect', (t) => {
test('Redirect.redirect', (t) => {
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
const simpleRedirect = new SimpleRedirect(simpleParameters);
const regexRedirect = new RegexRedirect(regexParameters);
const samples: Array<[string, Redirect]> = [
const samples: Array<[string | URL, Redirect]> = [
['https://example.com', hostnameRedirect],
['https://example.com/path#hash?query=test', hostnameRedirect],
['https://example.com', simpleRedirect],
['https://example.com/path', simpleRedirect],
['https://example.com', regexRedirect],
[new URL('https://example.com'), regexRedirect],
];
for (const [index, [url, redirect]] of samples.entries()) {
t.snapshot(
{
original: url,
original: url instanceof URL ? url.href : url,
redirected: redirect.redirect(url).href,
},
`${index} ${redirect.constructor.name}`,

View File

@ -32,6 +32,19 @@ Generated by [AVA](https://avajs.dev).
},
}
> Class RegexRedirect
RegexRedirect {
parameters: {
enabled: true,
id: 3,
matcherType: 'regex',
matcherValue: '(.+)\\.com',
redirectType: 'regex',
redirectValue: '$1.org',
},
}
## Redirect.redirect
> 0 HostnameRedirect
@ -61,3 +74,17 @@ Generated by [AVA](https://avajs.dev).
original: 'https://example.com/path',
redirected: 'https://example.org/simple',
}
> 4 RegexRedirect
{
original: 'https://example.com',
redirected: 'https://example.org/',
}
> 5 RegexRedirect
{
original: 'https://example.com/',
redirected: 'https://example.org/',
}