import { HTTPException } from "https://deno.land/x/hono@v4.0.7/http-exception.ts";
import { type FC } from "https://deno.land/x/hono@v4.0.7/jsx/index.ts";
import type { Context, Hono } from "https://deno.land/x/hono@v4.0.7/mod.ts";
import { html } from "https://esm.town/v/stevekrouse/html";
type HandlerItem = (ctx: Context) => Response | JSX.Element | Promise<Response> | Promise<JSX.Element>;
type Handler = HandlerItem | {
[method: string]: HandlerItem;
};
export function createRoute(handler: Handler) {
return async (ctx: Context) => {
if (typeof handler == "object") {
const method = ctx.req.method;
if (!(method in handler)) {
throw new HTTPException(405);
}
const res = await handler[method](ctx);
if (res instanceof Response) {
return res;
}
return ctx.render(res);
}
const res = await handler(ctx);
if (res instanceof Response) {
return res;
}
return ctx.render(res);
};
}