UNPKG

8.99 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
2// Node module: @loopback/rest
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {BindingKey, Context, CoreBindings} from '@loopback/core';
7import {DEFAULT_MIDDLEWARE_CHAIN, InvokeMiddleware} from '@loopback/express';
8import {HttpProtocol} from '@loopback/http-server';
9import {OpenApiSpec, OperationObject} from '@loopback/openapi-v3';
10import https from 'https';
11import {ErrorWriterOptions} from 'strong-error-handler';
12import {BodyParser, RequestBodyParser} from './body-parsers';
13import {HttpHandler} from './http-handler';
14import {RestServer, RestServerConfig} from './rest.server';
15import {ResolvedRoute, RestRouter, RestRouterOptions} from './router';
16import {SequenceHandler} from './sequence';
17import {
18 AjvFactory,
19 FindRoute,
20 InvokeMethod,
21 LogError,
22 OperationArgs,
23 OperationRetval,
24 ParseParams,
25 Reject,
26 Request,
27 RequestBodyParserOptions,
28 Response,
29 Send,
30} from './types';
31
32/**
33 * RestServer-specific bindings
34 */
35export namespace RestBindings {
36 /**
37 * Binding key for setting and injecting RestComponentConfig
38 */
39 export const CONFIG: BindingKey<RestServerConfig> =
40 CoreBindings.APPLICATION_CONFIG.deepProperty('rest');
41 /**
42 * Binding key for setting and injecting the host name of RestServer
43 */
44 export const HOST = BindingKey.create<string | undefined>('rest.host');
45 /**
46 * Binding key for setting and injecting the port number of RestServer
47 */
48 export const PORT = BindingKey.create<number>('rest.port');
49 /**
50 * Binding key for setting and injecting the socket path of the RestServer
51 */
52 export const PATH = BindingKey.create<string | undefined>('rest.path');
53 /**
54 * Binding key for setting and injecting the URL of RestServer
55 */
56 export const URL = BindingKey.create<string>('rest.url');
57 /**
58 * Binding key for setting and injecting the protocol of RestServer
59 */
60 export const PROTOCOL = BindingKey.create<HttpProtocol>('rest.protocol');
61 /**
62 * Binding key for HTTPS options
63 */
64 export const HTTPS_OPTIONS =
65 BindingKey.create<https.ServerOptions>('rest.httpsOptions');
66
67 /**
68 * Binding key for the server itself
69 */
70 export const SERVER = BindingKey.create<RestServer>('servers.RestServer');
71
72 /**
73 * Internal binding key for basePath
74 */
75 export const BASE_PATH = BindingKey.create<string>('rest.basePath');
76
77 /**
78 * Internal binding key for http-handler
79 */
80 export const HANDLER = BindingKey.create<HttpHandler>('rest.handler');
81
82 /**
83 * Internal binding key for rest router
84 */
85 export const ROUTER = BindingKey.create<RestRouter>('rest.router');
86
87 export const ROUTER_OPTIONS = BindingKey.create<RestRouterOptions>(
88 'rest.router.options',
89 );
90
91 /**
92 * Binding key for setting and injecting Reject action's error handling
93 * options.
94 *
95 * See https://github.com/loopbackio/strong-error-handler#options for
96 * the list of available options. Please note that the flag `log` is not used
97 * by `@loopback/rest`.
98 */
99 export const ERROR_WRITER_OPTIONS = BindingKey.create<ErrorWriterOptions>(
100 'rest.errorWriterOptions',
101 );
102
103 /**
104 * Binding key for request body parser options
105 */
106 export const REQUEST_BODY_PARSER_OPTIONS =
107 BindingKey.create<RequestBodyParserOptions>(
108 'rest.requestBodyParserOptions',
109 );
110
111 /**
112 * Binding key for request body parser
113 */
114 export const REQUEST_BODY_PARSER = BindingKey.create<RequestBodyParser>(
115 'rest.requestBodyParser',
116 );
117
118 function bodyParserBindingKey(parser: string) {
119 return `${REQUEST_BODY_PARSER}.${parser}`;
120 }
121
122 /**
123 * Binding key for request json body parser
124 */
125 export const REQUEST_BODY_PARSER_JSON = BindingKey.create<BodyParser>(
126 bodyParserBindingKey('JsonBodyParser'),
127 );
128
129 /**
130 * Binding key for request urlencoded body parser
131 */
132 export const REQUEST_BODY_PARSER_URLENCODED = BindingKey.create<BodyParser>(
133 bodyParserBindingKey('UrlEncodedBodyParser'),
134 );
135
136 /**
137 * Binding key for request text body parser
138 */
139 export const REQUEST_BODY_PARSER_TEXT = BindingKey.create<BodyParser>(
140 bodyParserBindingKey('TextBodyParser'),
141 );
142
143 /**
144 * Binding key for request raw body parser
145 */
146 export const REQUEST_BODY_PARSER_RAW = BindingKey.create<BodyParser>(
147 bodyParserBindingKey('RawBodyParser'),
148 );
149
150 /**
151 * Binding key for request raw body parser
152 */
153 export const REQUEST_BODY_PARSER_STREAM = BindingKey.create<BodyParser>(
154 bodyParserBindingKey('StreamBodyParser'),
155 );
156
157 /**
158 * Binding key for AJV
159 */
160 export const AJV_FACTORY = BindingKey.create<AjvFactory>(
161 bodyParserBindingKey('rest.ajvFactory'),
162 );
163
164 /**
165 * Binding key for setting and injecting an OpenAPI spec
166 */
167 export const API_SPEC: BindingKey<OpenApiSpec> =
168 BindingKey.create<OpenApiSpec>('rest.apiSpec');
169
170 /**
171 * Binding key for setting and injecting an OpenAPI operation spec
172 */
173 export const OPERATION_SPEC_CURRENT = BindingKey.create<OperationObject>(
174 'rest.operationSpec.current',
175 );
176
177 /**
178 * Binding key for setting and injecting a Sequence
179 */
180 export const SEQUENCE = BindingKey.create<SequenceHandler>('rest.sequence');
181
182 /**
183 * Binding key for setting and injecting a `invokeMiddleware` function for
184 * middleware based sequence
185 */
186 export const INVOKE_MIDDLEWARE_SERVICE = BindingKey.create<InvokeMiddleware>(
187 'rest.invokeMiddleware',
188 );
189
190 /**
191 * Bindings for potential actions that could be used in a sequence
192 */
193 export namespace SequenceActions {
194 /**
195 * Binding key for setting and injecting `invokeMiddleware` function
196 */
197 export const INVOKE_MIDDLEWARE = BindingKey.create<InvokeMiddleware>(
198 'rest.sequence.actions.invokeMiddleware',
199 );
200 /**
201 * Binding key for setting and injecting a route finding function
202 */
203 export const FIND_ROUTE = BindingKey.create<FindRoute>(
204 'rest.sequence.actions.findRoute',
205 );
206 /**
207 * Binding key for setting and injecting a parameter parsing function
208 */
209 export const PARSE_PARAMS = BindingKey.create<ParseParams>(
210 'rest.sequence.actions.parseParams',
211 );
212 /**
213 * Binding key for setting and injecting a controller route invoking function
214 */
215 export const INVOKE_METHOD = BindingKey.create<InvokeMethod>(
216 'rest.sequence.actions.invokeMethod',
217 );
218 /**
219 * Binding key for setting and injecting an error logging function
220 */
221 export const LOG_ERROR = BindingKey.create<LogError>(
222 'rest.sequence.actions.logError',
223 );
224 /**
225 * Binding key for setting and injecting a response writing function
226 */
227 export const SEND = BindingKey.create<Send>('rest.sequence.actions.send');
228 /**
229 * Binding key for setting and injecting a bad response writing function
230 */
231 export const REJECT = BindingKey.create<Reject>(
232 'rest.sequence.actions.reject',
233 );
234 }
235
236 export namespace Operation {
237 export const ROUTE = BindingKey.create<ResolvedRoute>(
238 'rest.operation.route',
239 );
240
241 export const PARAMS = BindingKey.create<OperationArgs>(
242 'rest.operation.params',
243 );
244
245 export const RETURN_VALUE = BindingKey.create<OperationRetval>(
246 'rest.operation.returnValue',
247 );
248 }
249
250 /**
251 * Request-specific bindings
252 */
253 export namespace Http {
254 /**
255 * Binding key for setting and injecting the http request
256 */
257 export const REQUEST: BindingKey<Request> =
258 BindingKey.create<Request>('rest.http.request');
259 /**
260 * Binding key for setting and injecting the http response
261 */
262 export const RESPONSE = BindingKey.create<Response>('rest.http.response');
263 /**
264 * Binding key for setting and injecting the http request context
265 */
266 export const CONTEXT = BindingKey.create<Context>(
267 'rest.http.request.context',
268 );
269 }
270
271 /**
272 * Namespace for REST routes
273 */
274 export const ROUTES = 'routes';
275}
276
277/**
278 * Binding tags for RestServer
279 */
280export namespace RestTags {
281 /**
282 * Binding tag to identify REST routes
283 */
284 export const REST_ROUTE = 'restRoute';
285
286 /**
287 * Binding tag for the REST route verb
288 */
289 export const ROUTE_VERB = 'restRouteVerb';
290
291 /**
292 * Binding tag for the REST route path
293 */
294 export const ROUTE_PATH = 'restRoutePath';
295
296 /**
297 * Binding tag to identify controller based REST routes
298 */
299 export const CONTROLLER_ROUTE = 'controllerRoute';
300
301 /**
302 * Binding tag for controller route bindings to represent the controller
303 * binding key
304 */
305 export const CONTROLLER_BINDING = 'controllerBinding';
306
307 export const AJV_KEYWORD = 'ajvKeyword';
308 export const AJV_FORMAT = 'ajvFormat';
309
310 export const REST_MIDDLEWARE_CHAIN = DEFAULT_MIDDLEWARE_CHAIN;
311
312 /**
313 * Legacy middleware chain for action-based REST sequence
314 */
315 export const ACTION_MIDDLEWARE_CHAIN = 'middlewareChain.rest.actions';
316}