Add request parsing middlewares to controllers
This commit is contained in:
parent
a2ef0f6a42
commit
adf380c19d
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "wms-core",
|
||||
"version": "0.7.6",
|
||||
"version": "0.7.12",
|
||||
"description": "Node web framework",
|
||||
"repository": "git@gitlab.com:ArisuOngaku/wms-core.git",
|
||||
"author": "Alice Gaudon <alice@gaudon.pro>",
|
||||
|
@ -1,4 +1,4 @@
|
||||
import express, {NextFunction, Request, Response, Router} from 'express';
|
||||
import express, {NextFunction, Request, RequestHandler, Response, Router} from 'express';
|
||||
import {BadRequestError, HttpError, NotFoundHttpError, ServerError, ServiceUnavailableHttpError} from "./HttpError";
|
||||
import {lib} from "nunjucks";
|
||||
import Logger from "./Logger";
|
||||
@ -170,6 +170,12 @@ export default abstract class Application {
|
||||
});
|
||||
}
|
||||
|
||||
public setupRequestParsingMiddlewares(router: Router) {
|
||||
for (const controller of this.controllers) {
|
||||
controller.setupRequestParsingMiddlewares(router);
|
||||
}
|
||||
}
|
||||
|
||||
public getWebSocketListeners(): { [p: string]: WebSocketListener } {
|
||||
return this.webSocketListeners;
|
||||
}
|
||||
|
@ -39,6 +39,9 @@ export default abstract class Controller {
|
||||
return [];
|
||||
}
|
||||
|
||||
public setupRequestParsingMiddlewares(router: Router): void {
|
||||
}
|
||||
|
||||
public hasGlobalHandlers(): boolean {
|
||||
return this.getGlobalHandlers().length > 0;
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
import ApplicationComponent from "../ApplicationComponent";
|
||||
import express, {Express, Router} from "express";
|
||||
import express, {Express, NextFunction, Request, Response, Router} from "express";
|
||||
import Logger from "../Logger";
|
||||
import {Server} from "http";
|
||||
import {MulterError} from "multer";
|
||||
import {FileError, ValidationBag} from "../db/Validator";
|
||||
|
||||
export default class ExpressAppComponent extends ApplicationComponent<void> {
|
||||
private readonly port: number;
|
||||
@ -17,6 +19,22 @@ export default class ExpressAppComponent extends ApplicationComponent<void> {
|
||||
Logger.info(`Web server running on localhost:${this.port}.`);
|
||||
});
|
||||
|
||||
this.app?.setupRequestParsingMiddlewares(router);
|
||||
|
||||
// Multer error handler
|
||||
router.use((err: any, req: Request, res: Response, next: NextFunction) => {
|
||||
if (err instanceof MulterError) {
|
||||
const bag = new ValidationBag();
|
||||
const validationError = new FileError(err.message);
|
||||
validationError.thingName = err.field;
|
||||
bag.addMessage(validationError);
|
||||
req.flash('validation', bag.getMessages());
|
||||
res.redirectBack();
|
||||
} else {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
router.use(express.json());
|
||||
router.use(express.urlencoded({
|
||||
extended: true,
|
||||
|
@ -34,6 +34,7 @@ export default class LogRequestsComponent extends ApplicationComponent<void> {
|
||||
query: req.query,
|
||||
params: req.params,
|
||||
body: req.body,
|
||||
file: req.file,
|
||||
files: req.files,
|
||||
cookies: req.cookies,
|
||||
sessionId: req.sessionID,
|
||||
|
@ -410,3 +410,16 @@ export class UnknownRelationValidationError extends ValidationError {
|
||||
return `${this.thingName}=${this.value} relation was not found in ${this.table}${this.foreignKey !== undefined ? `.${this.foreignKey}` : ''}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class FileError extends ValidationError {
|
||||
private readonly m: string;
|
||||
|
||||
constructor(message: string) {
|
||||
super();
|
||||
this.m = message;
|
||||
}
|
||||
|
||||
public get message(): string {
|
||||
return `${this.m}`;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user