Add a regex redirect.
This commit is contained in:
parent
459380cb69
commit
2665ee3298
|
@ -1,5 +1,5 @@
|
||||||
export const matcherTypes = ['hostname', 'regex'] as const;
|
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 MatcherType = typeof matcherTypes[number];
|
||||||
export type RedirectType = typeof redirectTypes[number];
|
export type RedirectType = typeof redirectTypes[number];
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import {RedirectParameters} from './base.js';
|
import {RedirectParameters} from './base.js';
|
||||||
import {HostnameRedirect} from './hostname.js';
|
import {HostnameRedirect} from './hostname.js';
|
||||||
|
import {RegexRedirect} from './regex.js';
|
||||||
import {SimpleRedirect} from './simple.js';
|
import {SimpleRedirect} from './simple.js';
|
||||||
|
|
||||||
export * from './base.js';
|
export * from './base.js';
|
||||||
export * from './hostname.js';
|
export * from './hostname.js';
|
||||||
|
export * from './regex.js';
|
||||||
export * from './simple.js';
|
export * from './simple.js';
|
||||||
|
|
||||||
export type Redirects = HostnameRedirect | SimpleRedirect;
|
export type Redirects = HostnameRedirect | SimpleRedirect;
|
||||||
|
@ -11,6 +13,7 @@ export type Redirects = HostnameRedirect | SimpleRedirect;
|
||||||
export function parseRedirect(
|
export function parseRedirect(
|
||||||
parameters: RedirectParameters,
|
parameters: RedirectParameters,
|
||||||
): Redirects | undefined {
|
): Redirects | undefined {
|
||||||
|
const matcherType = parameters?.matcherType;
|
||||||
const redirectType = parameters?.redirectType;
|
const redirectType = parameters?.redirectType;
|
||||||
|
|
||||||
if (redirectType === 'hostname') {
|
if (redirectType === 'hostname') {
|
||||||
|
@ -20,4 +23,8 @@ export function parseRedirect(
|
||||||
if (redirectType === 'simple') {
|
if (redirectType === 'simple') {
|
||||||
return new SimpleRedirect(parameters);
|
return new SimpleRedirect(parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (matcherType === 'regex' && redirectType === 'regex') {
|
||||||
|
return new RegexRedirect(parameters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ import {
|
||||||
Redirect,
|
Redirect,
|
||||||
Redirects,
|
Redirects,
|
||||||
RedirectParameters,
|
RedirectParameters,
|
||||||
|
RegexRedirect,
|
||||||
SimpleRedirect,
|
SimpleRedirect,
|
||||||
} from '../source/redirect/exports.js';
|
} from '../source/redirect/exports.js';
|
||||||
|
|
||||||
|
@ -33,6 +34,15 @@ const simpleParameters: RedirectParameters = {
|
||||||
redirectValue: 'https://example.org/simple',
|
redirectValue: 'https://example.org/simple',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const regexParameters: RedirectParameters = {
|
||||||
|
enabled: true,
|
||||||
|
id: 3,
|
||||||
|
matcherType: 'regex',
|
||||||
|
matcherValue: '(.+)\\.com',
|
||||||
|
redirectType: 'regex',
|
||||||
|
redirectValue: '$1.org',
|
||||||
|
};
|
||||||
|
|
||||||
test('parseRedirect', (t) => {
|
test('parseRedirect', (t) => {
|
||||||
const samples: RedirectParameters[] = [
|
const samples: RedirectParameters[] = [
|
||||||
{
|
{
|
||||||
|
@ -41,6 +51,7 @@ test('parseRedirect', (t) => {
|
||||||
undefined as unknown as RedirectParameters,
|
undefined as unknown as RedirectParameters,
|
||||||
hostnameParameters,
|
hostnameParameters,
|
||||||
simpleParameters,
|
simpleParameters,
|
||||||
|
regexParameters,
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const sample of samples) {
|
for (const sample of samples) {
|
||||||
|
@ -58,18 +69,21 @@ test('parseRedirect', (t) => {
|
||||||
test('Redirect.redirect', (t) => {
|
test('Redirect.redirect', (t) => {
|
||||||
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
|
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
|
||||||
const simpleRedirect = new SimpleRedirect(simpleParameters);
|
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', hostnameRedirect],
|
||||||
['https://example.com/path#hash?query=test', hostnameRedirect],
|
['https://example.com/path#hash?query=test', hostnameRedirect],
|
||||||
['https://example.com', simpleRedirect],
|
['https://example.com', simpleRedirect],
|
||||||
['https://example.com/path', simpleRedirect],
|
['https://example.com/path', simpleRedirect],
|
||||||
|
['https://example.com', regexRedirect],
|
||||||
|
[new URL('https://example.com'), regexRedirect],
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const [index, [url, redirect]] of samples.entries()) {
|
for (const [index, [url, redirect]] of samples.entries()) {
|
||||||
t.snapshot(
|
t.snapshot(
|
||||||
{
|
{
|
||||||
original: url,
|
original: url instanceof URL ? url.href : url,
|
||||||
redirected: redirect.redirect(url).href,
|
redirected: redirect.redirect(url).href,
|
||||||
},
|
},
|
||||||
`${index} ${redirect.constructor.name}`,
|
`${index} ${redirect.constructor.name}`,
|
||||||
|
|
|
@ -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
|
## Redirect.redirect
|
||||||
|
|
||||||
> 0 HostnameRedirect
|
> 0 HostnameRedirect
|
||||||
|
@ -61,3 +74,17 @@ Generated by [AVA](https://avajs.dev).
|
||||||
original: 'https://example.com/path',
|
original: 'https://example.com/path',
|
||||||
redirected: 'https://example.org/simple',
|
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/',
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue