UNPKG

749 BPlain TextView Raw
1import { Request as ERequest, Response as EResponse } from "express";
2
3/**
4 *
5 * This class simply wrap the original Response class in order to add async/await
6 * functionality, improving code readability and maintenability.
7 */
8export default abstract class AsyncResponse {
9 /**
10 * Generate the correct response, injecting it to the standard Express response.
11 * @param req the standard Express request
12 * @param res the standard Express response
13 */
14 abstract async asyncResponse(req: ERequest, res: EResponse): Promise<void>;
15
16 performResponse(req: ERequest, res: EResponse) {
17 this.asyncResponse(req, res)
18 .then(() => {})
19 .catch(err => {
20 throw err;
21 });
22 }
23}