Compare commits

..

No commits in common. "f7d292932f0ecd06bf23d7f6811f5a910060df9a" and "d25a844facff9d9973c49e14db542b769b3ca1dd" have entirely different histories.

7 changed files with 74 additions and 34 deletions

View File

@ -1,6 +1,6 @@
import {customAlphabet} from 'nanoid';
export const matcherTypes = ['hostname', 'regex'] as const;
export const matcherTypes = ['hostname'] as const;
export const redirectTypes = ['hostname', 'simple'] as const;
export type MatcherType = typeof matcherTypes[number];
@ -14,14 +14,16 @@ export function narrowRedirectType(value: string): value is RedirectType {
return redirectTypes.includes(value as RedirectType);
}
export type RedirectParameters = {
export type Matcher = {
matcherType: MatcherType;
matcherValue: string;
redirectType: RedirectType;
redirectValue: string;
toMatch: string;
};
export abstract class Redirect {
export type RedirectParameters = {
redirectType: RedirectType;
};
export abstract class Redirect<P extends RedirectParameters> {
public static generateId(): string {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const nanoid = customAlphabet(`${alphabet}${alphabet.toUpperCase()}`, 20);
@ -30,7 +32,7 @@ export abstract class Redirect {
public id: string;
constructor(public parameters: RedirectParameters, id?: string) {
constructor(public parameters: P & Matcher, id?: string) {
this.id = id ?? Redirect.generateId();
}
@ -39,16 +41,15 @@ export abstract class Redirect {
const hostname = url.hostname.startsWith('www.')
? url.hostname.slice(4)
: url.hostname;
return hostname === this.parameters.matcherValue;
}
if (this.parameters.matcherType === 'regex') {
const regex = new RegExp(this.parameters.matcherValue, 'gi');
return regex.test(url.href);
return hostname === this.parameters.toMatch;
}
return false;
}
public abstract redirect(url: URL | string): URL;
public abstract get redirectValue(): string;
public abstract set redirectValue(value: string);
}

View File

@ -1,4 +1,3 @@
import {RedirectParameters} from './base.js';
import {HostnameRedirect} from './hostname.js';
import {SimpleRedirect} from './simple.js';
@ -8,8 +7,8 @@ export * from './simple.js';
export type Redirects = HostnameRedirect | SimpleRedirect;
export function parseRedirect(
parameters: RedirectParameters,
export function parseRedirect<P extends Redirects['parameters']>(
parameters: P,
id: string,
): Redirects | undefined {
const redirectType = parameters?.redirectType;

View File

@ -1,9 +1,22 @@
import {Redirect} from './base.js';
export class HostnameRedirect extends Redirect {
export type HostnameParameters = {
hostname: string;
redirectType: 'hostname';
};
export class HostnameRedirect extends Redirect<HostnameParameters> {
public redirect(url: URL | string): URL {
const redirected = new URL(url);
redirected.hostname = this.parameters.redirectValue;
redirected.hostname = this.parameters.hostname;
return redirected;
}
public get redirectValue(): string {
return this.parameters.hostname;
}
public set redirectValue(value: string) {
this.parameters.hostname = value;
}
}

View File

@ -1,7 +1,20 @@
import {Redirect} from './base.js';
export class SimpleRedirect extends Redirect {
export type SimpleParameters = {
target: string;
redirectType: 'simple';
};
export class SimpleRedirect extends Redirect<SimpleParameters> {
public redirect(): URL {
return new URL(this.parameters.redirectValue);
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

@ -15,26 +15,26 @@ import {
SimpleRedirect,
} from '../source/redirect/exports.js';
const hostnameParameters: RedirectParameters = {
const hostnameParameters: HostnameRedirect['parameters'] = {
hostname: 'example.org',
matcherType: 'hostname',
matcherValue: 'example.com',
toMatch: 'example.com',
redirectType: 'hostname',
redirectValue: 'example.org',
};
const simpleParameters: RedirectParameters = {
const simpleParameters: SimpleRedirect['parameters'] = {
matcherType: 'hostname',
matcherValue: 'example.com',
target: 'https://example.org/simple',
toMatch: 'example.com',
redirectType: 'simple',
redirectValue: 'https://example.org/simple',
};
test('parseRedirect', (t) => {
const samples: RedirectParameters[] = [
const samples: Array<Redirects['parameters']> = [
{
test: 'Invalid parameters',
} as unknown as RedirectParameters,
undefined as unknown as RedirectParameters,
} as unknown as Redirects['parameters'],
undefined as unknown as Redirects['parameters'],
hostnameParameters,
simpleParameters,
];
@ -56,7 +56,7 @@ test('Redirect.redirect', (t) => {
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
const simpleRedirect = new SimpleRedirect(simpleParameters);
const samples: Array<[string, Redirect]> = [
const samples: Array<[string, Redirect<RedirectParameters>]> = [
['https://example.com', hostnameRedirect],
['https://example.com/path#hash?query=test', hostnameRedirect],
['https://example.com', simpleRedirect],
@ -106,3 +106,17 @@ test('Narrow match & redirect types', (t) => {
t.true(matcherTypes.every((value) => narrowMatcherType(value)));
t.true(redirectTypes.every((value) => narrowRedirectType(value)));
});
test('Redirect getters & setters', (t) => {
const samples: Array<[Redirects, string]> = [
[new HostnameRedirect(hostnameParameters), hostnameParameters.hostname],
[new SimpleRedirect(simpleParameters), simpleParameters.target],
];
for (const [redirect, value] of samples) {
t.is(redirect.redirectValue, value);
const newValue = `${value} test`;
redirect.redirectValue = newValue;
t.is(redirect.redirectValue, newValue);
}
});

View File

@ -11,10 +11,10 @@ Generated by [AVA](https://avajs.dev).
HostnameRedirect {
id: 'id',
parameters: {
hostname: 'example.org',
matcherType: 'hostname',
matcherValue: 'example.com',
redirectType: 'hostname',
redirectValue: 'example.org',
toMatch: 'example.com',
},
}
@ -24,9 +24,9 @@ Generated by [AVA](https://avajs.dev).
id: 'id',
parameters: {
matcherType: 'hostname',
matcherValue: 'example.com',
redirectType: 'simple',
redirectValue: 'https://example.org/simple',
target: 'https://example.org/simple',
toMatch: 'example.com',
},
}