mCaptcha/templates/router.ts

92 lines
2 KiB
TypeScript
Raw Permalink Normal View History

// Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2021-05-01 09:11:22 +00:00
2021-07-16 15:46:49 +00:00
/** Removes trailing slash from URI */
2021-05-01 13:52:44 +00:00
const normalizeUri = (uri: string) => {
2021-05-06 12:46:13 +00:00
uri = uri.trim();
if (uri.length == 0) {
2021-10-08 09:54:29 +00:00
throw new Error("uri is empty");
2021-05-06 12:46:13 +00:00
}
2021-04-09 08:51:43 +00:00
2021-10-08 09:54:29 +00:00
const uriLength = uri.length;
if (uri[uriLength - 1] == "/") {
2021-05-06 12:46:13 +00:00
uri = uri.slice(0, uriLength - 1);
2021-04-09 08:51:43 +00:00
}
2021-05-06 12:46:13 +00:00
return uri;
2021-04-09 08:51:43 +00:00
};
/** URI<-> Fn mapping type */
2021-05-01 13:52:44 +00:00
type routeTuple = {
pattern: RegExp;
2021-05-01 13:52:44 +00:00
fn: () => void;
};
2021-05-06 05:23:05 +00:00
/**
* Router that selectively executes fucntions
2021-05-06 05:23:05 +00:00
* based on window.location.pathname
* */
2021-04-09 08:51:43 +00:00
export class Router {
2021-05-01 13:52:44 +00:00
routes: Array<routeTuple>;
2021-04-09 08:51:43 +00:00
constructor() {
this.routes = [];
}
2021-05-06 05:23:05 +00:00
/**
* registers a route-function pair with Router
* @param {string} uri - route to be registered
* @param {function} fn: - function to be registered when window.locatin.path
* matches uri
* */
2021-10-08 09:54:29 +00:00
register(uri: string, fn: () => void): void {
2021-05-06 06:41:06 +00:00
uri = normalizeUri(uri);
2021-10-08 09:54:29 +00:00
const pattern = new RegExp(`^${uri}$`);
2021-10-08 09:54:29 +00:00
const patterString = pattern.toString();
2021-05-06 12:46:13 +00:00
if (
2021-10-08 09:54:29 +00:00
this.routes.find((route) => {
if (route.pattern.toString() == patterString) {
2021-05-06 12:46:13 +00:00
return true;
} else {
return false;
2021-05-06 12:46:13 +00:00
}
})
) {
2021-10-08 09:54:29 +00:00
throw new Error("URI exists");
2021-05-06 12:46:13 +00:00
}
2021-04-09 08:51:43 +00:00
2021-05-01 13:52:44 +00:00
const route: routeTuple = {
pattern,
2021-04-09 08:51:43 +00:00
fn,
};
this.routes.push(route);
}
/**
* executes registered function with route
* matches window.pathname.location
* */
2021-10-08 09:54:29 +00:00
route(): void {
2021-05-06 05:23:05 +00:00
const path = normalizeUri(window.location.pathname);
2021-07-16 15:46:49 +00:00
let fn: undefined | (() => void);
2021-05-06 12:46:13 +00:00
2021-07-16 15:46:49 +00:00
if (
2021-10-08 09:54:29 +00:00
this.routes.find((route) => {
2021-07-16 15:46:49 +00:00
if (path.match(route.pattern)) {
fn = route.fn;
return true;
}
})
) {
if (fn === undefined) {
throw new Error("Route isn't registered");
} else {
return fn();
2021-04-09 08:51:43 +00:00
}
2021-05-06 12:46:13 +00:00
}
2021-04-09 08:51:43 +00:00
}
}