0.0.4 • Published 4 months ago

zervice v0.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

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);
0.0.4

4 months ago

0.0.3

4 months ago

0.0.2

4 months ago

0.0.1

4 months ago