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
36
37
38
import { validator } from "npm:hono/validator";
import { Hono } from "npm:hono@3";
import { z } from "npm:zod";
import { zodToJsonSchema } from "npm:zod-to-json-schema";
type ExecuteResult = string | { message: string; result: any };
export const makeHonoTool = (execute: (...args: any) => Promise<ExecuteResult>, schema: z.ZodTypeAny) => {
const app = new Hono();
app.get("/", (c) => c.json({ result: "GET not supported" }));
app.post(
"/",
validator("json", (value, c) => {
console.log("POST json value", value);
const parsed = schema.safeParse(value);
if (!parsed.success) {
return c.text("Invalid!", 401);
}
return parsed.data;
}),
async (c) => {
const payload = c.req.valid("json");
const result = await execute(payload);
if ((result as any).message) {
return c.json(result);
} else {
return c.json({ message: result, result });
}
},
);
app.get("/schema", (c) => c.json(zodToJsonSchema(schema)));
app.get("/info", (c) => {
return c.json({
schema: zodToJsonSchema(schema),
description: schema.description,
});
});
return app;
};