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-21 16:48:40 +00:00
|
|
|
import { generateExpressError } 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>;
|
|
|
|
|
|
2024-05-21 16:48:40 +00:00
|
|
|
public execute(req: express.Request, res: express.Response, next: express.NextFunction): void {
|
2024-04-23 15:29:38 +00:00
|
|
|
this.req = req;
|
|
|
|
|
this.res = res;
|
|
|
|
|
this.next = next;
|
|
|
|
|
|
|
|
|
|
this.serverURL = `${
|
2024-05-21 16:48:40 +00:00
|
|
|
new URL(`${this.req.protocol}://${this.req.get("host")}${this.req.originalUrl}`).origin
|
2024-04-23 15:29:38 +00:00
|
|
|
}/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
|
|
|
}
|
|
|
|
|
|
2024-07-23 11:19:00 +00:00
|
|
|
public downloadFile(filepath: string, filename: string, done?: any) {
|
2024-04-23 15:29:38 +00:00
|
|
|
return this.res.download(filepath, filename, done);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 11:19:00 +00:00
|
|
|
public downloadPDF(pdfBuffer: Buffer, filename: string) {
|
2024-08-18 20:39:06 +00:00
|
|
|
return this._download(pdfBuffer, "application/pdf", `${filename}`);
|
2024-07-23 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-23 15:29:38 +00:00
|
|
|
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-21 16:48:40 +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
|
|
|
}
|
|
|
|
|
|
2024-05-21 16:48:40 +00:00
|
|
|
private _jsonResponse(statusCode: number, jsonPayload: any): express.Response<any> {
|
2024-04-23 15:29:38 +00:00
|
|
|
return this.res.status(statusCode).json(jsonPayload).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _errorResponse(
|
|
|
|
|
statusCode: number,
|
|
|
|
|
message?: string,
|
2024-05-21 16:48:40 +00:00
|
|
|
error?: Error | InfrastructureError
|
2024-04-23 15:29:38 +00:00
|
|
|
): express.Response<IError_Response_DTO> {
|
2024-05-21 16:48:40 +00:00
|
|
|
return generateExpressError(this.req, this.res, statusCode, message, error);
|
2024-04-23 15:29:38 +00:00
|
|
|
}
|
2024-07-23 11:19:00 +00:00
|
|
|
|
|
|
|
|
private _download(buffer: Buffer, contentType: string, filename: string) {
|
|
|
|
|
this.res.set({
|
|
|
|
|
"Content-Type": contentType,
|
|
|
|
|
"Content-Disposition": `attachment; filename=${filename}`,
|
2024-08-18 20:39:06 +00:00
|
|
|
//"Content-Length": buffer.length,
|
2024-07-23 11:19:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.res.send(buffer);
|
|
|
|
|
}
|
2024-04-23 15:29:38 +00:00
|
|
|
}
|