ng / backend /src /controllers /BaseController.ts
epii-1
222222
f0953a4
raw
history blame contribute delete
679 Bytes
import { Request, Response } from "express";
import { ApiResponse } from "../core/ApiResponse";
interface ApiResponseData<T> {
data?: T;
message?: string;
}
export abstract class BaseController {
protected async handleRequest<T>(
req: Request,
res: Response,
action: () => Promise<ApiResponseData<T> | void>
): Promise<void> {
try {
const result = await action();
if (result) {
res.json(ApiResponse.success(result.data, result.message));
}
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : "未知错误";
res.status(200).json(ApiResponse.error(errorMessage));
}
}
}