swaf/dist/Controller.js

104 lines
14 KiB
JavaScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ29udHJvbGxlci5qcyIsInNvdXJjZVJvb3QiOiIuLyIsInNvdXJjZXMiOlsiQ29udHJvbGxlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLE1BQU0sTUFBTSxRQUFRLENBQUM7QUFDNUIsT0FBTyxNQUFNLE1BQU0sVUFBVSxDQUFDO0FBRTlCLE1BQU0sQ0FBQyxPQUFPLE9BQWdCLFVBQVU7SUFHN0IsTUFBTSxDQUFDLEtBQUssQ0FBQyxLQUFhLEVBQUUsU0FBc0IsRUFBRSxFQUFFLFdBQW9CLEtBQUs7UUFDbEYsSUFBSSxJQUFJLEdBQUcsSUFBSSxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsQ0FBQztRQUM5QixJQUFJLElBQUksS0FBSyxTQUFTO1lBQUUsTUFBTSxJQUFJLEtBQUssQ0FBQywwQkFBMEIsS0FBSyxHQUFHLENBQUMsQ0FBQztRQUU1RSxJQUFJLE9BQU8sTUFBTSxLQUFLLFFBQVEsSUFBSSxPQUFPLE1BQU0sS0FBSyxRQUFRLEVBQUU7WUFDMUQsSUFBSSxHQUFHLElBQUksQ0FBQyxPQUFPLENBQUMsaUJBQWlCLEVBQUUsRUFBRSxHQUFHLE1BQU0sQ0F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