UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2017,2020. 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.RoutingTable = void 0;
8const tslib_1 = require("tslib");
9const debug_1 = tslib_1.__importDefault(require("debug"));
10const http_errors_1 = tslib_1.__importDefault(require("http-errors"));
11const controller_route_1 = require("./controller-route");
12const openapi_path_1 = require("./openapi-path");
13const trie_router_1 = require("./trie-router");
14const debug = (0, debug_1.default)('loopback:rest:routing-table');
15/**
16 * Routing table
17 */
18class RoutingTable {
19 constructor(_router = new trie_router_1.TrieRouter(), _externalRoutes) {
20 this._router = _router;
21 this._externalRoutes = _externalRoutes;
22 }
23 /**
24 * Register a controller as the route
25 * @param spec
26 * @param controllerCtor
27 * @param controllerFactory
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 * Register a route
37 * @param route - A route entry
38 */
39 registerRoute(route) {
40 // TODO(bajtos) handle the case where opSpec.parameters contains $ref
41 // See https://github.com/loopbackio/loopback-next/issues/435
42 /* istanbul ignore if */
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 * Map a request to a route
63 * @param request
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}
80exports.RoutingTable = RoutingTable;
81function describeOperationParameters(opSpec) {
82 return (opSpec.parameters || [])
83 .map(p => (p === null || p === void 0 ? void 0 : p.name) || '')
84 .join(', ');
85}
86//# sourceMappingURL=routing-table.js.map
\No newline at end of file