mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-13 19:15:39 +00:00
frontend integration
This commit is contained in:
67
templates/router.js
Normal file
67
templates/router.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const normalizeUri = uri => {
|
||||
if (!uri) {
|
||||
throw new Error('uri is empty');
|
||||
}
|
||||
|
||||
if (typeof uri !== 'string') {
|
||||
throw new TypeError('URI must be a string');
|
||||
}
|
||||
|
||||
let uriLength = uri.length;
|
||||
if (uri[uriLength - 1] == '/') {
|
||||
uri = uri.slice(0, uriLength - 1);
|
||||
}
|
||||
return uri;
|
||||
};
|
||||
|
||||
export class Router {
|
||||
constructor() {
|
||||
this.routes = [];
|
||||
}
|
||||
|
||||
register(uri, fn) {
|
||||
// typechecks
|
||||
if (!uri) {
|
||||
throw new Error('uri is empty');
|
||||
}
|
||||
|
||||
if (!fn) {
|
||||
throw new Error('fn is empty');
|
||||
}
|
||||
if (typeof uri !== 'string') {
|
||||
throw new TypeError('URI must be a string');
|
||||
}
|
||||
|
||||
if (typeof fn !== 'function') {
|
||||
throw new TypeError('a callback fn must be provided');
|
||||
}
|
||||
|
||||
this.routes.forEach(route => {
|
||||
if (route.uri == uri) {
|
||||
throw new Error(
|
||||
`URI exists. provided URI: ${uri}, registered config: ${route}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
uri = normalizeUri(uri);
|
||||
|
||||
const route = {
|
||||
uri,
|
||||
fn,
|
||||
};
|
||||
this.routes.push(route);
|
||||
}
|
||||
|
||||
route() {
|
||||
this.routes.forEach(route => {
|
||||
// normalize for trailing slash
|
||||
let pattern = new RegExp(`^${route.uri}$`);
|
||||
let path = window.location.pathname;
|
||||
path = normalizeUri(path);
|
||||
if (path.match(pattern)) {
|
||||
return route.fn.call();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user