import { NextApiResponse } from "next";
import { APIError } from "./error";

export type APIRequest = (request:Request)=>Promise<Response>;

export async function runApiAction(c:APIRequest,request:Request){
    try{
        return await c(request);
    }
    catch(e){
        if (e instanceof APIError){
            return new Response(e.info.responseText,{status:e.info.status});
            console.log("responded");
        }
        else{
            throw e;
        }
    }
}