Compare commits
No commits in common. "aa9a7121f8f3eb5d34bf90dbb7150de7229cb55f" and "8e96a55661060be6e98361be3d09bad600b61b43" have entirely different histories.
aa9a7121f8
...
8e96a55661
|
@ -2,21 +2,8 @@ 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 & Matcher) {}
|
||||
|
||||
public isMatch(url: URL): boolean {
|
||||
if (this.parameters.matchType === 'hostname') {
|
||||
return url.hostname === this.parameters.toMatch;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
constructor(public parameters: P) {}
|
||||
|
||||
public abstract redirect(url: URL | string): URL;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import {RedirectParameters} from './base.js';
|
||||
import {HostnameRedirect} from './hostname.js';
|
||||
|
||||
export * from './base.js';
|
||||
|
@ -5,7 +6,7 @@ export * from './hostname.js';
|
|||
|
||||
export type Redirects = HostnameRedirect;
|
||||
|
||||
export function parseRedirect<P extends Redirects['parameters']>(
|
||||
export function parseRedirect<P extends RedirectParameters>(
|
||||
parameters: P,
|
||||
): Redirects | undefined {
|
||||
if (parameters.type === 'hostname') {
|
||||
|
|
|
@ -6,23 +6,18 @@ 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: Array<Redirects['parameters']> = [
|
||||
const samples: RedirectParameters[] = [
|
||||
{
|
||||
test: 'Invalid parameters',
|
||||
} as unknown as Redirects['parameters'],
|
||||
hostnameParameters,
|
||||
} as unknown as RedirectParameters,
|
||||
{
|
||||
hostname: 'example.org',
|
||||
type: 'hostname',
|
||||
},
|
||||
];
|
||||
|
||||
for (const sample of samples) {
|
||||
|
@ -37,7 +32,10 @@ test('parseRedirect', (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>]> = [
|
||||
['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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -11,8 +11,6 @@ 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