UNPKG

2.54 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 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.HttpHandler = void 0;
8const keys_1 = require("./keys");
9const request_context_1 = require("./request-context");
10const router_1 = require("./router");
11class HttpHandler {
12 constructor(_rootContext, _serverConfig, _routes = new router_1.RoutingTable()) {
13 this._rootContext = _rootContext;
14 this._serverConfig = _serverConfig;
15 this._routes = _routes;
16 this.handleRequest = (req, res) => this._handleRequest(req, res);
17 }
18 registerController(spec, controllerCtor, controllerFactory) {
19 this._routes.registerController(spec, controllerCtor, controllerFactory);
20 }
21 registerRoute(route) {
22 this._routes.registerRoute(route);
23 }
24 /**
25 * @deprecated Use `registerApiComponents`
26 * @param defs Schemas
27 */
28 registerApiDefinitions(defs) {
29 this.registerApiComponents({ schemas: defs });
30 }
31 /**
32 * Merge components into the OpenApi spec
33 * @param defs - Components
34 */
35 registerApiComponents(defs) {
36 var _a;
37 this._openApiComponents = (_a = this._openApiComponents) !== null && _a !== void 0 ? _a : {};
38 for (const p in defs) {
39 // Merge each child, such as `schemas`, `parameters`, and `headers`
40 this._openApiComponents[p] = { ...this._openApiComponents[p], ...defs[p] };
41 }
42 }
43 getApiComponents() {
44 return this._openApiComponents;
45 }
46 /**
47 * @deprecated Use `getApiComponents`
48 */
49 getApiDefinitions() {
50 var _a;
51 return (_a = this._openApiComponents) === null || _a === void 0 ? void 0 : _a.schemas;
52 }
53 describeApiPaths() {
54 return this._routes.describeApiPaths();
55 }
56 findRoute(request) {
57 const route = this._routes.find(request);
58 Object.assign(route.schemas, this.getApiDefinitions());
59 return route;
60 }
61 async _handleRequest(request, response) {
62 const requestContext = new request_context_1.RequestContext(request, response, this._rootContext, this._serverConfig);
63 const sequence = await requestContext.get(keys_1.RestBindings.SEQUENCE);
64 await sequence.handle(requestContext);
65 }
66}
67exports.HttpHandler = HttpHandler;
68//# sourceMappingURL=http-handler.js.map
\No newline at end of file