UNPKG

3.44 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2019,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.rebaseOpenApiSpec = exports.ExternalExpressRoutes = void 0;
8const tslib_1 = require("tslib");
9const express_1 = require("@loopback/express");
10const express_2 = tslib_1.__importDefault(require("express"));
11const http_errors_1 = tslib_1.__importDefault(require("http-errors"));
12const router_spec_1 = require("./router-spec");
13/**
14 * A registry of external, Express-style routes. These routes are invoked
15 * _after_ no LB4 route (controller or handler based) matched the incoming
16 * request.
17 *
18 * @internal
19 */
20class ExternalExpressRoutes {
21 constructor() {
22 this._externalRoutes = express_2.default.Router();
23 this._staticRoutes = express_2.default.Router();
24 this._specForExternalRoutes = { paths: {} };
25 }
26 get routerSpec() {
27 return this._specForExternalRoutes;
28 }
29 registerAssets(path, rootDir, options) {
30 this._staticRoutes.use(path, express_2.default.static(rootDir, options));
31 }
32 mountRouter(basePath, router, spec = { paths: {} }) {
33 this._externalRoutes.use(basePath, router);
34 spec = rebaseOpenApiSpec(spec, basePath);
35 (0, router_spec_1.assignRouterSpec)(this._specForExternalRoutes, spec);
36 }
37 find(request) {
38 return new ExternalRoute(this._externalRoutes, this._staticRoutes, request.method, request.url, {
39 description: 'External route or a static asset',
40 'x-visibility': 'undocumented',
41 responses: {},
42 });
43 }
44}
45exports.ExternalExpressRoutes = ExternalExpressRoutes;
46class ExternalRoute {
47 constructor(_externalRouter, _staticAssets, verb, path, spec) {
48 this._externalRouter = _externalRouter;
49 this._staticAssets = _staticAssets;
50 this.verb = verb;
51 this.path = path;
52 this.spec = spec;
53 // ResolvedRoute API
54 this.pathParams = [];
55 this.schemas = {};
56 }
57 updateBindings(requestContext) {
58 // no-op
59 }
60 async invokeHandler({ request, response }, args) {
61 let handled = await (0, express_1.executeExpressRequestHandler)(this._externalRouter, request, response);
62 if (handled)
63 return;
64 handled = await (0, express_1.executeExpressRequestHandler)(this._staticAssets, request, response);
65 if (handled)
66 return;
67 // Express router called next, which means no route was matched
68 throw new http_errors_1.default.NotFound(`Endpoint "${request.method} ${request.path}" not found.`);
69 }
70 describe() {
71 // TODO(bajtos) provide better description for Express routes with spec
72 return `External Express route "${this.verb} ${this.path}"`;
73 }
74}
75function rebaseOpenApiSpec(spec, basePath) {
76 if (!spec.paths)
77 return spec;
78 if (!basePath || basePath === '/')
79 return spec;
80 const localPaths = spec.paths;
81 // Don't modify the spec object provided to us.
82 spec = Object.assign({}, spec);
83 spec.paths = {};
84 for (const url in localPaths) {
85 spec.paths[`${basePath}${url}`] = localPaths[url];
86 }
87 return spec;
88}
89exports.rebaseOpenApiSpec = rebaseOpenApiSpec;
90//# sourceMappingURL=external-express-routes.js.map
\No newline at end of file