Add PUT and DELETE REST methods to controllers

This commit is contained in:
Alice Gaudon 2020-06-14 11:43:49 +02:00
parent e63b5d21fe
commit 8755a152a6
1 changed files with 16 additions and 0 deletions

View File

@ -80,6 +80,22 @@ export default abstract class Controller {
this.router?.post(path, this.wrap(handler));
}
protected put(path: PathParams, handler: RequestHandler, routeName?: string, ...middlewares: RequestHandler[]) {
this.registerRoutes(path, handler, routeName);
for (const middleware of middlewares) {
this.router?.put(path, this.wrap(middleware));
}
this.router?.put(path, this.wrap(handler));
}
protected delete(path: PathParams, handler: RequestHandler, routeName?: string, ...middlewares: RequestHandler[]) {
this.registerRoutes(path, handler, routeName);
for (const middleware of middlewares) {
this.router?.delete(path, this.wrap(middleware));
}
this.router?.delete(path, this.wrap(handler));
}
private wrap(handler: RequestHandler): RequestHandler {
return (req, res, next) => {
function handleErr(e: any) {