Compare commits
8 Commits
263c0497f3
...
e2d233f8d6
Author | SHA1 | Date |
---|---|---|
Bauke | e2d233f8d6 | |
Bauke | a4b12334c5 | |
Bauke | 81669cc535 | |
Bauke | e260419f87 | |
Bauke | 67a2c7567f | |
Bauke | 1743959769 | |
Bauke | b54d6ab0c2 | |
Bauke | 1c7d4ca34b |
|
@ -1,17 +1,31 @@
|
||||||
import {HostnameParameters} from './hostname.js';
|
export const matcherTypes = ['hostname'] as const;
|
||||||
|
export const redirectTypes = ['hostname', 'simple'] as const;
|
||||||
|
|
||||||
export type RedirectParameters = HostnameParameters;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,4 +33,8 @@ export abstract class Redirect<P extends RedirectParameters> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract redirect(url: URL | string): URL;
|
public abstract redirect(url: URL | string): URL;
|
||||||
|
|
||||||
|
public abstract get redirectValue(): string;
|
||||||
|
|
||||||
|
public abstract set redirectValue(value: string);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,22 @@
|
||||||
import {HostnameRedirect} from './hostname.js';
|
import {HostnameRedirect} from './hostname.js';
|
||||||
|
import {SimpleRedirect} from './simple.js';
|
||||||
|
|
||||||
export * from './base.js';
|
export * from './base.js';
|
||||||
export * from './hostname.js';
|
export * from './hostname.js';
|
||||||
|
export * from './simple.js';
|
||||||
|
|
||||||
export type Redirects = HostnameRedirect;
|
export type Redirects = HostnameRedirect | SimpleRedirect;
|
||||||
|
|
||||||
export function parseRedirect<P extends Redirects['parameters']>(
|
export function parseRedirect<P extends Redirects['parameters']>(
|
||||||
parameters: P,
|
parameters: P,
|
||||||
): Redirects | undefined {
|
): Redirects | undefined {
|
||||||
if (parameters?.type === 'hostname') {
|
const type = parameters?.type;
|
||||||
|
|
||||||
|
if (type === 'hostname') {
|
||||||
return new HostnameRedirect(parameters);
|
return new HostnameRedirect(parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type === 'simple') {
|
||||||
|
return new SimpleRedirect(parameters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,4 +11,12 @@ export class HostnameRedirect extends Redirect<HostnameParameters> {
|
||||||
redirected.hostname = this.parameters.hostname;
|
redirected.hostname = this.parameters.hostname;
|
||||||
return redirected;
|
return redirected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get redirectValue(): string {
|
||||||
|
return this.parameters.hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public set redirectValue(value: string) {
|
||||||
|
this.parameters.hostname = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
import {Redirect} from './base.js';
|
||||||
|
|
||||||
|
export type SimpleParameters = {
|
||||||
|
target: string;
|
||||||
|
type: 'simple';
|
||||||
|
};
|
||||||
|
|
||||||
|
export class SimpleRedirect extends Redirect<SimpleParameters> {
|
||||||
|
public redirect(): URL {
|
||||||
|
return new URL(this.parameters.target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public get redirectValue(): string {
|
||||||
|
return this.parameters.target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public set redirectValue(value: string) {
|
||||||
|
this.parameters.target = value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,20 +3,32 @@
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
matcherTypes,
|
||||||
|
narrowMatchType,
|
||||||
|
narrowRedirectType,
|
||||||
parseRedirect,
|
parseRedirect,
|
||||||
|
redirectTypes,
|
||||||
HostnameRedirect,
|
HostnameRedirect,
|
||||||
Redirect,
|
Redirect,
|
||||||
Redirects,
|
Redirects,
|
||||||
RedirectParameters,
|
RedirectParameters,
|
||||||
|
SimpleRedirect,
|
||||||
} from '../source/redirect/exports.js';
|
} from '../source/redirect/exports.js';
|
||||||
|
|
||||||
const hostnameParameters: HostnameRedirect['parameters'] = {
|
const hostnameParameters: HostnameRedirect['parameters'] = {
|
||||||
hostname: 'example.org',
|
hostname: 'example.org',
|
||||||
matchType: 'hostname',
|
matcherType: 'hostname',
|
||||||
toMatch: 'example.com',
|
toMatch: 'example.com',
|
||||||
type: 'hostname',
|
type: 'hostname',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const simpleParameters: SimpleRedirect['parameters'] = {
|
||||||
|
matcherType: 'hostname',
|
||||||
|
target: 'https://example.org/simple',
|
||||||
|
toMatch: 'example.com',
|
||||||
|
type: 'simple',
|
||||||
|
};
|
||||||
|
|
||||||
test('parseRedirect', (t) => {
|
test('parseRedirect', (t) => {
|
||||||
const samples: Array<Redirects['parameters']> = [
|
const samples: Array<Redirects['parameters']> = [
|
||||||
{
|
{
|
||||||
|
@ -24,6 +36,7 @@ test('parseRedirect', (t) => {
|
||||||
} as unknown as Redirects['parameters'],
|
} as unknown as Redirects['parameters'],
|
||||||
undefined as unknown as Redirects['parameters'],
|
undefined as unknown as Redirects['parameters'],
|
||||||
hostnameParameters,
|
hostnameParameters,
|
||||||
|
simpleParameters,
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const sample of samples) {
|
for (const sample of samples) {
|
||||||
|
@ -39,10 +52,13 @@ 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 samples: Array<[string, Redirect<RedirectParameters>]> = [
|
const samples: Array<[string, Redirect<RedirectParameters>]> = [
|
||||||
['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/path', simpleRedirect],
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const [index, [url, redirect]] of samples.entries()) {
|
for (const [index, [url, redirect]] of samples.entries()) {
|
||||||
|
@ -68,7 +84,7 @@ test('Redirect.isMatch', (t) => {
|
||||||
|
|
||||||
const invalidRedirect = new HostnameRedirect({
|
const invalidRedirect = new HostnameRedirect({
|
||||||
test: 'invalid',
|
test: 'invalid',
|
||||||
} as unknown as Redirects['parameters']);
|
} as unknown as HostnameRedirect['parameters']);
|
||||||
|
|
||||||
const samples: Array<[Redirects, UrlSamples]> = [
|
const samples: Array<[Redirects, UrlSamples]> = [
|
||||||
[invalidRedirect, [['https://example.org', false]]],
|
[invalidRedirect, [['https://example.org', false]]],
|
||||||
|
@ -81,3 +97,10 @@ test('Redirect.isMatch', (t) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Narrow match & redirect types', (t) => {
|
||||||
|
t.false(narrowMatchType('invalid'));
|
||||||
|
t.false(narrowRedirectType('invalid'));
|
||||||
|
t.true(matcherTypes.every((type) => narrowMatchType(type)));
|
||||||
|
t.true(redirectTypes.every((type) => narrowRedirectType(type)));
|
||||||
|
});
|
||||||
|
|
|
@ -11,12 +11,23 @@ Generated by [AVA](https://avajs.dev).
|
||||||
HostnameRedirect {
|
HostnameRedirect {
|
||||||
parameters: {
|
parameters: {
|
||||||
hostname: 'example.org',
|
hostname: 'example.org',
|
||||||
matchType: 'hostname',
|
matcherType: 'hostname',
|
||||||
toMatch: 'example.com',
|
toMatch: 'example.com',
|
||||||
type: 'hostname',
|
type: 'hostname',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
> Class SimpleRedirect
|
||||||
|
|
||||||
|
SimpleRedirect {
|
||||||
|
parameters: {
|
||||||
|
matcherType: 'hostname',
|
||||||
|
target: 'https://example.org/simple',
|
||||||
|
toMatch: 'example.com',
|
||||||
|
type: 'simple',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
## Redirect.redirect
|
## Redirect.redirect
|
||||||
|
|
||||||
> 0 HostnameRedirect
|
> 0 HostnameRedirect
|
||||||
|
@ -32,3 +43,17 @@ Generated by [AVA](https://avajs.dev).
|
||||||
original: 'https://example.com/path#hash?query=test',
|
original: 'https://example.com/path#hash?query=test',
|
||||||
redirected: 'https://example.org/path#hash?query=test',
|
redirected: 'https://example.org/path#hash?query=test',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
> 2 SimpleRedirect
|
||||||
|
|
||||||
|
{
|
||||||
|
original: 'https://example.com',
|
||||||
|
redirected: 'https://example.org/simple',
|
||||||
|
}
|
||||||
|
|
||||||
|
> 3 SimpleRedirect
|
||||||
|
|
||||||
|
{
|
||||||
|
original: 'https://example.com/path',
|
||||||
|
redirected: 'https://example.org/simple',
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue