UNPKG

9.72 kBTypeScriptView Raw
1import { Application, ApplicationConfig, Binding, BindingAddress, Constructor, Context, Provider, Server } from '@loopback/core';
2import { ExpressMiddlewareFactory, ExpressRequestHandler, Middleware, MiddlewareBindingOptions } from '@loopback/express';
3import { OpenApiSpec, OperationObject } from '@loopback/openapi-v3';
4import { PathParams } from 'express-serve-static-core';
5import { ServeStaticOptions } from 'serve-static';
6import { BodyParser } from './body-parsers';
7import { RestBindings } from './keys';
8import { HttpRequestListener, HttpServerLike, RestServer } from './rest.server';
9import { ControllerClass, ControllerFactory, RouteEntry } from './router';
10import { RouterSpec } from './router/router-spec';
11import { SequenceFunction, SequenceHandler } from './sequence';
12export declare const ERR_NO_MULTI_SERVER: string;
13export declare const SequenceActions: typeof RestBindings.SequenceActions;
14/**
15 * An implementation of the Application class that automatically provides
16 * an instance of a REST server. This application class is intended to be
17 * a single-server implementation. Any attempt to bind additional servers
18 * will throw an error.
19 *
20 */
21export declare class RestApplication extends Application implements HttpServerLike {
22 /**
23 * The main REST server instance providing REST API for this application.
24 */
25 get restServer(): RestServer;
26 /**
27 * Handle incoming HTTP(S) request by invoking the corresponding
28 * Controller method via the configured Sequence.
29 *
30 * @example
31 *
32 * ```ts
33 * const app = new RestApplication();
34 * // setup controllers, etc.
35 *
36 * const server = http.createServer(app.requestHandler);
37 * server.listen(3000);
38 * ```
39 *
40 * @param req - The request.
41 * @param res - The response.
42 */
43 get requestHandler(): HttpRequestListener;
44 /**
45 * Create a REST application with the given parent context
46 * @param parent - Parent context
47 */
48 constructor(parent: Context);
49 /**
50 * Create a REST application with the given configuration and parent context
51 * @param config - Application configuration
52 * @param parent - Parent context
53 */
54 constructor(config?: ApplicationConfig, parent?: Context);
55 server(server: Constructor<Server>, name?: string): Binding;
56 sequence(sequence: Constructor<SequenceHandler>): Binding;
57 handler(handlerFn: SequenceFunction): void;
58 /**
59 * Mount static assets to the REST server.
60 * See https://expressjs.com/en/4x/api.html#express.static
61 * @param path - The path(s) to serve the asset.
62 * See examples at https://expressjs.com/en/4x/api.html#path-examples
63 * To avoid performance penalty, `/` is not allowed for now.
64 * @param rootDir - The root directory from which to serve static assets
65 * @param options - Options for serve-static
66 */
67 static(path: PathParams, rootDir: string, options?: ServeStaticOptions): void;
68 /**
69 * Bind a body parser to the server context
70 * @param parserClass - Body parser class
71 * @param address - Optional binding address
72 */
73 bodyParser(bodyParserClass: Constructor<BodyParser>, address?: BindingAddress<BodyParser>): Binding<BodyParser>;
74 /**
75 * Configure the `basePath` for the rest server
76 * @param path - Base path
77 */
78 basePath(path?: string): void;
79 /**
80 * Bind an Express middleware to this server context
81 *
82 * @example
83 * ```ts
84 * import myExpressMiddlewareFactory from 'my-express-middleware';
85 * const myExpressMiddlewareConfig= {};
86 * const myExpressMiddleware = myExpressMiddlewareFactory(myExpressMiddlewareConfig);
87 * server.expressMiddleware('middleware.express.my', myExpressMiddleware);
88 * ```
89 * @param key - Middleware binding key
90 * @param middleware - Express middleware handler function(s)
91 *
92 */
93 expressMiddleware(key: BindingAddress, middleware: ExpressRequestHandler | ExpressRequestHandler[], options?: MiddlewareBindingOptions): Binding<Middleware>;
94 /**
95 * Bind an Express middleware to this server context
96 *
97 * @example
98 * ```ts
99 * import myExpressMiddlewareFactory from 'my-express-middleware';
100 * const myExpressMiddlewareConfig= {};
101 * server.expressMiddleware(myExpressMiddlewareFactory, myExpressMiddlewareConfig);
102 * ```
103 * @param middlewareFactory - Middleware module name or factory function
104 * @param middlewareConfig - Middleware config
105 * @param options - Options for registration
106 *
107 * @typeParam CFG - Configuration type
108 */
109 expressMiddleware<CFG>(middlewareFactory: ExpressMiddlewareFactory<CFG>, middlewareConfig?: CFG, options?: MiddlewareBindingOptions): Binding<Middleware>;
110 /**
111 * Register a middleware function or provider class
112 *
113 * @example
114 * ```ts
115 * const log: Middleware = async (requestCtx, next) {
116 * // ...
117 * }
118 * server.middleware(log);
119 * ```
120 *
121 * @param middleware - Middleware function or provider class
122 * @param options - Middleware binding options
123 */
124 middleware(middleware: Middleware | Constructor<Provider<Middleware>>, options?: MiddlewareBindingOptions): Binding<Middleware>;
125 /**
126 * Register a new Controller-based route.
127 *
128 * @example
129 * ```ts
130 * class MyController {
131 * greet(name: string) {
132 * return `hello ${name}`;
133 * }
134 * }
135 * app.route('get', '/greet', operationSpec, MyController, 'greet');
136 * ```
137 *
138 * @param verb - HTTP verb of the endpoint
139 * @param path - URL path of the endpoint
140 * @param spec - The OpenAPI spec describing the endpoint (operation)
141 * @param controllerCtor - Controller constructor
142 * @param controllerFactory - A factory function to create controller instance
143 * @param methodName - The name of the controller method
144 */
145 route<T extends object>(verb: string, path: string, spec: OperationObject, controllerCtor: ControllerClass<T>, controllerFactory: ControllerFactory<T>, methodName: string): Binding;
146 /**
147 * Register a new route invoking a handler function.
148 *
149 * @example
150 * ```ts
151 * function greet(name: string) {
152 * return `hello ${name}`;
153 * }
154 * app.route('get', '/', operationSpec, greet);
155 * ```
156 *
157 * @param verb - HTTP verb of the endpoint
158 * @param path - URL path of the endpoint
159 * @param spec - The OpenAPI spec describing the endpoint (operation)
160 * @param handler - The function to invoke with the request parameters
161 * described in the spec.
162 */
163 route(verb: string, path: string, spec: OperationObject, handler: Function): Binding;
164 /**
165 * Register a new route.
166 *
167 * @example
168 * ```ts
169 * function greet(name: string) {
170 * return `hello ${name}`;
171 * }
172 * const route = new Route('get', '/', operationSpec, greet);
173 * app.route(route);
174 * ```
175 *
176 * @param route - The route to add.
177 */
178 route(route: RouteEntry): Binding;
179 /**
180 * Register a new route.
181 *
182 * @example
183 * ```ts
184 * function greet(name: string) {
185 * return `hello ${name}`;
186 * }
187 * app.route('get', '/', operationSpec, greet);
188 * ```
189 */
190 route(verb: string, path: string, spec: OperationObject, handler: Function): Binding;
191 /**
192 * Register a route redirecting callers to a different URL.
193 *
194 * @example
195 * ```ts
196 * app.redirect('/explorer', '/explorer/');
197 * ```
198 *
199 * @param fromPath - URL path of the redirect endpoint
200 * @param toPathOrUrl - Location (URL path or full URL) where to redirect to.
201 * If your server is configured with a custom `basePath`, then the base path
202 * is prepended to the target location.
203 * @param statusCode - HTTP status code to respond with,
204 * defaults to 303 (See Other).
205 */
206 redirect(fromPath: string, toPathOrUrl: string, statusCode?: number): Binding;
207 /**
208 * Set the OpenAPI specification that defines the REST API schema for this
209 * application. All routes, parameter definitions and return types will be
210 * defined in this way.
211 *
212 * Note that this will override any routes defined via decorators at the
213 * controller level (this function takes precedent).
214 *
215 * @param spec - The OpenAPI specification, as an object.
216 * @returns Binding for the api spec
217 */
218 api(spec: OpenApiSpec): Binding;
219 /**
220 * Mount an Express router to expose additional REST endpoints handled
221 * via legacy Express-based stack.
222 *
223 * @param basePath - Path where to mount the router at, e.g. `/` or `/api`.
224 * @param router - The Express router to handle the requests.
225 * @param spec - A partial OpenAPI spec describing endpoints provided by the
226 * router. LoopBack will prepend `basePath` to all endpoints automatically.
227 * This argument is optional. You can leave it out if you don't want to
228 * document the routes.
229 */
230 mountExpressRouter(basePath: string, router: ExpressRequestHandler, spec?: RouterSpec): void;
231 /**
232 * Export the OpenAPI spec to the given json or yaml file
233 * @param outFile - File name for the spec. The extension of the file
234 * determines the format of the file.
235 * - `yaml` or `yml`: YAML
236 * - `json` or other: JSON
237 * If the outFile is not provided or its value is `''` or `'-'`, the spec is
238 * written to the console using the `log` function.
239 * @param log - Log function, default to `console.log`
240 */
241 exportOpenApiSpec(outFile?: string, log?: (message?: any, ...optionalParams: any[]) => void): Promise<void>;
242}
243
\No newline at end of file