import httpStatus from "http-status"; import { APIResponse, Methods, MyPaths, ResponseSchemas, StatusCodes, } from "~/types/backend"; export function ErrorResponse< P extends MyPaths, M extends Methods
, C extends StatusCodes
= StatusCodes
, >(code: C, error?: string): APIResponse
{ console.log(code, error); const responseData = { error: error ?? httpStatus[`${httpStatus[code]}_NAME`], }; return new Response(JSON.stringify(responseData), { status: httpStatus[code], headers: { "Content-Type": "application/json", }, }); } export function Res< P extends MyPaths, M extends Methods
, C extends StatusCodes
= StatusCodes
, >(code: C, payload: ResponseSchemas
): APIResponse
{ return new Response(payload === null ? null : JSON.stringify(payload), { status: httpStatus[code], headers: { "Content-Type": "application/json", }, }); }