1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.BaseRouter = void 0;
|
8 | const openapi_path_1 = require("./openapi-path");
|
9 | const route_entry_1 = require("./route-entry");
|
10 | const route_sort_1 = require("./route-sort");
|
11 |
|
12 |
|
13 |
|
14 | class BaseRouter {
|
15 | constructor(options = { strict: false }) {
|
16 | this.options = options;
|
17 | |
18 |
|
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 |
|
42 | let path = request.path;
|
43 |
|
44 | const route = this.findRoute(request.method, path);
|
45 | if (route || path === '/')
|
46 | return route;
|
47 | if (path.endsWith('/')) {
|
48 |
|
49 | path = path.substring(0, path.length - 1);
|
50 | }
|
51 | else {
|
52 |
|
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 |
|
69 | return routes.sort(route_sort_1.compareRoute);
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | getKey(verb, path) {
|
77 | verb = normalizeVerb(verb);
|
78 | path = normalizePath(path);
|
79 | return `/${verb}${path}`;
|
80 | }
|
81 | }
|
82 | exports.BaseRouter = BaseRouter;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function normalizeVerb(verb) {
|
88 |
|
89 | return (verb === null || verb === void 0 ? void 0 : verb.toLowerCase()) || 'get';
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function normalizePath(path) {
|
96 |
|
97 | path = path || '/';
|
98 | path = path.startsWith('/') ? path : `/${path}`;
|
99 | return path;
|
100 | }
|
101 |
|
\ | No newline at end of file |