1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { parseRPCArgs } from "https://esm.town/v/std/parseRPCArgs";
import { z } from "npm:zod@3.22.4";
export function rpc(fn: Function) {
return async (req: Request): Promise<Response> => {
try {
const args = await parseRPCArgs(req);
const result = await fn.apply(null, args);
return Response.json(result);
} catch (e) {
if (e instanceof Response) {
return e;
}
console.error(e);
return Response.json("Error", { status: 500 });
}
};
}