UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

123 lines 4.51 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Route = void 0; const http = __importStar(require("http")); const path_to_regexp_1 = require("path-to-regexp"); const container_1 = require("../../container"); const middleware_1 = require("../middleware"); const response_1 = require("../response"); const helpers_1 = require("./helpers"); class Route { constructor(uri, methods = [], controller = null, action = '') { this.app = container_1.Container.get('app'); this.keys = []; this.enctypt = false; this.middleware = this.app.make(middleware_1.MiddlewareService, [], true); this.uri = uri; this.methods = this.parseMethods(methods); this.regexp = (0, path_to_regexp_1.pathToRegexp)(uri, this.keys); this.controller = controller; this.action = action; if (this.methods.includes('GET') && !this.methods.includes('HEAD')) { this.methods.push('HEAD'); } } get pieces() { const paths = (0, helpers_1.parsePattern)(this.uri); const res = []; for (const _path of paths) { if (_path === '*') { res.push({ key: _path, type: 'all' }); } else { const keys = []; const reg = (0, path_to_regexp_1.pathToRegexp)(_path, keys); if (!keys.length) { res.push({ key: _path, type: 'static' }); } else { res.push({ key: reg, type: 'reg' }); } } } return res; } registerMiddleware(middleware, args) { if (middleware && typeof middleware === 'function') { this.middleware.register(middleware, args); } return this; } addMethod(method) { const _method = method.toUpperCase(); if (http.METHODS.includes(_method) && !this.methods.includes(_method)) { this.methods.push(_method); } return this; } parseMethods(methods = []) { const _methods = []; for (const method of methods) { const _method = method.toUpperCase(); _methods.push(_method); } return [...new Set(_methods)]; } getParams(requestPath) { var _a, _b; return (_b = (_a = requestPath.match(this.regexp)) === null || _a === void 0 ? void 0 : _a.slice(1)) !== null && _b !== void 0 ? _b : []; } registerControllerMiddlewares(middlewares) { if (!Array.isArray(middlewares)) return this; for (const middleware of middlewares) { this.middleware.register(middleware); } return this; } match(requestPath) { return !!this.regexp.exec(requestPath); } async resolve(request) { const controller = this.app.get(this.controller, [request]); const routeParams = this.getParams(request.path); const res = await controller[this.action](...routeParams); if (res instanceof response_1.Response) return res.encrypt(this.enctypt); return (new response_1.Response()).setData(res).encrypt(this.enctypt); } } exports.Route = Route; //# sourceMappingURL=route.js.map