1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/** @jsxImportSource https://esm.sh/hono/jsx */
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);
};
}