1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.rebaseOpenApiSpec = exports.ExternalExpressRoutes = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const express_1 = require("@loopback/express");
|
10 | const express_2 = tslib_1.__importDefault(require("express"));
|
11 | const http_errors_1 = tslib_1.__importDefault(require("http-errors"));
|
12 | const router_spec_1 = require("./router-spec");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class 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 | }
|
45 | exports.ExternalExpressRoutes = ExternalExpressRoutes;
|
46 | class 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 |
|
54 | this.pathParams = [];
|
55 | this.schemas = {};
|
56 | }
|
57 | updateBindings(requestContext) {
|
58 |
|
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 |
|
68 | throw new http_errors_1.default.NotFound(`Endpoint "${request.method} ${request.path}" not found.`);
|
69 | }
|
70 | describe() {
|
71 |
|
72 | return `External Express route "${this.verb} ${this.path}"`;
|
73 | }
|
74 | }
|
75 | function rebaseOpenApiSpec(spec, basePath) {
|
76 | if (!spec.paths)
|
77 | return spec;
|
78 | if (!basePath || basePath === '/')
|
79 | return spec;
|
80 | const localPaths = spec.paths;
|
81 |
|
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 | }
|
89 | exports.rebaseOpenApiSpec = rebaseOpenApiSpec;
|
90 |
|
\ | No newline at end of file |