UNPKG

5.49 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,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.RestComponent = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const express_1 = require("@loopback/express");
11const openapi_v3_1 = require("@loopback/openapi-v3");
12const body_parsers_1 = require("./body-parsers");
13const body_parser_raw_1 = require("./body-parsers/body-parser.raw");
14const keys_1 = require("./keys");
15const providers_1 = require("./providers");
16const rest_server_1 = require("./rest.server");
17const consolidate_spec_enhancer_1 = require("./spec-enhancers/consolidate.spec-enhancer");
18const info_spec_enhancer_1 = require("./spec-enhancers/info.spec-enhancer");
19const ajv_factory_provider_1 = require("./validation/ajv-factory.provider");
20let RestComponent = class RestComponent {
21 constructor(app, config) {
22 var _a;
23 this.providers = {
24 [keys_1.RestBindings.AJV_FACTORY.key]: ajv_factory_provider_1.AjvFactoryProvider,
25 };
26 /**
27 * Add built-in body parsers
28 */
29 this.bindings = [
30 ...createActionBindings(),
31 // FIXME(rfeng): We now register request body parsers in TRANSIENT scope
32 // so that they can be bound at application or server level
33 core_1.Binding.bind(keys_1.RestBindings.REQUEST_BODY_PARSER).toClass(body_parsers_1.RequestBodyParser),
34 (0, rest_server_1.createBodyParserBinding)(body_parsers_1.JsonBodyParser, keys_1.RestBindings.REQUEST_BODY_PARSER_JSON),
35 (0, rest_server_1.createBodyParserBinding)(body_parsers_1.TextBodyParser, keys_1.RestBindings.REQUEST_BODY_PARSER_TEXT),
36 (0, rest_server_1.createBodyParserBinding)(body_parsers_1.UrlEncodedBodyParser, keys_1.RestBindings.REQUEST_BODY_PARSER_URLENCODED),
37 (0, rest_server_1.createBodyParserBinding)(body_parser_raw_1.RawBodyParser, keys_1.RestBindings.REQUEST_BODY_PARSER_RAW),
38 (0, rest_server_1.createBodyParserBinding)(body_parsers_1.StreamBodyParser, keys_1.RestBindings.REQUEST_BODY_PARSER_STREAM),
39 (0, core_1.createBindingFromClass)(info_spec_enhancer_1.InfoSpecEnhancer),
40 (0, core_1.createBindingFromClass)(consolidate_spec_enhancer_1.ConsolidationEnhancer),
41 ...getRestMiddlewareBindings(),
42 ];
43 this.servers = {
44 RestServer: rest_server_1.RestServer,
45 };
46 // Register the `InvokeMiddleware` with default to `ACTION_MIDDLEWARE_CHAIN`
47 // to keep backward compatibility with action based sequence
48 const invokeMiddlewareActionBinding = (0, core_1.createBindingFromClass)(express_1.InvokeMiddlewareProvider, {
49 key: keys_1.RestBindings.SequenceActions.INVOKE_MIDDLEWARE,
50 }).tag({ [core_1.CoreTags.EXTENSION_POINT]: keys_1.RestTags.ACTION_MIDDLEWARE_CHAIN });
51 app.add(invokeMiddlewareActionBinding);
52 // Register the `InvokeMiddleware` with default to `DEFAULT_MIDDLEWARE_CHAIN`
53 // for the middleware based sequence
54 const invokeMiddlewareServiceBinding = (0, core_1.createBindingFromClass)(express_1.InvokeMiddlewareProvider, {
55 key: keys_1.RestBindings.INVOKE_MIDDLEWARE_SERVICE,
56 }).tag({ [core_1.CoreTags.EXTENSION_POINT]: keys_1.RestTags.REST_MIDDLEWARE_CHAIN });
57 app.add(invokeMiddlewareServiceBinding);
58 const apiSpec = (0, openapi_v3_1.createEmptyApiSpec)();
59 // Merge the OpenAPI `servers` spec from the config into the empty one
60 if ((_a = config === null || config === void 0 ? void 0 : config.openApiSpec) === null || _a === void 0 ? void 0 : _a.servers) {
61 Object.assign(apiSpec, { servers: config.openApiSpec.servers });
62 }
63 app.bind(keys_1.RestBindings.API_SPEC).to(apiSpec);
64 }
65};
66RestComponent = tslib_1.__decorate([
67 tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
68 tslib_1.__param(1, (0, core_1.inject)(keys_1.RestBindings.CONFIG)),
69 tslib_1.__metadata("design:paramtypes", [core_1.Application, Object])
70], RestComponent);
71exports.RestComponent = RestComponent;
72function getRestMiddlewareBindings() {
73 return [
74 providers_1.SendResponseMiddlewareProvider,
75 providers_1.FindRouteMiddlewareProvider,
76 providers_1.ParseParamsMiddlewareProvider,
77 providers_1.InvokeMethodMiddlewareProvider,
78 ].map(cls => (0, core_1.createBindingFromClass)(cls));
79}
80function createActionBindings() {
81 const bindings = [];
82 const providers = {
83 [keys_1.RestBindings.SequenceActions.LOG_ERROR.key]: providers_1.LogErrorProvider,
84 [keys_1.RestBindings.SequenceActions.FIND_ROUTE.key]: providers_1.FindRouteProvider,
85 [keys_1.RestBindings.SequenceActions.INVOKE_METHOD.key]: providers_1.InvokeMethodProvider,
86 [keys_1.RestBindings.SequenceActions.REJECT.key]: providers_1.RejectProvider,
87 [keys_1.RestBindings.SequenceActions.PARSE_PARAMS.key]: providers_1.ParseParamsProvider,
88 [keys_1.RestBindings.SequenceActions.SEND.key]: providers_1.SendProvider,
89 };
90 for (const k in providers) {
91 bindings.push((0, core_1.createBindingFromClass)(providers[k], { key: k }));
92 }
93 return bindings;
94}
95//# sourceMappingURL=rest.component.js.map
\No newline at end of file