Compare commits
No commits in common. "e2d233f8d63bfbbae40a8026c0394c57e7dda0a9" and "263c0497f30b9014f54ac6a04dca8e5bba77844c" have entirely different histories.
e2d233f8d6
...
263c0497f3
|
@ -1,31 +1,17 @@
|
||||||
export const matcherTypes = ['hostname'] as const;
|
import {HostnameParameters} from './hostname.js';
|
||||||
export const redirectTypes = ['hostname', 'simple'] as const;
|
|
||||||
|
|
||||||
export type MatcherType = typeof matcherTypes[number];
|
export type RedirectParameters = HostnameParameters;
|
||||||
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 = {
|
||||||
matcherType: MatcherType;
|
matchType: 'hostname';
|
||||||
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.matcherType === 'hostname') {
|
if (this.parameters.matchType === 'hostname') {
|
||||||
return url.hostname === this.parameters.toMatch;
|
return url.hostname === this.parameters.toMatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,8 +19,4 @@ 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,22 +1,14 @@
|
||||||
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 | SimpleRedirect;
|
export type Redirects = HostnameRedirect;
|
||||||
|
|
||||||
export function parseRedirect<P extends Redirects['parameters']>(
|
export function parseRedirect<P extends Redirects['parameters']>(
|
||||||
parameters: P,
|
parameters: P,
|
||||||
): Redirects | undefined {
|
): Redirects | undefined {
|
||||||
const type = parameters?.type;
|
if (parameters?.type === 'hostname') {
|
||||||
|
|
||||||
if (type === 'hostname') {
|
|
||||||
return new HostnameRedirect(parameters);
|
return new HostnameRedirect(parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'simple') {
|
|
||||||
return new SimpleRedirect(parameters);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,12 +11,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
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,32 +3,20 @@
|
||||||
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',
|
||||||
matcherType: 'hostname',
|
matchType: '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']> = [
|
||||||
{
|
{
|
||||||
|
@ -36,7 +24,6 @@ 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) {
|
||||||
|
@ -52,13 +39,10 @@ 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()) {
|
||||||
|
@ -84,7 +68,7 @@ test('Redirect.isMatch', (t) => {
|
||||||
|
|
||||||
const invalidRedirect = new HostnameRedirect({
|
const invalidRedirect = new HostnameRedirect({
|
||||||
test: 'invalid',
|
test: 'invalid',
|
||||||
} as unknown as HostnameRedirect['parameters']);
|
} as unknown as Redirects['parameters']);
|
||||||
|
|
||||||
const samples: Array<[Redirects, UrlSamples]> = [
|
const samples: Array<[Redirects, UrlSamples]> = [
|
||||||
[invalidRedirect, [['https://example.org', false]]],
|
[invalidRedirect, [['https://example.org', false]]],
|
||||||
|
@ -97,10 +81,3 @@ 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,23 +11,12 @@ Generated by [AVA](https://avajs.dev).
|
||||||
HostnameRedirect {
|
HostnameRedirect {
|
||||||
parameters: {
|
parameters: {
|
||||||
hostname: 'example.org',
|
hostname: 'example.org',
|
||||||
matcherType: 'hostname',
|
matchType: '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
|
||||||
|
@ -43,17 +32,3 @@ 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