UNPKG

3.32 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
3// Node module: @loopback/rest
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.BaseRouter = void 0;
8const openapi_path_1 = require("./openapi-path");
9const route_entry_1 = require("./route-entry");
10const route_sort_1 = require("./route-sort");
11/**
12 * Base router implementation that only handles path without variables
13 */
14class BaseRouter {
15 constructor(options = { strict: false }) {
16 this.options = options;
17 /**
18 * A map to optimize matching for routes without variables in the path
19 */
20 this.routesWithoutPathVars = {};
21 }
22 getKeyForRoute(route) {
23 return this.getKey(route.verb, route.path);
24 }
25 add(route) {
26 if (!(0, openapi_path_1.getPathVariables)(route.path)) {
27 const key = this.getKeyForRoute(route);
28 this.routesWithoutPathVars[key] = route;
29 }
30 else {
31 this.addRouteWithPathVars(route);
32 }
33 }
34 getKeyForRequest(request) {
35 return this.getKey(request.method, request.path);
36 }
37 find(request) {
38 if (this.options.strict) {
39 return this.findRoute(request.method, request.path);
40 }
41 // Non-strict mode
42 let path = request.path;
43 // First try the exact match
44 const route = this.findRoute(request.method, path);
45 if (route || path === '/')
46 return route;
47 if (path.endsWith('/')) {
48 // Fall back to the path without trailing slash
49 path = path.substring(0, path.length - 1);
50 }
51 else {
52 // Fall back to the path with trailing slash
53 path = path + '/';
54 }
55 return this.findRoute(request.method, path);
56 }
57 findRoute(verb, path) {
58 const key = this.getKey(verb, path);
59 const route = this.routesWithoutPathVars[key];
60 if (route)
61 return (0, route_entry_1.createResolvedRoute)(route, {});
62 else
63 return this.findRouteWithPathVars(verb, path);
64 }
65 list() {
66 let routes = Object.values(this.routesWithoutPathVars);
67 routes = routes.concat(this.listRoutesWithPathVars());
68 // Sort the routes so that they show up in OpenAPI spec in order
69 return routes.sort(route_sort_1.compareRoute);
70 }
71 /**
72 * Build a key for verb+path as `/<verb>/<path>`
73 * @param verb - HTTP verb/method
74 * @param path - URL path
75 */
76 getKey(verb, path) {
77 verb = normalizeVerb(verb);
78 path = normalizePath(path);
79 return `/${verb}${path}`;
80 }
81}
82exports.BaseRouter = BaseRouter;
83/**
84 * Normalize http verb to lowercase
85 * @param verb - Http verb
86 */
87function normalizeVerb(verb) {
88 // Use lower case, default to `get`
89 return (verb === null || verb === void 0 ? void 0 : verb.toLowerCase()) || 'get';
90}
91/**
92 * Normalize path to make sure it starts with `/`
93 * @param path - Path
94 */
95function normalizePath(path) {
96 // Prepend `/` if needed
97 path = path || '/';
98 path = path.startsWith('/') ? path : `/${path}`;
99 return path;
100}
101//# sourceMappingURL=router-base.js.map
\No newline at end of file