Presupuestador_web/server/src/contexts/common/infrastructure/express/ExpressController.ts

139 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-05-16 18:16:00 +00:00
import { IError_Response_DTO } from "@shared/contexts";
2024-04-23 15:29:38 +00:00
import * as express from "express";
2024-05-16 18:16:00 +00:00
import httpStatus from "http-status";
2024-04-23 15:29:38 +00:00
import { URL } from "url";
import { IServerError } from "../../domain/errors";
2024-05-15 19:56:22 +00:00
import { IController } from "../Controller.interface";
2024-04-23 15:29:38 +00:00
import { InfrastructureError } from "../InfrastructureError";
2024-05-16 18:16:00 +00:00
import { generateExpressErrorResponse } from "./ExpressErrorResponse";
2024-04-23 15:29:38 +00:00
export abstract class ExpressController implements IController {
protected req: express.Request;
protected res: express.Response;
protected next: express.NextFunction;
protected serverURL: string = "";
protected file: any;
protected abstract executeImpl(): Promise<void | any>;
public execute(
req: express.Request,
res: express.Response,
2024-05-15 19:56:22 +00:00
next: express.NextFunction,
2024-04-23 15:29:38 +00:00
): void {
this.req = req;
this.res = res;
this.next = next;
this.serverURL = `${
new URL(
2024-05-15 19:56:22 +00:00
`${this.req.protocol}://${this.req.get("host")}${this.req.originalUrl}`,
2024-04-23 15:29:38 +00:00
).origin
}/api/v1`;
this.file = this.req && this.req["file"]; // <-- ????
this.executeImpl();
}
public ok<T>(dto?: T) {
if (dto) {
return this._jsonResponse(200, dto);
}
return this.res.status(200).send();
}
public fail(error: IServerError) {
2024-05-18 16:51:31 +00:00
console.error("ExpressController FAIL RESPONSE:", error);
2024-04-23 15:29:38 +00:00
2024-05-18 16:51:31 +00:00
return this._errorResponse(httpStatus.INTERNAL_SERVER_ERROR, error.message);
2024-04-23 15:29:38 +00:00
}
public created<T>(dto?: T) {
if (dto) {
2024-05-16 18:16:00 +00:00
return this.res.status(httpStatus.CREATED).json(dto).send();
2024-04-23 15:29:38 +00:00
}
2024-05-16 18:16:00 +00:00
return this.res.status(httpStatus.CREATED).send();
2024-04-23 15:29:38 +00:00
}
public noContent() {
2024-05-16 18:16:00 +00:00
return this.res.status(httpStatus.NO_CONTENT).send();
2024-04-23 15:29:38 +00:00
}
public download(filepath: string, filename: string, done?: any) {
return this.res.download(filepath, filename, done);
}
public clientError(message?: string) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.BAD_REQUEST, message);
2024-04-23 15:29:38 +00:00
}
public unauthorizedError(message?: string) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.UNAUTHORIZED, message);
2024-04-23 15:29:38 +00:00
}
public paymentRequiredError(message?: string) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.PAYMENT_REQUIRED, message);
2024-04-23 15:29:38 +00:00
}
public forbiddenError(message?: string) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.FORBIDDEN, message);
2024-04-23 15:29:38 +00:00
}
public notFoundError(message: string, error?: IServerError) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.NOT_FOUND, message, error);
2024-04-23 15:29:38 +00:00
}
public conflictError(message: string, error?: IServerError) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.CONFLICT, message, error);
2024-04-23 15:29:38 +00:00
}
public invalidInputError(message?: string, error?: InfrastructureError) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.UNPROCESSABLE_ENTITY, message, error);
2024-04-23 15:29:38 +00:00
}
public tooManyError(message: string, error?: Error) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.TOO_MANY_REQUESTS, message, error);
2024-04-23 15:29:38 +00:00
}
public internalServerError(message?: string, error?: IServerError) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(
httpStatus.INTERNAL_SERVER_ERROR,
message,
error,
);
2024-04-23 15:29:38 +00:00
}
public todoError(message?: string) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.NOT_IMPLEMENTED, message);
2024-04-23 15:29:38 +00:00
}
public unavailableError(message?: string) {
2024-05-16 18:16:00 +00:00
return this._errorResponse(httpStatus.SERVICE_UNAVAILABLE, message);
2024-04-23 15:29:38 +00:00
}
private _jsonResponse(
statusCode: number,
2024-05-15 19:56:22 +00:00
jsonPayload: any,
2024-04-23 15:29:38 +00:00
): express.Response<any> {
return this.res.status(statusCode).json(jsonPayload).send();
}
private _errorResponse(
statusCode: number,
message?: string,
2024-05-15 19:56:22 +00:00
error?: Error | InfrastructureError,
2024-04-23 15:29:38 +00:00
): express.Response<IError_Response_DTO> {
2024-05-16 18:16:00 +00:00
return generateExpressErrorResponse(
this.req,
this.res,
2024-04-23 15:29:38 +00:00
statusCode,
2024-05-16 18:16:00 +00:00
message,
error,
2024-04-23 15:29:38 +00:00
);
}
}