Compare commits

..

8 Commits

7 changed files with 111 additions and 9 deletions

View File

@ -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 = {
matchType: 'hostname';
matcherType: MatcherType;
toMatch: string;
};
export type RedirectParameters = {
type: RedirectType;
};
export abstract class Redirect<P extends RedirectParameters> {
constructor(public parameters: P & Matcher) {}
public isMatch(url: URL): boolean {
if (this.parameters.matchType === 'hostname') {
if (this.parameters.matcherType === 'hostname') {
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 get redirectValue(): string;
public abstract set redirectValue(value: string);
}

View File

@ -1,14 +1,22 @@
import {HostnameRedirect} from './hostname.js';
import {SimpleRedirect} from './simple.js';
export * from './base.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']>(
parameters: P,
): Redirects | undefined {
if (parameters?.type === 'hostname') {
const type = parameters?.type;
if (type === 'hostname') {
return new HostnameRedirect(parameters);
}
if (type === 'simple') {
return new SimpleRedirect(parameters);
}
}

View File

@ -11,4 +11,12 @@ export class HostnameRedirect extends Redirect<HostnameParameters> {
redirected.hostname = this.parameters.hostname;
return redirected;
}
public get redirectValue(): string {
return this.parameters.hostname;
}
public set redirectValue(value: string) {
this.parameters.hostname = value;
}
}

20
source/redirect/simple.ts Normal file
View File

@ -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;
}
}

View File

@ -3,20 +3,32 @@
import test from 'ava';
import {
matcherTypes,
narrowMatchType,
narrowRedirectType,
parseRedirect,
redirectTypes,
HostnameRedirect,
Redirect,
Redirects,
RedirectParameters,
SimpleRedirect,
} from '../source/redirect/exports.js';
const hostnameParameters: HostnameRedirect['parameters'] = {
hostname: 'example.org',
matchType: 'hostname',
matcherType: 'hostname',
toMatch: 'example.com',
type: 'hostname',
};
const simpleParameters: SimpleRedirect['parameters'] = {
matcherType: 'hostname',
target: 'https://example.org/simple',
toMatch: 'example.com',
type: 'simple',
};
test('parseRedirect', (t) => {
const samples: Array<Redirects['parameters']> = [
{
@ -24,6 +36,7 @@ test('parseRedirect', (t) => {
} as unknown as Redirects['parameters'],
undefined as unknown as Redirects['parameters'],
hostnameParameters,
simpleParameters,
];
for (const sample of samples) {
@ -39,10 +52,13 @@ test('parseRedirect', (t) => {
test('Redirect.redirect', (t) => {
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
const simpleRedirect = new SimpleRedirect(simpleParameters);
const samples: Array<[string, Redirect<RedirectParameters>]> = [
['https://example.com', 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()) {
@ -68,7 +84,7 @@ test('Redirect.isMatch', (t) => {
const invalidRedirect = new HostnameRedirect({
test: 'invalid',
} as unknown as Redirects['parameters']);
} as unknown as HostnameRedirect['parameters']);
const samples: Array<[Redirects, UrlSamples]> = [
[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)));
});

View File

@ -11,12 +11,23 @@ Generated by [AVA](https://avajs.dev).
HostnameRedirect {
parameters: {
hostname: 'example.org',
matchType: 'hostname',
matcherType: 'hostname',
toMatch: 'example.com',
type: 'hostname',
},
}
> Class SimpleRedirect
SimpleRedirect {
parameters: {
matcherType: 'hostname',
target: 'https://example.org/simple',
toMatch: 'example.com',
type: 'simple',
},
}
## Redirect.redirect
> 0 HostnameRedirect
@ -32,3 +43,17 @@ Generated by [AVA](https://avajs.dev).
original: 'https://example.com/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',
}