1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.createBodyParserBinding = exports.RestServer = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const core_1 = require("@loopback/core");
|
10 | const express_1 = require("@loopback/express");
|
11 | const http_server_1 = require("@loopback/http-server");
|
12 | const openapi_v3_1 = require("@loopback/openapi-v3");
|
13 | const assert_1 = tslib_1.__importStar(require("assert"));
|
14 | const cors_1 = tslib_1.__importDefault(require("cors"));
|
15 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
16 | const express_2 = tslib_1.__importDefault(require("express"));
|
17 | const fs_1 = tslib_1.__importDefault(require("fs"));
|
18 | const js_yaml_1 = require("js-yaml");
|
19 | const lodash_1 = require("lodash");
|
20 | const strong_error_handler_1 = require("strong-error-handler");
|
21 | const body_parsers_1 = require("./body-parsers");
|
22 | const http_handler_1 = require("./http-handler");
|
23 | const keys_1 = require("./keys");
|
24 | const request_context_1 = require("./request-context");
|
25 | const router_1 = require("./router");
|
26 | const router_spec_1 = require("./router/router-spec");
|
27 | const sequence_1 = require("./sequence");
|
28 | const debug = (0, debug_1.default)('loopback:rest:server');
|
29 | const SequenceActions = keys_1.RestBindings.SequenceActions;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | let RestServer = class RestServer extends express_1.BaseMiddlewareRegistry {
|
58 |
|
59 | get OASEnhancer() {
|
60 | this._setupOASEnhancerIfNeeded();
|
61 | return this.oasEnhancerService;
|
62 | }
|
63 | get requestHandler() {
|
64 | if (this._requestHandler == null) {
|
65 | this._setupRequestHandlerIfNeeded();
|
66 | }
|
67 | return this._requestHandler;
|
68 | }
|
69 | get httpHandler() {
|
70 | this._setupHandlerIfNeeded();
|
71 | return this._httpHandler;
|
72 | }
|
73 | get listening() {
|
74 | return this._httpServer ? this._httpServer.listening : false;
|
75 | }
|
76 | get httpServer() {
|
77 | return this._httpServer;
|
78 | }
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 | get url() {
|
85 | let serverUrl = this.rootUrl;
|
86 | if (!serverUrl)
|
87 | return serverUrl;
|
88 | serverUrl = serverUrl + (this._basePath || '');
|
89 | return serverUrl;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 | get rootUrl() {
|
96 | var _a;
|
97 | return (_a = this._httpServer) === null || _a === void 0 ? void 0 : _a.url;
|
98 | }
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | constructor(app, config = {}) {
|
110 | var _a;
|
111 | super(app);
|
112 | |
113 |
|
114 |
|
115 | this._externalRoutes = new router_1.ExternalExpressRoutes();
|
116 | this.scope = core_1.BindingScope.SERVER;
|
117 | this.config = resolveRestServerConfig(config);
|
118 | this.bind(keys_1.RestBindings.PORT).to(this.config.port);
|
119 | this.bind(keys_1.RestBindings.HOST).to(config.host);
|
120 | this.bind(keys_1.RestBindings.PATH).to(config.path);
|
121 | this.bind(keys_1.RestBindings.PROTOCOL).to((_a = config.protocol) !== null && _a !== void 0 ? _a : 'http');
|
122 | this.bind(keys_1.RestBindings.HTTPS_OPTIONS).to(config);
|
123 | if (config.requestBodyParser) {
|
124 | this.bind(keys_1.RestBindings.REQUEST_BODY_PARSER_OPTIONS).to(config.requestBodyParser);
|
125 | }
|
126 | if (config.sequence) {
|
127 | this.sequence(config.sequence);
|
128 | }
|
129 | else {
|
130 | this.sequence(sequence_1.MiddlewareSequence);
|
131 | }
|
132 | if (config.router) {
|
133 | this.bind(keys_1.RestBindings.ROUTER_OPTIONS).to(config.router);
|
134 | }
|
135 | this.basePath(config.basePath);
|
136 | this.bind(keys_1.RestBindings.BASE_PATH).toDynamicValue(() => this._basePath);
|
137 | this.bind(keys_1.RestBindings.HANDLER).toDynamicValue(() => this.httpHandler);
|
138 | }
|
139 | _setupOASEnhancerIfNeeded() {
|
140 | if (this.oasEnhancerService != null)
|
141 | return;
|
142 | this.add((0, core_1.createBindingFromClass)(openapi_v3_1.OASEnhancerService, {
|
143 | key: openapi_v3_1.OASEnhancerBindings.OAS_ENHANCER_SERVICE,
|
144 | }));
|
145 | this.oasEnhancerService = this.getSync(openapi_v3_1.OASEnhancerBindings.OAS_ENHANCER_SERVICE);
|
146 | }
|
147 | _setupRequestHandlerIfNeeded() {
|
148 | if (this._expressApp != null)
|
149 | return;
|
150 | this._expressApp = (0, express_2.default)();
|
151 | this._applyExpressSettings();
|
152 | this._requestHandler = this._expressApp;
|
153 |
|
154 |
|
155 | this.expressMiddleware(cors_1.default, this.config.cors, {
|
156 | injectConfiguration: false,
|
157 | key: 'middleware.cors',
|
158 | group: sequence_1.RestMiddlewareGroups.CORS,
|
159 | }).apply((0, core_1.extensionFor)(keys_1.RestTags.REST_MIDDLEWARE_CHAIN, keys_1.RestTags.ACTION_MIDDLEWARE_CHAIN));
|
160 |
|
161 | this._setupOpenApiSpecEndpoints();
|
162 |
|
163 | this._expressApp.use(this._basePath, (req, res, next) => {
|
164 | this._handleHttpRequest(req, res).catch(next);
|
165 | });
|
166 |
|
167 | this._expressApp.use(this._unexpectedErrorHandler());
|
168 | }
|
169 | |
170 |
|
171 |
|
172 | _unexpectedErrorHandler() {
|
173 | const handleUnExpectedError = (err, req, res, next) => {
|
174 |
|
175 |
|
176 | this.get(SequenceActions.REJECT, { optional: true })
|
177 | .then(reject => {
|
178 | if (reject) {
|
179 |
|
180 |
|
181 | return reject({ request: req, response: res }, err);
|
182 | }
|
183 |
|
184 | (0, strong_error_handler_1.writeErrorToResponse)(err, req, res);
|
185 | })
|
186 | .catch(unexpectedErr => next(unexpectedErr));
|
187 | };
|
188 | return handleUnExpectedError;
|
189 | }
|
190 | |
191 |
|
192 |
|
193 | _applyExpressSettings() {
|
194 | assertExists(this._expressApp, 'this._expressApp');
|
195 | const settings = this.config.expressSettings;
|
196 | for (const key in settings) {
|
197 | this._expressApp.set(key, settings[key]);
|
198 | }
|
199 | if (this.config.router && typeof this.config.router.strict === 'boolean') {
|
200 | this._expressApp.set('strict routing', this.config.router.strict);
|
201 | }
|
202 | }
|
203 | |
204 |
|
205 |
|
206 |
|
207 | _setupOpenApiSpecEndpoints() {
|
208 | assertExists(this._expressApp, 'this._expressApp');
|
209 | if (this.config.openApiSpec.disabled)
|
210 | return;
|
211 | const router = express_2.default.Router();
|
212 | const mapping = this.config.openApiSpec.endpointMapping;
|
213 |
|
214 | for (const p in mapping) {
|
215 | this.addOpenApiSpecEndpoint(p, mapping[p], router);
|
216 | }
|
217 | const explorerPaths = ['/swagger-ui', '/explorer'];
|
218 | router.get(explorerPaths, (req, res, next) => this._redirectToSwaggerUI(req, res, next));
|
219 | this.expressMiddleware('middleware.apiSpec.defaults', router, {
|
220 | group: sequence_1.RestMiddlewareGroups.API_SPEC,
|
221 | upstreamGroups: sequence_1.RestMiddlewareGroups.CORS,
|
222 | }).apply((0, core_1.extensionFor)(keys_1.RestTags.REST_MIDDLEWARE_CHAIN, keys_1.RestTags.ACTION_MIDDLEWARE_CHAIN));
|
223 | }
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | addOpenApiSpecEndpoint(path, form, router) {
|
231 | if (router == null) {
|
232 | const key = `middleware.apiSpec.${path}.${form}`;
|
233 | if (this.contains(key)) {
|
234 | throw new Error(`The path ${path} is already configured for OpenApi hosting`);
|
235 | }
|
236 | const newRouter = express_2.default.Router();
|
237 | newRouter.get(path, (req, res) => this._serveOpenApiSpec(req, res, form));
|
238 | this.expressMiddleware(() => newRouter, {}, {
|
239 | injectConfiguration: false,
|
240 | key: `middleware.apiSpec.${path}.${form}`,
|
241 | group: 'apiSpec',
|
242 | });
|
243 | }
|
244 | else {
|
245 | router.get(path, (req, res) => this._serveOpenApiSpec(req, res, form));
|
246 | }
|
247 | }
|
248 | _handleHttpRequest(request, response) {
|
249 | return this.httpHandler.handleRequest(request, response);
|
250 | }
|
251 | _setupHandlerIfNeeded() {
|
252 | if (this._httpHandler)
|
253 | return;
|
254 |
|
255 |
|
256 | const routesObserver = {
|
257 | filter: binding => (0, core_1.filterByKey)(keys_1.RestBindings.API_SPEC.key)(binding) ||
|
258 | ((0, core_1.filterByKey)(/^(controllers|routes)\..+/)(binding) &&
|
259 |
|
260 | !(0, core_1.filterByTag)(keys_1.RestTags.CONTROLLER_ROUTE)(binding)),
|
261 | observe: () => {
|
262 |
|
263 |
|
264 | this._createHttpHandler();
|
265 | },
|
266 | };
|
267 | this._routesEventSubscription = this.subscribe(routesObserver);
|
268 | this._createHttpHandler();
|
269 | }
|
270 | |
271 |
|
272 |
|
273 | _createHttpHandler() {
|
274 | |
275 |
|
276 |
|
277 | const router = this.getSync(keys_1.RestBindings.ROUTER, { optional: true });
|
278 | const routingTable = new router_1.RoutingTable(router, this._externalRoutes);
|
279 | this._httpHandler = new http_handler_1.HttpHandler(this, this.config, routingTable);
|
280 |
|
281 | for (const b of this.findByTag(keys_1.RestTags.CONTROLLER_ROUTE)) {
|
282 | this.unbind(b.key);
|
283 | }
|
284 | for (const b of this.find(`${core_1.CoreBindings.CONTROLLERS}.*`)) {
|
285 | const controllerName = b.key.replace(/^controllers\./, '');
|
286 | const ctor = b.valueConstructor;
|
287 | if (!ctor) {
|
288 | throw new Error(`The controller ${controllerName} was not bound via .toClass()`);
|
289 | }
|
290 | const apiSpec = (0, openapi_v3_1.getControllerSpec)(ctor);
|
291 | if (!apiSpec) {
|
292 |
|
293 | debug('Skipping controller %s - no API spec provided', controllerName);
|
294 | continue;
|
295 | }
|
296 | debug('Registering controller %s', controllerName);
|
297 | if (apiSpec.components) {
|
298 | this._httpHandler.registerApiComponents(apiSpec.components);
|
299 | }
|
300 | const controllerFactory = (0, router_1.createControllerFactoryForBinding)(b.key);
|
301 | const routes = (0, router_1.createRoutesForController)(apiSpec, ctor, controllerFactory);
|
302 | for (const route of routes) {
|
303 | const binding = this.bindRoute(route);
|
304 | binding
|
305 | .tag(keys_1.RestTags.CONTROLLER_ROUTE)
|
306 | .tag({ [keys_1.RestTags.CONTROLLER_BINDING]: b.key });
|
307 | }
|
308 | }
|
309 | for (const b of this.findByTag(keys_1.RestTags.REST_ROUTE)) {
|
310 |
|
311 | const route = this.getSync(b.key);
|
312 | this._httpHandler.registerRoute(route);
|
313 | }
|
314 |
|
315 | const spec = this.getSync(keys_1.RestBindings.API_SPEC);
|
316 | if (spec.components) {
|
317 | this._httpHandler.registerApiComponents(spec.components);
|
318 | }
|
319 | for (const path in spec.paths) {
|
320 | for (const verb in spec.paths[path]) {
|
321 | const routeSpec = spec.paths[path][verb];
|
322 | this._setupOperation(verb, path, routeSpec);
|
323 | }
|
324 | }
|
325 | }
|
326 | _setupOperation(verb, path, spec) {
|
327 | const handler = spec['x-operation'];
|
328 | if (typeof handler === 'function') {
|
329 |
|
330 |
|
331 |
|
332 | spec = Object.assign({}, spec);
|
333 | delete spec['x-operation'];
|
334 | const route = new router_1.Route(verb, path, spec, handler);
|
335 | this._httpHandler.registerRoute(route);
|
336 | return;
|
337 | }
|
338 | const controllerName = spec['x-controller-name'];
|
339 | if (typeof controllerName === 'string') {
|
340 | const b = this.getBinding(`controllers.${controllerName}`, {
|
341 | optional: true,
|
342 | });
|
343 | if (!b) {
|
344 | throw new Error(`Unknown controller ${controllerName} used by "${verb} ${path}"`);
|
345 | }
|
346 | const ctor = b.valueConstructor;
|
347 | if (!ctor) {
|
348 | throw new Error(`The controller ${controllerName} was not bound via .toClass()`);
|
349 | }
|
350 | const controllerFactory = (0, router_1.createControllerFactoryForBinding)(b.key);
|
351 | const route = new router_1.ControllerRoute(verb, path, spec, ctor, controllerFactory);
|
352 | this._httpHandler.registerRoute(route);
|
353 | return;
|
354 | }
|
355 | throw new Error(`There is no handler configured for operation "${verb} ${path}`);
|
356 | }
|
357 | async _serveOpenApiSpec(request, response, specForm) {
|
358 | const requestContext = new request_context_1.RequestContext(request, response, this, this.config);
|
359 | specForm = specForm !== null && specForm !== void 0 ? specForm : { version: '3.0.0', format: 'json' };
|
360 | const specObj = await this.getApiSpec(requestContext);
|
361 | if (specForm.format === 'json') {
|
362 | const spec = JSON.stringify(specObj, null, 2);
|
363 | response.setHeader('content-type', 'application/json; charset=utf-8');
|
364 | response.end(spec, 'utf-8');
|
365 | }
|
366 | else {
|
367 | const yaml = (0, js_yaml_1.dump)(specObj, {});
|
368 | response.setHeader('content-type', 'text/yaml; charset=utf-8');
|
369 | response.end(yaml, 'utf-8');
|
370 | }
|
371 | }
|
372 | async _redirectToSwaggerUI(request, response, next) {
|
373 | const config = this.config.apiExplorer;
|
374 | if (config.disabled) {
|
375 | debug('Redirect to swagger-ui was disabled by configuration.');
|
376 | next();
|
377 | return;
|
378 | }
|
379 | debug('Redirecting to swagger-ui from %j.', request.originalUrl);
|
380 | const requestContext = new request_context_1.RequestContext(request, response, this, this.config);
|
381 | const protocol = requestContext.requestedProtocol;
|
382 | const baseUrl = protocol === 'http' ? config.httpUrl : config.url;
|
383 | const openApiUrl = `${requestContext.requestedBaseUrl}/openapi.json`;
|
384 | const fullUrl = `${baseUrl}?url=${openApiUrl}`;
|
385 | response.redirect(302, fullUrl);
|
386 | }
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 | controller(controllerCtor) {
|
405 | return this.bind('controllers.' + controllerCtor.name).toClass(controllerCtor);
|
406 | }
|
407 | route(routeOrVerb, path, spec, controllerCtorOrHandler, controllerFactory, methodName) {
|
408 | if (typeof routeOrVerb === 'object') {
|
409 | const r = routeOrVerb;
|
410 |
|
411 | return this.bindRoute(r);
|
412 | }
|
413 | if (!path) {
|
414 | throw new assert_1.AssertionError({
|
415 | message: 'path is required for a controller-based route',
|
416 | });
|
417 | }
|
418 | if (!spec) {
|
419 | throw new assert_1.AssertionError({
|
420 | message: 'spec is required for a controller-based route',
|
421 | });
|
422 | }
|
423 | if (arguments.length === 4) {
|
424 | if (!controllerCtorOrHandler) {
|
425 | throw new assert_1.AssertionError({
|
426 | message: 'handler function is required for a handler-based route',
|
427 | });
|
428 | }
|
429 | return this.route(new router_1.Route(routeOrVerb, path, spec, controllerCtorOrHandler));
|
430 | }
|
431 | if (!controllerCtorOrHandler) {
|
432 | throw new assert_1.AssertionError({
|
433 | message: 'controller is required for a controller-based route',
|
434 | });
|
435 | }
|
436 | if (!methodName) {
|
437 | throw new assert_1.AssertionError({
|
438 | message: 'methodName is required for a controller-based route',
|
439 | });
|
440 | }
|
441 | return this.route(new router_1.ControllerRoute(routeOrVerb, path, spec, controllerCtorOrHandler, controllerFactory, methodName));
|
442 | }
|
443 | bindRoute(r) {
|
444 | const namespace = keys_1.RestBindings.ROUTES;
|
445 | const encodedPath = encodeURIComponent(r.path).replace(/\./g, '%2E');
|
446 | return this.bind(`${namespace}.${r.verb} ${encodedPath}`)
|
447 | .to(r)
|
448 | .tag(keys_1.RestTags.REST_ROUTE)
|
449 | .tag({ [keys_1.RestTags.ROUTE_VERB]: r.verb, [keys_1.RestTags.ROUTE_PATH]: r.path });
|
450 | }
|
451 | |
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | redirect(fromPath, toPathOrUrl, statusCode) {
|
467 | return this.route(new router_1.RedirectRoute(fromPath, this._basePath + toPathOrUrl, statusCode));
|
468 | }
|
469 | |
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 | static(path, rootDir, options) {
|
478 | this._externalRoutes.registerAssets(path, rootDir, options);
|
479 | }
|
480 | |
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 | api(spec) {
|
493 | return this.bind(keys_1.RestBindings.API_SPEC).to(spec);
|
494 | }
|
495 | |
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 | async getApiSpec(requestContext) {
|
517 | let spec = await this.get(keys_1.RestBindings.API_SPEC);
|
518 | spec = (0, lodash_1.cloneDeep)(spec);
|
519 | const components = this.httpHandler.getApiComponents();
|
520 |
|
521 |
|
522 | const paths = (0, lodash_1.cloneDeep)(this.httpHandler.describeApiPaths());
|
523 | spec.paths = { ...paths, ...spec.paths };
|
524 | if (components) {
|
525 | const defs = (0, lodash_1.cloneDeep)(components);
|
526 | spec.components = { ...spec.components, ...defs };
|
527 | }
|
528 | (0, router_spec_1.assignRouterSpec)(spec, this._externalRoutes.routerSpec);
|
529 | if (requestContext) {
|
530 | spec = this.updateSpecFromRequest(spec, requestContext);
|
531 | }
|
532 |
|
533 | this.OASEnhancer.spec = spec;
|
534 | spec = await this.OASEnhancer.applyAllEnhancers();
|
535 | return spec;
|
536 | }
|
537 | |
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 | updateSpecFromRequest(spec, requestContext) {
|
547 | if (this.config.openApiSpec.setServersFromRequest) {
|
548 | spec = Object.assign({}, spec);
|
549 | spec.servers = [{ url: requestContext.requestedBaseUrl }];
|
550 | }
|
551 | const basePath = requestContext.basePath;
|
552 | if (spec.servers && basePath) {
|
553 | for (const s of spec.servers) {
|
554 |
|
555 | if (s.url === '/') {
|
556 | s.url = basePath;
|
557 | }
|
558 | }
|
559 | }
|
560 | return spec;
|
561 | }
|
562 | |
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 | sequence(sequenceClass) {
|
581 | const sequenceBinding = (0, core_1.createBindingFromClass)(sequenceClass, {
|
582 | key: keys_1.RestBindings.SEQUENCE,
|
583 | });
|
584 | this.add(sequenceBinding);
|
585 | return sequenceBinding;
|
586 | }
|
587 | |
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 | handler(handlerFn) {
|
600 | class SequenceFromFunction extends sequence_1.DefaultSequence {
|
601 | async handle(context) {
|
602 | return handlerFn(context, this);
|
603 | }
|
604 | }
|
605 | this.sequence(SequenceFromFunction);
|
606 | }
|
607 | |
608 |
|
609 |
|
610 |
|
611 |
|
612 | bodyParser(bodyParserClass, address) {
|
613 | const binding = createBodyParserBinding(bodyParserClass, address);
|
614 | this.add(binding);
|
615 | return binding;
|
616 | }
|
617 | |
618 |
|
619 |
|
620 |
|
621 | basePath(path = '') {
|
622 | if (this._requestHandler != null) {
|
623 | throw new Error('Base path cannot be set as the request handler has been created');
|
624 | }
|
625 |
|
626 | path = path.replace(/(^\/)|(\/$)/, '');
|
627 | if (path)
|
628 | path = '/' + path;
|
629 | this._basePath = path;
|
630 | this.config.basePath = path;
|
631 | }
|
632 | |
633 |
|
634 |
|
635 | async start() {
|
636 |
|
637 | this._setupRequestHandlerIfNeeded();
|
638 |
|
639 |
|
640 | this._setupHandlerIfNeeded();
|
641 | const port = await this.get(keys_1.RestBindings.PORT);
|
642 | const host = await this.get(keys_1.RestBindings.HOST);
|
643 | const path = await this.get(keys_1.RestBindings.PATH);
|
644 | const protocol = await this.get(keys_1.RestBindings.PROTOCOL);
|
645 | const httpsOptions = await this.get(keys_1.RestBindings.HTTPS_OPTIONS);
|
646 | if (this.config.listenOnStart === false) {
|
647 | debug('RestServer is not listening as listenOnStart flag is set to false.');
|
648 | return;
|
649 | }
|
650 | const serverOptions = { ...httpsOptions, port, host, protocol, path };
|
651 | this._httpServer = new http_server_1.HttpServer(this.requestHandler, serverOptions);
|
652 | await this._httpServer.start();
|
653 | this.bind(keys_1.RestBindings.PORT).to(this._httpServer.port);
|
654 | this.bind(keys_1.RestBindings.HOST).to(this._httpServer.host);
|
655 | this.bind(keys_1.RestBindings.URL).to(this._httpServer.url);
|
656 | debug('RestServer listening at %s', this._httpServer.url);
|
657 | }
|
658 | |
659 |
|
660 |
|
661 | async stop() {
|
662 |
|
663 | if (!this._httpServer)
|
664 | return;
|
665 | await this._httpServer.stop();
|
666 | this._httpServer = undefined;
|
667 | }
|
668 | |
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 | mountExpressRouter(basePath, router, spec) {
|
680 | this._externalRoutes.mountRouter(basePath, router, spec);
|
681 | }
|
682 | |
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 | async exportOpenApiSpec(outFile = '', log = console.log) {
|
693 | const spec = await this.getApiSpec();
|
694 | if (outFile === '-' || outFile === '') {
|
695 | const json = JSON.stringify(spec, null, 2);
|
696 | log('%s', json);
|
697 | return;
|
698 | }
|
699 | const fileName = outFile.toLowerCase();
|
700 | if (fileName.endsWith('.yaml') || fileName.endsWith('.yml')) {
|
701 | const yaml = (0, js_yaml_1.dump)(spec);
|
702 | fs_1.default.writeFileSync(outFile, yaml, 'utf-8');
|
703 | }
|
704 | else {
|
705 | const json = JSON.stringify(spec, null, 2);
|
706 | fs_1.default.writeFileSync(outFile, json, 'utf-8');
|
707 | }
|
708 | log('The OpenAPI spec has been saved to %s.', outFile);
|
709 | }
|
710 | };
|
711 | RestServer = tslib_1.__decorate([
|
712 | tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
|
713 | tslib_1.__param(1, (0, core_1.inject)(keys_1.RestBindings.CONFIG, { optional: true })),
|
714 | tslib_1.__metadata("design:paramtypes", [core_1.Application, Object])
|
715 | ], RestServer);
|
716 | exports.RestServer = RestServer;
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 | function assertExists(val, name) {
|
724 | (0, assert_1.default)(val != null, `The value of ${name} cannot be null or undefined`);
|
725 | }
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 | function createBodyParserBinding(parserClass, key) {
|
732 | const address = key !== null && key !== void 0 ? key : `${keys_1.RestBindings.REQUEST_BODY_PARSER}.${parserClass.name}`;
|
733 | return core_1.Binding.bind(address)
|
734 | .toClass(parserClass)
|
735 | .inScope(core_1.BindingScope.TRANSIENT)
|
736 | .tag(body_parsers_1.REQUEST_BODY_PARSER_TAG);
|
737 | }
|
738 | exports.createBodyParserBinding = createBodyParserBinding;
|
739 | const OPENAPI_SPEC_MAPPING = {
|
740 | '/openapi.json': { version: '3.0.0', format: 'json' },
|
741 | '/openapi.yaml': { version: '3.0.0', format: 'yaml' },
|
742 | };
|
743 | const DEFAULT_CONFIG = {
|
744 | port: 3000,
|
745 | openApiSpec: {},
|
746 | apiExplorer: {},
|
747 | cors: {
|
748 | origin: '*',
|
749 | methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
750 | preflightContinue: false,
|
751 | optionsSuccessStatus: 204,
|
752 | maxAge: 86400,
|
753 | credentials: true,
|
754 | },
|
755 | expressSettings: {},
|
756 | router: {},
|
757 | listenOnStart: true,
|
758 | };
|
759 | function resolveRestServerConfig(config) {
|
760 | const result = Object.assign((0, lodash_1.cloneDeep)(DEFAULT_CONFIG), config);
|
761 |
|
762 | if (result.port == null) {
|
763 | result.port = 3000;
|
764 | }
|
765 | if (result.host == null) {
|
766 |
|
767 | result.host = undefined;
|
768 | }
|
769 | if (!result.openApiSpec.endpointMapping) {
|
770 |
|
771 |
|
772 | result.openApiSpec.endpointMapping = (0, lodash_1.cloneDeep)(OPENAPI_SPEC_MAPPING);
|
773 | }
|
774 | result.apiExplorer = normalizeApiExplorerConfig(config.apiExplorer);
|
775 | if (result.openApiSpec.disabled) {
|
776 |
|
777 | result.apiExplorer.disabled = true;
|
778 | }
|
779 | return result;
|
780 | }
|
781 | function normalizeApiExplorerConfig(input) {
|
782 | var _a, _b, _c;
|
783 | const config = input !== null && input !== void 0 ? input : {};
|
784 | const url = (_a = config.url) !== null && _a !== void 0 ? _a : 'https://explorer.loopback.io';
|
785 | config.httpUrl =
|
786 | (_c = (_b = config.httpUrl) !== null && _b !== void 0 ? _b : config.url) !== null && _c !== void 0 ? _c : 'http://explorer.loopback.io';
|
787 | config.url = url;
|
788 | return config;
|
789 | }
|
790 |
|
\ | No newline at end of file |