1 | ;
|
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
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.RestApplication = exports.SequenceActions = exports.ERR_NO_MULTI_SERVER = void 0;
|
8 | const core_1 = require("@loopback/core");
|
9 | const util_1 = require("util");
|
10 | const keys_1 = require("./keys");
|
11 | const rest_component_1 = require("./rest.component");
|
12 | exports.ERR_NO_MULTI_SERVER = (0, util_1.format)('RestApplication does not support multiple servers!', 'To create your own server bindings, please extend the Application class.');
|
13 | // To help cut down on verbosity!
|
14 | exports.SequenceActions = keys_1.RestBindings.SequenceActions;
|
15 | /**
|
16 | * An implementation of the Application class that automatically provides
|
17 | * an instance of a REST server. This application class is intended to be
|
18 | * a single-server implementation. Any attempt to bind additional servers
|
19 | * will throw an error.
|
20 | *
|
21 | */
|
22 | class RestApplication extends core_1.Application {
|
23 | /**
|
24 | * The main REST server instance providing REST API for this application.
|
25 | */
|
26 | get restServer() {
|
27 | // FIXME(kjdelisle): I attempted to mimic the pattern found in RestServer
|
28 | // with no success, so until I've got a better way, this is functional.
|
29 | return this.getSync('servers.RestServer');
|
30 | }
|
31 | /**
|
32 | * Handle incoming HTTP(S) request by invoking the corresponding
|
33 | * Controller method via the configured Sequence.
|
34 | *
|
35 | * @example
|
36 | *
|
37 | * ```ts
|
38 | * const app = new RestApplication();
|
39 | * // setup controllers, etc.
|
40 | *
|
41 | * const server = http.createServer(app.requestHandler);
|
42 | * server.listen(3000);
|
43 | * ```
|
44 | *
|
45 | * @param req - The request.
|
46 | * @param res - The response.
|
47 | */
|
48 | get requestHandler() {
|
49 | return this.restServer.requestHandler;
|
50 | }
|
51 | constructor(configOrParent, parent) {
|
52 | super(configOrParent, parent);
|
53 | this.component(rest_component_1.RestComponent);
|
54 | }
|
55 | server(server, name) {
|
56 | if (this.findByTag('server').length > 0) {
|
57 | throw new Error(exports.ERR_NO_MULTI_SERVER);
|
58 | }
|
59 | return super.server(server, name);
|
60 | }
|
61 | sequence(sequence) {
|
62 | return this.restServer.sequence(sequence);
|
63 | }
|
64 | handler(handlerFn) {
|
65 | this.restServer.handler(handlerFn);
|
66 | }
|
67 | /**
|
68 | * Mount static assets to the REST server.
|
69 | * See https://expressjs.com/en/4x/api.html#express.static
|
70 | * @param path - The path(s) to serve the asset.
|
71 | * See examples at https://expressjs.com/en/4x/api.html#path-examples
|
72 | * To avoid performance penalty, `/` is not allowed for now.
|
73 | * @param rootDir - The root directory from which to serve static assets
|
74 | * @param options - Options for serve-static
|
75 | */
|
76 | static(path, rootDir, options) {
|
77 | this.restServer.static(path, rootDir, options);
|
78 | }
|
79 | /**
|
80 | * Bind a body parser to the server context
|
81 | * @param parserClass - Body parser class
|
82 | * @param address - Optional binding address
|
83 | */
|
84 | bodyParser(bodyParserClass, address) {
|
85 | return this.restServer.bodyParser(bodyParserClass, address);
|
86 | }
|
87 | /**
|
88 | * Configure the `basePath` for the rest server
|
89 | * @param path - Base path
|
90 | */
|
91 | basePath(path = '') {
|
92 | this.restServer.basePath(path);
|
93 | }
|
94 | expressMiddleware(factoryOrKey, configOrHandlers, options = {}) {
|
95 | return this.restServer.expressMiddleware(factoryOrKey, configOrHandlers, options);
|
96 | }
|
97 | /**
|
98 | * Register a middleware function or provider class
|
99 | *
|
100 | * @example
|
101 | * ```ts
|
102 | * const log: Middleware = async (requestCtx, next) {
|
103 | * // ...
|
104 | * }
|
105 | * server.middleware(log);
|
106 | * ```
|
107 | *
|
108 | * @param middleware - Middleware function or provider class
|
109 | * @param options - Middleware binding options
|
110 | */
|
111 | middleware(middleware, options = {}) {
|
112 | return this.restServer.middleware(middleware, options);
|
113 | }
|
114 | route(routeOrVerb, path, spec, controllerCtorOrHandler, controllerFactory, methodName) {
|
115 | const server = this.restServer;
|
116 | if (typeof routeOrVerb === 'object') {
|
117 | return server.route(routeOrVerb);
|
118 | }
|
119 | else if (arguments.length === 4) {
|
120 | return server.route(routeOrVerb, path, spec, controllerCtorOrHandler);
|
121 | }
|
122 | else {
|
123 | return server.route(routeOrVerb, path, spec, controllerCtorOrHandler, controllerFactory, methodName);
|
124 | }
|
125 | }
|
126 | /**
|
127 | * Register a route redirecting callers to a different URL.
|
128 | *
|
129 | * @example
|
130 | * ```ts
|
131 | * app.redirect('/explorer', '/explorer/');
|
132 | * ```
|
133 | *
|
134 | * @param fromPath - URL path of the redirect endpoint
|
135 | * @param toPathOrUrl - Location (URL path or full URL) where to redirect to.
|
136 | * If your server is configured with a custom `basePath`, then the base path
|
137 | * is prepended to the target location.
|
138 | * @param statusCode - HTTP status code to respond with,
|
139 | * defaults to 303 (See Other).
|
140 | */
|
141 | redirect(fromPath, toPathOrUrl, statusCode) {
|
142 | return this.restServer.redirect(fromPath, toPathOrUrl, statusCode);
|
143 | }
|
144 | /**
|
145 | * Set the OpenAPI specification that defines the REST API schema for this
|
146 | * application. All routes, parameter definitions and return types will be
|
147 | * defined in this way.
|
148 | *
|
149 | * Note that this will override any routes defined via decorators at the
|
150 | * controller level (this function takes precedent).
|
151 | *
|
152 | * @param spec - The OpenAPI specification, as an object.
|
153 | * @returns Binding for the api spec
|
154 | */
|
155 | api(spec) {
|
156 | return this.restServer.bind(keys_1.RestBindings.API_SPEC).to(spec);
|
157 | }
|
158 | /**
|
159 | * Mount an Express router to expose additional REST endpoints handled
|
160 | * via legacy Express-based stack.
|
161 | *
|
162 | * @param basePath - Path where to mount the router at, e.g. `/` or `/api`.
|
163 | * @param router - The Express router to handle the requests.
|
164 | * @param spec - A partial OpenAPI spec describing endpoints provided by the
|
165 | * router. LoopBack will prepend `basePath` to all endpoints automatically.
|
166 | * This argument is optional. You can leave it out if you don't want to
|
167 | * document the routes.
|
168 | */
|
169 | mountExpressRouter(basePath, router, spec) {
|
170 | this.restServer.mountExpressRouter(basePath, router, spec);
|
171 | }
|
172 | /**
|
173 | * Export the OpenAPI spec to the given json or yaml file
|
174 | * @param outFile - File name for the spec. The extension of the file
|
175 | * determines the format of the file.
|
176 | * - `yaml` or `yml`: YAML
|
177 | * - `json` or other: JSON
|
178 | * If the outFile is not provided or its value is `''` or `'-'`, the spec is
|
179 | * written to the console using the `log` function.
|
180 | * @param log - Log function, default to `console.log`
|
181 | */
|
182 | async exportOpenApiSpec(outFile = '', log = console.log) {
|
183 | return this.restServer.exportOpenApiSpec(outFile, log);
|
184 | }
|
185 | }
|
186 | exports.RestApplication = RestApplication;
|
187 | //# sourceMappingURL=rest.application.js.map |
\ | No newline at end of file |