import { NextResponse } from "next/server"; export class ApiError extends Error { constructor( public status: number, message: string ) { super(message); this.name = "ApiError"; } } export function apiSuccess(data: T, status = 200) { return NextResponse.json({ success: true, data }, { status }); } export function apiError(error: unknown) { if (error instanceof ApiError) { return NextResponse.json( { success: false, error: error.message }, { status: error.status } ); } const message = error instanceof Error ? error.message : "Internal server error"; console.error("[API]", error); return NextResponse.json({ success: false, error: message }, { status: 500 }); } export async function parseJson(request: Request): Promise { try { return (await request.json()) as T; } catch { throw new ApiError(400, "Invalid JSON body"); } }