1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const AbstractMiddleware_1 = require("./AbstractMiddleware");
|
13 | const Response_1 = require("../Response");
|
14 | const Error_1 = require("../Error");
|
15 | const debug_1 = require("../../debug");
|
16 | const decorators_1 = require("../ServiceManager/decorators");
|
17 | const Logger_1 = require("../Logger");
|
18 | const debug = debug_1.createDebugLogger('middleware:dispatch');
|
19 | class DispatchMiddleware extends AbstractMiddleware_1.AbstractMiddleware {
|
20 | async pass(ctx, next) {
|
21 | if (ctx.state.response) {
|
22 | debug('Response found on context, calling next.');
|
23 | return next();
|
24 | }
|
25 | const { controller, action, controllerName } = ctx.state.dispatch;
|
26 | debug(`Dispatching ${controllerName}.${action}.`);
|
27 |
|
28 | if (typeof controller[action] !== 'function') {
|
29 | debug(`${controllerName}.${action} not found, calling next.`);
|
30 | this.logger.error(`Action "${action}" not found on controller "${controllerName}" for request path "${ctx.path}".`);
|
31 | ctx.state.response = this.responseService.serverError().notImplemented();
|
32 | return next();
|
33 | }
|
34 | let response;
|
35 | try {
|
36 | response = await controller[action](ctx);
|
37 | if (!(response instanceof Response_1.Response)) {
|
38 | throw new Error_1.InvalidActionResultError([
|
39 | `Action "${controllerName}.${action}" failed to produce a Response instance,`,
|
40 | `instead got type "${typeof response}".`,
|
41 | 'Did you forget to add a return statement in front of your response?',
|
42 | ].join(' '));
|
43 | }
|
44 | }
|
45 | catch (error) {
|
46 | this.logger.error(error.message, error);
|
47 | response = this.responseService.serverError().internalServerError(null, null, { error });
|
48 | }
|
49 | ctx.state.response = response;
|
50 | debug(`Dispatched ${controllerName}.${action}, calling next.`);
|
51 | next();
|
52 | }
|
53 | }
|
54 | __decorate([
|
55 | decorators_1.inject(Response_1.ResponseService),
|
56 | __metadata("design:type", Response_1.ResponseService)
|
57 | ], DispatchMiddleware.prototype, "responseService", void 0);
|
58 | __decorate([
|
59 | decorators_1.inject(Logger_1.LoggerService),
|
60 | __metadata("design:type", Logger_1.LoggerService)
|
61 | ], DispatchMiddleware.prototype, "logger", void 0);
|
62 | exports.DispatchMiddleware = DispatchMiddleware;
|
63 |
|
\ | No newline at end of file |