Compare commits
4 Commits
8e96a55661
...
aa9a7121f8
Author | SHA1 | Date |
---|---|---|
Bauke | aa9a7121f8 | |
Bauke | 814c718c41 | |
Bauke | b9a56fe881 | |
Bauke | 5888dfdc97 |
|
@ -2,8 +2,21 @@ import {HostnameParameters} from './hostname.js';
|
|||
|
||||
export type RedirectParameters = HostnameParameters;
|
||||
|
||||
export type Matcher = {
|
||||
matchType: 'hostname';
|
||||
toMatch: string;
|
||||
};
|
||||
|
||||
export abstract class Redirect<P extends RedirectParameters> {
|
||||
constructor(public parameters: P) {}
|
||||
constructor(public parameters: P & Matcher) {}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import {RedirectParameters} from './base.js';
|
||||
import {HostnameRedirect} from './hostname.js';
|
||||
|
||||
export * from './base.js';
|
||||
|
@ -6,7 +5,7 @@ export * from './hostname.js';
|
|||
|
||||
export type Redirects = HostnameRedirect;
|
||||
|
||||
export function parseRedirect<P extends RedirectParameters>(
|
||||
export function parseRedirect<P extends Redirects['parameters']>(
|
||||
parameters: P,
|
||||
): Redirects | undefined {
|
||||
if (parameters.type === 'hostname') {
|
||||
|
|
|
@ -6,18 +6,23 @@ import {
|
|||
parseRedirect,
|
||||
HostnameRedirect,
|
||||
Redirect,
|
||||
Redirects,
|
||||
RedirectParameters,
|
||||
} from '../source/redirect/exports.js';
|
||||
|
||||
const hostnameParameters: HostnameRedirect['parameters'] = {
|
||||
hostname: 'example.org',
|
||||
matchType: 'hostname',
|
||||
toMatch: 'example.com',
|
||||
type: 'hostname',
|
||||
};
|
||||
|
||||
test('parseRedirect', (t) => {
|
||||
const samples: RedirectParameters[] = [
|
||||
const samples: Array<Redirects['parameters']> = [
|
||||
{
|
||||
test: 'Invalid parameters',
|
||||
} as unknown as RedirectParameters,
|
||||
{
|
||||
hostname: 'example.org',
|
||||
type: 'hostname',
|
||||
},
|
||||
} as unknown as Redirects['parameters'],
|
||||
hostnameParameters,
|
||||
];
|
||||
|
||||
for (const sample of samples) {
|
||||
|
@ -32,10 +37,7 @@ test('parseRedirect', (t) => {
|
|||
});
|
||||
|
||||
test('Redirect.redirect', (t) => {
|
||||
const hostnameRedirect = new HostnameRedirect({
|
||||
hostname: 'example.org',
|
||||
type: 'hostname',
|
||||
});
|
||||
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
|
||||
|
||||
const samples: Array<[string, Redirect<RedirectParameters>]> = [
|
||||
['https://example.com', hostnameRedirect],
|
||||
|
@ -52,3 +54,29 @@ 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -11,6 +11,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
HostnameRedirect {
|
||||
parameters: {
|
||||
hostname: 'example.org',
|
||||
matchType: 'hostname',
|
||||
toMatch: 'example.com',
|
||||
type: 'hostname',
|
||||
},
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue