1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.RoutingTable = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const http_errors_1 = tslib_1.__importDefault(require("http-errors"));
|
11 | const controller_route_1 = require("./controller-route");
|
12 | const openapi_path_1 = require("./openapi-path");
|
13 | const trie_router_1 = require("./trie-router");
|
14 | const debug = (0, debug_1.default)('loopback:rest:routing-table');
|
15 |
|
16 |
|
17 |
|
18 | class RoutingTable {
|
19 | constructor(_router = new trie_router_1.TrieRouter(), _externalRoutes) {
|
20 | this._router = _router;
|
21 | this._externalRoutes = _externalRoutes;
|
22 | }
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | registerController(spec, controllerCtor, controllerFactory) {
|
30 | const routes = (0, controller_route_1.createRoutesForController)(spec, controllerCtor, controllerFactory);
|
31 | for (const route of routes) {
|
32 | this.registerRoute(route);
|
33 | }
|
34 | }
|
35 | |
36 |
|
37 |
|
38 |
|
39 | registerRoute(route) {
|
40 |
|
41 |
|
42 |
|
43 | if (debug.enabled) {
|
44 | debug('Registering route %s %s -> %s(%s)', route.verb.toUpperCase(), route.path, route.describe(), describeOperationParameters(route.spec));
|
45 | }
|
46 | (0, openapi_path_1.validateApiPath)(route.path);
|
47 | this._router.add(route);
|
48 | }
|
49 | describeApiPaths() {
|
50 | const paths = {};
|
51 | for (const route of this._router.list()) {
|
52 | if (route.spec['x-visibility'] === 'undocumented')
|
53 | continue;
|
54 | if (!paths[route.path]) {
|
55 | paths[route.path] = {};
|
56 | }
|
57 | paths[route.path][route.verb] = route.spec;
|
58 | }
|
59 | return paths;
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 | find(request) {
|
66 | debug('Finding route for %s %s', request.method, request.path);
|
67 | const found = this._router.find(request);
|
68 | if (found) {
|
69 | debug('Route matched: %j', found);
|
70 | return found;
|
71 | }
|
72 | if (this._externalRoutes) {
|
73 | debug('No API route found for %s %s, trying to find an external Express route', request.method, request.path);
|
74 | return this._externalRoutes.find(request);
|
75 | }
|
76 | debug('No route found for %s %s', request.method, request.path);
|
77 | throw new http_errors_1.default.NotFound(`Endpoint "${request.method} ${request.path}" not found.`);
|
78 | }
|
79 | }
|
80 | exports.RoutingTable = RoutingTable;
|
81 | function describeOperationParameters(opSpec) {
|
82 | return (opSpec.parameters || [])
|
83 | .map(p => (p === null || p === void 0 ? void 0 : p.name) || '')
|
84 | .join(', ');
|
85 | }
|
86 |
|
\ | No newline at end of file |