UNPKG

alapa

Version:

A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.

136 lines (135 loc) 6.07 kB
"use strict"; /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); exports.makeResourcefulRoute = void 0; const resourceful_1 = require("../../interface/resourceful"); require("reflect-metadata"); const tsyringe_1 = require("tsyringe"); /** * Cleans the provided path by removing double slashes. * @param path - The path to clean. * @returns Cleaned path without double slashes. */ const cleanPath = (path) => { return path.replace(/\/+/g, "/"); // Ensures no double slashes }; /** * Helper function to create resourceful routes. * This function handles the repetitive process of route creation. */ const createRoute = (method, routePath, controllerMethod, route, name, createNames, middleware = []) => { const routeHandler = route[method](cleanPath(routePath), ...middleware, controllerMethod); if (createNames) { routeHandler.name(name); } }; /** * Creates resourceful routes for a given controller. * * @param path - The base path for the resource. * @param controllerClass - The controller class to handle requests. * @param route - The router instance to attach the routes to. * @param options - Options for configuring the routes. */ const makeResourcefulRoute = (path, controllerClass, route, options) => { const controller = tsyringe_1.container.resolve(controllerClass); options = options || {}; if (controller.resourceOption) { options = { ...options, ...controller.resourceOption }; } if (!options) { options = {}; } const cleanName = (name) => { return `${options.namePrefix || ""}${name.replace(/\/+/g, "")}`; }; const verb = { ...resourceful_1.defaultVerb, ...options.verb }; const middleware = { ...controller.middleware, ...options.middleware, }; const changeNamesWithVerbs = options.changeNamesWithVerbs !== undefined ? options.changeNamesWithVerbs : true; const createNames = options.createNames !== undefined ? options.createNames : true; const only = Array.isArray(options.only) ? options.only : typeof options.only === "string" ? [options.only] : []; const except = Array.isArray(options.except) ? options.except : typeof options.except === "string" ? [options.except] : []; /** * Applies middleware before and after route handler. * @param action - The action (index, create, etc.). * @returns Middleware to apply for that action. */ const applyMiddleware = (action) => { let actionMiddleware = (middleware[action] || []); if (!Array.isArray(actionMiddleware)) { actionMiddleware = [actionMiddleware]; } let beforeMiddleware = (middleware.before || []); if (!Array.isArray(beforeMiddleware)) { beforeMiddleware = [beforeMiddleware]; } if (options.mergeMiddleware) { beforeMiddleware = [...beforeMiddleware, ...actionMiddleware]; } else { beforeMiddleware = [...actionMiddleware]; } let afterMiddleware = (middleware.after || []); if (!Array.isArray(afterMiddleware)) { afterMiddleware = [afterMiddleware]; } return [...beforeMiddleware, ...afterMiddleware]; }; // Index route if (!except.includes("index") && (only.length === 0 || only.includes("index"))) { const indexRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.index}` : `${path}.index`); createRoute("get", `/${path}`, controller.index, route, indexRouteName, createNames, applyMiddleware("index")); } // Create route if (!except.includes("create") && (only.length === 0 || only.includes("create"))) { const createRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.create}` : `${path}.create`); createRoute("get", `/${path}/${verb.create}`, controller.create, route, createRouteName, createNames, applyMiddleware("create")); } // Store route if (!except.includes("store") && (only.length === 0 || only.includes("store"))) { const storeRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.store}` : `${path}.store`); createRoute("post", `/${path}`, controller.store, route, storeRouteName, createNames, applyMiddleware("store")); } // Show route if (!except.includes("show") && (only.length === 0 || only.includes("show"))) { const showRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.show}` : `${path}.show`); createRoute("get", `/${path}/:id`, controller.show, route, showRouteName, createNames, applyMiddleware("show")); } // Edit route if (!except.includes("edit") && (only.length === 0 || only.includes("edit"))) { const editRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.edit}` : `${path}.edit`); createRoute("get", `/${path}/:id/${verb.edit}`, controller.edit, route, editRouteName, createNames, applyMiddleware("edit")); } // Update routes if (!except.includes("update") && (only.length === 0 || only.includes("update"))) { const updateRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.update}` : `${path}.update`); createRoute("put", `/${path}/:id`, controller.update, route, updateRouteName, createNames, applyMiddleware("update")); createRoute("patch", `/${path}/:id`, controller.update, route, updateRouteName, createNames, applyMiddleware("update")); } // Destroy route if (!except.includes("destroy") && (only.length === 0 || only.includes("destroy"))) { const destroyRouteName = cleanName(changeNamesWithVerbs ? `${path}.${verb.destroy}` : `${path}.destroy`); createRoute("delete", `/${path}/:id`, controller.destroy, route, destroyRouteName, createNames, applyMiddleware("destroy")); } }; exports.makeResourcefulRoute = makeResourcefulRoute;