| 187 | */ |
| 188 | @injectable({scope: BindingScope.SINGLETON}) |
| 189 | export class MiddlewareSequence implements SequenceHandler { |
| 190 | private middlewareView: MiddlewareView; |
| 191 | |
| 192 | static defaultOptions: InvokeMiddlewareOptions = { |
| 193 | chain: RestTags.REST_MIDDLEWARE_CHAIN, |
| 194 | orderedGroups: [ |
| 195 | // Please note that middleware is cascading. The `sendResponse` is |
| 196 | // added first to invoke downstream middleware to get the result or |
| 197 | // catch errors so that it can produce the http response. |
| 198 | RestMiddlewareGroups.SEND_RESPONSE, |
| 199 | |
| 200 | RestMiddlewareGroups.CORS, |
| 201 | RestMiddlewareGroups.API_SPEC, |
| 202 | RestMiddlewareGroups.MIDDLEWARE, |
| 203 | |
| 204 | RestMiddlewareGroups.FIND_ROUTE, |
| 205 | |
| 206 | // authentication depends on the route |
| 207 | RestMiddlewareGroups.AUTHENTICATION, |
| 208 | |
| 209 | RestMiddlewareGroups.PARSE_PARAMS, |
| 210 | |
| 211 | RestMiddlewareGroups.INVOKE_METHOD, |
| 212 | ], |
| 213 | |
| 214 | /** |
| 215 | * Reports an error if there are middleware groups are unreachable as they |
| 216 | * are ordered after the `invokeMethod` group. |
| 217 | */ |
| 218 | validate: groups => { |
| 219 | const index = groups.indexOf(RestMiddlewareGroups.INVOKE_METHOD); |
| 220 | if (index !== -1) { |
| 221 | const unreachableGroups = groups.slice(index + 1); |
| 222 | if (unreachableGroups.length > 0) { |
| 223 | throw new Error( |
| 224 | `Middleware groups "${unreachableGroups.join( |
| 225 | ',', |
| 226 | )}" are not invoked as they are ordered after "${ |
| 227 | RestMiddlewareGroups.INVOKE_METHOD |
| 228 | }"`, |
| 229 | ); |
| 230 | } |
| 231 | } |
| 232 | }, |
| 233 | }; |
| 234 | |
| 235 | /** |
| 236 | * Constructor: Injects `InvokeMiddleware` and `InvokeMiddlewareOptions` |
| 237 | * |
| 238 | * @param invokeMiddleware - invoker for registered middleware in a chain. |
| 239 | * To be injected via RestBindings.INVOKE_MIDDLEWARE_SERVICE. |
| 240 | */ |
| 241 | constructor( |
| 242 | @inject.context() |
| 243 | context: Context, |
| 244 | |
| 245 | @inject(RestBindings.INVOKE_MIDDLEWARE_SERVICE) |
| 246 | readonly invokeMiddleware: InvokeMiddleware, |
nothing calls this directly
no outgoing calls
no test coverage detected