@tscall/server
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Tscall - Minimalistic RPC handler


Tscall is a minimalistic and lightweight RPC handler for TypeScript. It doesn't have unnecessary clutter.

  • Lightweight: It's just a few lines of code.
  • Type-safe: It's written in TypeScript and it's type-safe.
  • Easy to use: It's easy to use, you just create your own router and give it to the server.
  • Works out of the box: It works out of the box, you don't need to configure anything.
  • Small bandwidth: All the data is serialized into bytes, so it's very small and faster than json serialization.

Usage

You just create your own router and give it to the server. The server will handle the rest.

import { buildRoutes } from "@ts-call/server";

export const routes = buildRoutes({
    sum: (a: number, b: number) => a + b,
});

export type Routes = typeof routes; //Only export the type of the routes
import { createServer as createHttpServer } from "@ts-call/server";
import { routes } from "./routes";

const server = createHttpServer(routes);

server.listen(3000, () => {
    console.log("Server is running on port http://localhost:3000");
});

And on the client side, you can directly call the server methods.

import { createCaller } from "@ts-call/client";
import type { Routes } from "../your-api/server";

const call = createCaller<Routes>("http://localhost:3000");

call("/sum", 1, 2).then(console.log); //3
call("/sum", 10, 8).then(console.log); //18

Installation

#On server
npm install @ts-call/server

# On client
npm install @ts-call/client

Data validation

You can use any validation library you want, tscall lets you define your own interface and use it.

project
├── protocol.ts
├── routes
│   └── sum.ts
└── server.ts
//protocol.ts

import { useValidator } from "@ts-call/server";
import * as z from "zod";

function zod<
    const TSchema extends z.ZodTypeAny
>(schema: TSchema, data: unknown): z.infer<TSchema> {
    return schema.parse(data);
}

export const withValidator = useValidator(zod);
//routes/sum.ts

import { withValidator } from "../protocol";
import * as z from "zod";

export default withValidator(z.object({ a: z.number(), b: z.number() }), ({ a, b }) => {
    return a + b;
});

Package Sidebar

Install

npm i @tscall/server

Weekly Downloads

219

Version

0.0.2

License

ISC

Unpacked Size

19.8 kB

Total Files

20

Last publish

Collaborators

  • someramsey