UNPKG

midnight

Version:

Web framework for building modern web applications

112 lines (111 loc) 4.15 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var path = require("path"); /** * @typedef {import("midnight").Request} Request * @typedef {import("midnight").Response} Response * @typedef {import("midnight").Next} Next * @typedef {import("midnight").Middleware} Middleware * @typedef {import("midnight").App} App */ /** * @typedef {import("midnight").Route} MidnightRoute */ /** * @param {import("midnight").App} app * @param {string} pattern * @param {(req: import("midnight").Request, res: import("midnight").Response) => void} handler * @param {import("midnight").Middleware[]} stack * @returns {import("midnight").Route} */ var newRoute = function (app, pattern, handler, stack) { app.log.info("Route: ".concat(pattern)); // TODO: debug var route = Route(app, pattern, handler, stack); app.routes.push(route); return route; }; /** * @param {import("midnight").App} app * @param {string} pattern * @param {(req: import("midnight").Request, res: import("midnight").Response) => void} handler * @param {import("midnight").Middleware[]} [stack] * @returns {import("midnight").Route} */ var Route = function (app, pattern, handler, stack) { if (stack === void 0) { stack = []; } /** @type {MidnightRoute} */ var route = __assign(__assign({ app: app, pattern: pattern, handler: handler, methods: [], stack: stack, children: false }, app.utils.path.normalize(pattern, [])), { method: function (method) { var _a; if (typeof method === "string") { route.methods.push(method); } else if (method instanceof Array) { (_a = route.methods).push.apply(_a, method); } return route; }, get: function () { route.methods.push("GET"); return route; }, post: function () { route.methods.push("POST"); return route; }, put: function () { route.methods.push("PUT"); return route; }, delete: function () { route.methods.push("DELETE"); return route; }, // Assign route specific middleware use: function (fn) { app.log.debug("Use middleware '".concat(fn.name, "' for route ").concat(pattern)); route.stack.push(fn); return route; }, // Subroute (route group) route: function (pattern, handler) { var fullPattern = typeof pattern === "string" ? path.join(route.pattern, pattern) : pattern; route.children = true; // This route has children return newRoute(app, path.resolve(fullPattern), handler, route.stack); }, match: function (path, method) { if (route.methods.length === 0 || route.methods.indexOf(method) != -1) { var params = {}; var splats = []; var captures = path.match(route.re); if (captures) { for (var i = 1; i < captures.length; ++i) { var key = route.keys[i - 1]; var value = typeof captures[i] === "string" ? unescape(captures[i]) : captures[i]; if (key) { params[key] = value; } else { splats.push(value); } } return { route: route, params: params, splats: splats, }; } } return; } }); return route; }; module.exports = newRoute;