alapa
Version:
A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.
113 lines (112 loc) • 3.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
const express_1 = require("express");
const chain_1 = require("./chain");
const processor_1 = require("./processor");
const middleware_1 = require("./middleware");
const utils_1 = require("../utils");
const resource_1 = require("./resource");
class Router {
routeChain;
expressRouter;
routesNames;
middleware;
resourceHandlers;
constructor(options) {
this.routesNames = {};
this.expressRouter = (0, express_1.Router)(options);
this.routeChain = new chain_1.RouteChainManger(this, this.routesNames);
this.resourceHandlers = new resource_1.ResourceRouteManager(this);
this.middleware = new middleware_1.MiddlewareRouteHandler();
}
getNames = () => this.routesNames;
get(path, ...handlers) {
return this.addMethod("get", path, ...handlers);
}
post(path, ...handlers) {
return this.addMethod("post", path, ...handlers);
}
put(path, ...handlers) {
return this.addMethod("put", path, ...handlers);
}
delete(path, ...handlers) {
return this.addMethod("delete", path, ...handlers);
}
patch(path, ...handlers) {
return this.addMethod("patch", path, ...handlers);
}
options(path, ...handlers) {
return this.addMethod("options", path, ...handlers);
}
head(path, ...handlers) {
return this.addMethod("head", path, ...handlers);
}
resource(path, controller, option) {
return this.resourceHandlers.resource(path, controller, option);
}
restfulResource(path, controller, option) {
return this.resourceHandlers.restfulResource(path, controller, option);
}
apiResource(path, controller, option) {
return this.resourceHandlers.apiResource(path, controller, option);
}
resources(resources, options) {
return this.resourceHandlers.resources(resources, options);
}
restfulResources(resources, options) {
return this.resourceHandlers.restfulResources(resources, options);
}
apiResources(resources, options) {
return this.resourceHandlers.apiResources(resources, options);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
view(path, view, data) {
// return this.processView(path, view, data);]
return this.get(path, view);
}
toExpressRoutes() {
return this.getRoutes();
}
addExpressRoute(method, path, ...handlers) {
this.expressRouter[method](path, ...handlers);
}
addRoute(method, path, ...handlers) {
const proceedHandler = (0, processor_1.processHandlers)(...handlers);
this.addExpressRoute(method, path, ...proceedHandler);
}
getRoutes() {
return this.expressRouter;
}
addMethod(method, path, ...handlers) {
this.addRoute(method, path, ...handlers);
return this.routeChain.getChain(path);
}
use(pathOrHandler, ...handlers) {
return this.processUse(pathOrHandler, ...handlers);
}
all(path, ...handlers) {
return this.addMethod("all", path, ...handlers);
}
processUse(pathOrHandler, ...handlers) {
const result = this.middleware.processUse(pathOrHandler, ...handlers);
const middleware = [];
if (result.processed.length > 0) {
middleware.push(...result.processed);
}
for (const route of result.routers) {
if (result.path) {
(0, utils_1.adjustNameWithPrefix)(result.path, route);
}
middleware.push(route.toExpressRoutes());
}
if (result.path) {
this.expressRouter.use(result.path, middleware);
}
else {
this.expressRouter.use(middleware);
}
return this.all("");
}
}
exports.Router = Router;