-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
36 lines (29 loc) · 836 Bytes
/
server.ts
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
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
const app = new Hono();
const rpcSchema = z.object({
method: z.string(),
params: z.array(z.union([z.string(), z.number()])),
});
const route = app.post("/rpc", zValidator("json", rpcSchema), async (c) => {
const { method, params } = await c.req.json();
let result: string | number;
switch (method) {
case "hello":
result = `Hello, ${params[0]}!`;
break;
case "add":
if (typeof params[0] === "number" && typeof params[1] === "number") {
result = params[0] + params[1];
} else {
result = String(params[0]) + String(params[1]);
}
break;
default:
return c.json({ error: "Method not found" }, 404);
}
return c.json({ result });
});
export type AppType = typeof route;
export default app;