Compare commits

..

No commits in common. "aa9a7121f8f3eb5d34bf90dbb7150de7229cb55f" and "8e96a55661060be6e98361be3d09bad600b61b43" have entirely different histories.

5 changed files with 13 additions and 55 deletions

View File

@ -2,21 +2,8 @@ import {HostnameParameters} from './hostname.js';
export type RedirectParameters = HostnameParameters; export type RedirectParameters = HostnameParameters;
export type Matcher = {
matchType: 'hostname';
toMatch: string;
};
export abstract class Redirect<P extends RedirectParameters> { export abstract class Redirect<P extends RedirectParameters> {
constructor(public parameters: P & Matcher) {} constructor(public parameters: P) {}
public isMatch(url: URL): boolean {
if (this.parameters.matchType === 'hostname') {
return url.hostname === this.parameters.toMatch;
}
return false;
}
public abstract redirect(url: URL | string): URL; public abstract redirect(url: URL | string): URL;
} }

View File

@ -1,3 +1,4 @@
import {RedirectParameters} from './base.js';
import {HostnameRedirect} from './hostname.js'; import {HostnameRedirect} from './hostname.js';
export * from './base.js'; export * from './base.js';
@ -5,7 +6,7 @@ export * from './hostname.js';
export type Redirects = HostnameRedirect; export type Redirects = HostnameRedirect;
export function parseRedirect<P extends Redirects['parameters']>( export function parseRedirect<P extends RedirectParameters>(
parameters: P, parameters: P,
): Redirects | undefined { ): Redirects | undefined {
if (parameters.type === 'hostname') { if (parameters.type === 'hostname') {

View File

@ -6,23 +6,18 @@ import {
parseRedirect, parseRedirect,
HostnameRedirect, HostnameRedirect,
Redirect, Redirect,
Redirects,
RedirectParameters, RedirectParameters,
} from '../source/redirect/exports.js'; } from '../source/redirect/exports.js';
const hostnameParameters: HostnameRedirect['parameters'] = {
hostname: 'example.org',
matchType: 'hostname',
toMatch: 'example.com',
type: 'hostname',
};
test('parseRedirect', (t) => { test('parseRedirect', (t) => {
const samples: Array<Redirects['parameters']> = [ const samples: RedirectParameters[] = [
{ {
test: 'Invalid parameters', test: 'Invalid parameters',
} as unknown as Redirects['parameters'], } as unknown as RedirectParameters,
hostnameParameters, {
hostname: 'example.org',
type: 'hostname',
},
]; ];
for (const sample of samples) { for (const sample of samples) {
@ -37,7 +32,10 @@ test('parseRedirect', (t) => {
}); });
test('Redirect.redirect', (t) => { test('Redirect.redirect', (t) => {
const hostnameRedirect = new HostnameRedirect(hostnameParameters); const hostnameRedirect = new HostnameRedirect({
hostname: 'example.org',
type: 'hostname',
});
const samples: Array<[string, Redirect<RedirectParameters>]> = [ const samples: Array<[string, Redirect<RedirectParameters>]> = [
['https://example.com', hostnameRedirect], ['https://example.com', hostnameRedirect],
@ -54,29 +52,3 @@ test('Redirect.redirect', (t) => {
); );
} }
}); });
test('Redirect.isMatch', (t) => {
type UrlSamples = Array<[string, boolean]>;
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
const hostnameSamples: UrlSamples = [
['https://example.com', true],
['https://www.example.com', false],
['https://example.org', false],
];
const invalidRedirect = new HostnameRedirect({
test: 'invalid',
} as unknown as Redirects['parameters']);
const samples: Array<[Redirects, UrlSamples]> = [
[invalidRedirect, [['https://example.org', false]]],
[hostnameRedirect, hostnameSamples],
];
for (const [redirect, urlSamples] of samples) {
for (const [sample, expected] of urlSamples) {
t.is(redirect.isMatch(new URL(sample)), expected);
}
}
});

View File

@ -11,8 +11,6 @@ Generated by [AVA](https://avajs.dev).
HostnameRedirect { HostnameRedirect {
parameters: { parameters: {
hostname: 'example.org', hostname: 'example.org',
matchType: 'hostname',
toMatch: 'example.com',
type: 'hostname', type: 'hostname',
}, },
} }