104 lines
14 KiB
JavaScript
104 lines
14 KiB
JavaScript
import config from "config";
|
|
import Logger from "./Logger";
|
|
export default class Controller {
|
|
static route(route, params = [], absolute = false) {
|
|
let path = this.routes[route];
|
|
if (path === undefined)
|
|
throw new Error(`Unknown route for name ${route}.`);
|
|
if (typeof params === 'string' || typeof params === 'number') {
|
|
path = path.replace(/:[a-zA-Z_-]+\??/, '' + params);
|
|
}
|
|
else if (Array.isArray(params)) {
|
|
let i = 0;
|
|
for (const match of path.matchAll(/:[a-zA-Z_-]+\??/)) {
|
|
if (match.length > 0) {
|
|
path = path.replace(match[0], typeof params[i] !== 'undefined' ? params[i] : '');
|
|
}
|
|
i++;
|
|
}
|
|
path = path.replace(/\/+/, '/');
|
|
}
|
|
else {
|
|
for (const key in params) {
|
|
if (params.hasOwnProperty(key)) {
|
|
path = path.replace(new RegExp(`:${key}\\??`), params[key]);
|
|
}
|
|
}
|
|
}
|
|
return `${absolute ? config.get('public_url') : ''}${path}`;
|
|
}
|
|
getGlobalHandlers() {
|
|
return [];
|
|
}
|
|
hasGlobalHandlers() {
|
|
return this.getGlobalHandlers().length > 0;
|
|
}
|
|
setupGlobalHandlers(router) {
|
|
for (const globalHandler of this.getGlobalHandlers()) {
|
|
router.use(this.wrap(globalHandler));
|
|
}
|
|
}
|
|
getRoutesPrefix() {
|
|
return '/';
|
|
}
|
|
setupRoutes(router) {
|
|
this.router = router;
|
|
this.routes();
|
|
}
|
|
use(handler) {
|
|
var _a;
|
|
(_a = this.router) === null || _a === void 0 ? void 0 : _a.use(this.wrap(handler));
|
|
}
|
|
get(path, handler, routeName, ...middlewares) {
|
|
var _a, _b;
|
|
this.registerRoutes(path, handler, routeName);
|
|
for (const middleware of middlewares) {
|
|
(_a = this.router) === null || _a === void 0 ? void 0 : _a.get(path, this.wrap(middleware));
|
|
}
|
|
(_b = this.router) === null || _b === void 0 ? void 0 : _b.get(path, this.wrap(handler));
|
|
}
|
|
post(path, handler, routeName, ...middlewares) {
|
|
var _a, _b;
|
|
this.registerRoutes(path, handler, routeName);
|
|
for (const middleware of middlewares) {
|
|
(_a = this.router) === null || _a === void 0 ? void 0 : _a.post(path, this.wrap(middleware));
|
|
}
|
|
(_b = this.router) === null || _b === void 0 ? void 0 : _b.post(path, this.wrap(handler));
|
|
}
|
|
wrap(handler) {
|
|
return (req, res, next) => {
|
|
const promise = handler.call(this, req, res, next);
|
|
if (promise instanceof Promise) {
|
|
promise.catch(err => next(err));
|
|
}
|
|
};
|
|
}
|
|
registerRoutes(path, handler, routeName) {
|
|
if (typeof routeName !== 'string') {
|
|
routeName = handler.name
|
|
.replace(/(?:^|\.?)([A-Z])/g, (x, y) => '_' + y.toLowerCase())
|
|
.replace(/(^_|get_|post_)/g, '');
|
|
}
|
|
if (routeName.length === 0)
|
|
return;
|
|
let routePath = null;
|
|
if (path instanceof Array && path.length > 0) {
|
|
path = path[0];
|
|
}
|
|
if (typeof path === 'string') {
|
|
const prefix = this.getRoutesPrefix();
|
|
routePath = (prefix !== '/' ? prefix : '') + path;
|
|
}
|
|
if (!Controller.routes[routeName]) {
|
|
if (typeof routePath === 'string') {
|
|
Logger.info(`Route ${routeName} has path ${routePath}`);
|
|
Controller.routes[routeName] = routePath;
|
|
}
|
|
else {
|
|
Logger.warn(`Cannot assign path to route ${routeName}.`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Controller.routes = {};
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ29udHJvbGxlci5qcyIsInNvdXJjZVJvb3QiOiIuLyIsInNvdXJjZXMiOlsiQ29udHJvbGxlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLE1BQU0sTUFBTSxRQUFRLENBQUM7QUFDNUIsT0FBTyxNQUFNLE1BQU0sVUFBVSxDQUFDO0FBRTlCLE1BQU0sQ0FBQyxPQUFPLE9BQWdCLFVBQVU7SUFHN0IsTUFBTSxDQUFDLEtBQUssQ0FBQyxL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SBbXSwgYWJzb2x1dGU6IGJvb2xlYW4gPSBmYWxzZSk6IHN0cmluZyB7XG4gICAgICAgIGxldCBwYXRoID0gdGhpcy5yb3V0ZXNbcm91dGVdO1xuICAgICAgICBpZiAocGF0aCA9PT0gdW5kZWZpbmVkKSB0aHJvdyBuZXcgRXJyb3IoYFVua25vd24gcm91dGUgZm9yIG5hbWUgJHtyb3V0ZX0uYCk7XG5cbiAgICAgICAgaWYgKHR5cGVvZiBwYXJhbXMgPT09ICdzdHJpbmcnIHx8IHR5cGVvZiBwYXJhbXMgPT09ICdudW1iZXInKSB7XG4gICAgICAgICAgICBwYXRoID0gcGF0aC5yZXBsYWNlKC86W2EtekEtWl8tXStcXD8/LywgJycgKyBwYXJhbXMpO1xuICAgICAgICB9IGVsc2UgaWYgKEFycmF5LmlzQXJyYXkocGFyYW1zKSkge1xuICAgICAgICAgICAgbGV0IGkgPSAwO1xuICAgICAgICAgICAgZm9yIChjb25zdCBtYXRjaCBvZiBwYXRoLm1hdGNoQWxsKC86W2EtekEtWl8tXStcXD8/LykpIHtcbiAgICAgICAgICAgICAgICBpZiAobWF0Y2gubGVuZ3RoID4gMCkge1xuICAgICAgICAgICAgICAgICAgICBwYXRoID0gcGF0aC5yZXBsYWNlKG1hdGNoWzBdLCB0eXBlb2YgcGFyYW1zW2ldICE9PSAndW5kZWZpbmVkJyA/IHBhcmFtc1tpXSA6ICcnKTtcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgaSsrO1xuICAgICAgICAgICAgfVxuICAgICAgICAgICAgcGF0aCA9IHBhdGgucmVwbGFjZSgvXFwvKy8sICcvJyk7XG4gICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICBmb3IgKGNvbnN0IGtleSBpbiBwYXJhbXMpIHtcbiAgICAgICAgICAgICAgICBpZiAocGFyYW1zLmhhc093blByb3BlcnR5KGtleSkpIHtcbiAgICAgICAgICAgICAgICAgICAgcGF0aCA9IHBhdGgucmVwbGFjZShuZXcg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FJlcXVlc3RIYW5kbGVyKTogUmVxdWVzdEhhbmRsZXIge1xuICAgICAgICByZXR1cm4gKHJlcSwgcmVzLCBuZXh0KSA9PiB7XG4gICAgICAgICAgICBjb25zdCBwcm9taXNlID0gaGFuZGxlci5jYWxsKHRoaXMsIHJlcSwgcmVzLCBuZXh0KTtcbiAgICAgICAgICAgIGlmIChwcm9taXNlIGluc3RhbmNlb2YgUHJvbWlzZSkge1xuICAgICAgICAgICAgICAgIHByb21pc2UuY2F0Y2goZXJyID0+IG5leHQoZXJyKSk7XG4gICAgICAgICAgICB9XG4gICAgICAgIH07XG4gICAgfVxuXG4gICAgcHJpdmF0ZSByZWdpc3RlclJvdXRlcyhwYXRoOiBQYXRoUGFyYW1zLCBoYW5kbGVyOiBSZXF1ZXN0SGFuZGxlciwgcm91dGVOYW1lPzogc3RyaW5nKSB7XG4gICAgICAgIGlmICh0eXBlb2Ygcm91dGVOYW1lICE9PSAnc3RyaW5nJykge1xuICAgICAgICAgICAgcm91dGVOYW1lID0gaGFuZGxlci5uYW1lXG4gICAgICAgICAgICAgICAgLnJlcGxhY2UoLyg/Ol58XFwuPykoW0EtWl0pL2csICh4LCB5KSA9PiAnXycgKyB5LnRvTG93ZXJDYXNlKCkpXG4gICAgICAgICAgICAgICAgLnJlcGxhY2UoLyheX3xnZXRffHBvc3RfKS9nLCAnJyk7XG4gICAgICAgIH1cblxuICAgICAgICBpZiAocm91dGVOYW1lLmxlbmd0aCA9PT0gMCkgcmV0dXJuO1xuXG4gICAgICAgIGxldCByb3V0ZVBhdGggPSBudWxsO1xuICAgICAgICBpZiAocGF0aCBpbnN0YW5jZW9mIEFycmF5ICYmIHBhdGgubGVuZ3RoID4gMCkge1xuICAgICAgICAgICAgcGF0aCA9IHBhdGhbMF07XG4gICAgICAgIH1cbiAgICAgICAgaWYgKHR5cGVvZiBwYXRoID09PSAnc3RyaW5nJykge1xuICAgICAgICAgICAgY29uc3QgcHJlZml4ID0gdGhpcy5nZXRSb3V0ZXNQcmVmaXgoKTtcbiAgICAgICAgICAgIHJvdXRlUGF0aCA9IChwcmVmaXggIT09ICcvJyA/IHByZWZpeCA6ICcnKSArIHBhdGg7XG4gICAgICAgIH1cblxuICAgICAgICBpZiAoIUNvbnRyb2xsZXIucm91dGVzW3JvdXRlTmFtZV0pIHtcbiAgICAgICAgICAgIGlmICh0eXBlb2Ygcm91dGVQYXRoID09PSAnc3RyaW5nJykge1xuICAgICAgICAgICAgICAgIExvZ2dlci5pbmZvKGBSb3V0ZSAke3JvdXRlTmFtZX0gaGFzIHBhdGggJHtyb3V0ZVBhdGh9YCk7XG4gICAgICAgICAgICAgICAgQ29udHJvbGxlci5yb3V0ZXNbcm91dGVOYW1lXSA9IHJvdXRlUGF0aDtcbiAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgICAgTG9nZ2VyLndhcm4oYENhbm5vdCBhc3NpZ24gcGF0aCB0byByb3V0ZSAke3JvdXRlTmFtZX0uYCk7XG4gICAgICAgICAgICB9XG4gICAgICAgIH1cbiAgICB9XG59XG5cbmV4cG9ydCB0eXBlIFJvdXRlUGFyYW1zID0geyBbcDogc3RyaW5nXTogc3RyaW5nIH0gfCBzdHJpbmdbXSB8IHN0cmluZyB8IG51bWJlcjsiXX0=
|