deno.land / x / lightrpc@1.0.0

LightRPC

Light, simple, easy type-safe RPC for TypeScript.

lightrpc is Inspired by trpc, gentle_rpc.

Usage

  1. Define Method.
// method.ts
export const defineFuncs = {
  add: (p1: number, p2: number): number => {
    return p1 + p2;
  },
  join: (p1: string, p2: string, separator = ''): string => {
    return `${p1}${separator}${p2}`;
  },
} as const;
  1. Define server and start.
// server_main.ts
import { serve } from "https://deno.land/std@0.156.0/http/server.ts";
import { defineFuncs } from "./method.ts";
import { serverFunc } from "https://deno.land/x/lightrpc/mod.ts";

serve(async (request) => {
  return await serverFunc(defineFuncs, request);
});
> deno run --allow-net=0.0.0.0:8000 server_main.ts
Listening on http://localhost:8000/
  1. Define client asd start.
// client_main.ts
import { defineFuncs } from "./method.ts";
import { clientFunc } from "https://deno.land/x/lightrpc/mod.ts";

console.log(
  await clientFunc<typeof defineFuncs>("http://localhost:8000").add(
    123,
    456,
  ),
);
// => 579

console.log(
  await clientFunc<typeof defineFuncs>("http://localhost:8000").join(
    "Hello",
    "World",
  ),
);
// => Hello World
> deno run --allow-net=localhost:8000 client_main.ts
579
HelloWorld
lightrpc

Version Info

Tagged at
a year ago