zervice
v0.0.4
Published
Remote service server/client with strong zod based typings.
Downloads
2
Readme
zervice
Simple RPC Services with Zod validation.
Example
Service Speacification
import { z } from "zervice";
export const schema = z.service({
hello: {
req: z.object({
name: z.string()
}),
res: z.object({
message: z.string()
})
},
sum: {
req: z.object({
a: z.number(),
b: z.number()
}),
res: z.number()
}
});
Server Implementation
import { z } from "zervice";
const server = z.server({ log_service_name: "demo-service" });
server.handle(schema.hello, async (req) => {
return { message: "Hello " + req.name };
});
server.handle(schema.sum, async (req) => {
return req.a + req.b;
});
import { serve } from "@hono/node-server";
serve({
fetch: server.request_handler,
port: 9753
});
A Client
import { z } from "zervice";
const client = z.client("http://localhost:9753/zervice");
const res = await client(schema.sum, { a: 2, b: 2 });
console.log("sum res", res);