mCaptcha/templates/router.ts

104 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-05-01 14:41:22 +05:30
/*
2022-01-08 22:16:05 +05:30
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
2021-05-01 14:41:22 +05:30
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-07-16 21:16:49 +05:30
/** Removes trailing slash from URI */
2021-05-01 19:22:44 +05:30
const normalizeUri = (uri: string) => {
2021-05-06 18:16:13 +05:30
uri = uri.trim();
if (uri.length == 0) {
2021-10-08 15:24:29 +05:30
throw new Error("uri is empty");
2021-05-06 18:16:13 +05:30
}
2021-04-09 14:21:43 +05:30
2021-10-08 15:24:29 +05:30
const uriLength = uri.length;
if (uri[uriLength - 1] == "/") {
2021-05-06 18:16:13 +05:30
uri = uri.slice(0, uriLength - 1);
2021-04-09 14:21:43 +05:30
}
2021-05-06 18:16:13 +05:30
return uri;
2021-04-09 14:21:43 +05:30
};
/** URI<-> Fn mapping type */
2021-05-01 19:22:44 +05:30
type routeTuple = {
pattern: RegExp;
2021-05-01 19:22:44 +05:30
fn: () => void;
};
2021-05-06 10:53:05 +05:30
/**
* Router that selectively executes fucntions
2021-05-06 10:53:05 +05:30
* based on window.location.pathname
* */
2021-04-09 14:21:43 +05:30
export class Router {
2021-05-01 19:22:44 +05:30
routes: Array<routeTuple>;
2021-04-09 14:21:43 +05:30
constructor() {
this.routes = [];
}
2021-05-06 10:53:05 +05:30
/**
* 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 15:24:29 +05:30
register(uri: string, fn: () => void): void {
2021-05-06 12:11:06 +05:30
uri = normalizeUri(uri);
2021-10-08 15:24:29 +05:30
const pattern = new RegExp(`^${uri}$`);
2021-10-08 15:24:29 +05:30
const patterString = pattern.toString();
2021-05-06 18:16:13 +05:30
if (
2021-10-08 15:24:29 +05:30
this.routes.find((route) => {
if (route.pattern.toString() == patterString) {
2021-05-06 18:16:13 +05:30
return true;
} else {
return false;
2021-05-06 18:16:13 +05:30
}
})
) {
2021-10-08 15:24:29 +05:30
throw new Error("URI exists");
2021-05-06 18:16:13 +05:30
}
2021-04-09 14:21:43 +05:30
2021-05-01 19:22:44 +05:30
const route: routeTuple = {
pattern,
2021-04-09 14:21:43 +05:30
fn,
};
this.routes.push(route);
}
/**
* executes registered function with route
* matches window.pathname.location
* */
2021-10-08 15:24:29 +05:30
route(): void {
2021-05-06 10:53:05 +05:30
const path = normalizeUri(window.location.pathname);
2021-07-16 21:16:49 +05:30
let fn: undefined | (() => void);
2021-05-06 18:16:13 +05:30
2021-07-16 21:16:49 +05:30
if (
2021-10-08 15:24:29 +05:30
this.routes.find((route) => {
2021-07-16 21:16:49 +05:30
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 14:21:43 +05:30
}
2021-05-06 18:16:13 +05:30
}
2021-04-09 14:21:43 +05:30
}
}