0.1.1 • Published 1 year ago

typescript-rpc v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

TypeScript RPC

Another attempt to a TypeScript end-to-end type safe framework.

Npm package version

npm i typescript-rpc

Autocompletion Heaven

Autocomplete in webstorm Autocomplete in StackBlitz

Usage

// server.ts
import createHandler from "typescript-rpc/createHandler";
import {randomUUID} from "crypto";
import * as http from "http";

// define your api
const api = {
    async hello(greeting: string) {
        return greeting + " World";
    },
    // you can nest and regroup methods
    foo: {
        bar: {
            baz: {
                async create(item: Item) {
                    const id = randomUUID();
                    items.set(id, item);
                    return id;
                },
                async read(id: string) {
                    return items.get(id);
                },
                async update(id: string, item: Item) {
                    items.set(id, item);
                },
                async delete(id: string) {
                    items.delete(id);
                }
            }
        }
    }
}

type Item = { attr: string }
const items = new Map<string, Item>();

// make sure to export your api
export default api;

// create your request handler
const handler = createHandler(api);

// pass it to your server
http.createServer(handler).listen(8000);
// client.ts

// import type from your api definition
import type api from "./server";
import createClient from "typescript-rpc/createClient";

// create your client with 
// typeof <YOUR API DEFINITION>
const client = createClient<typeof api>();

(async () => {
    // wait for the client to be ready
    await client.ready();

    // make some calls!
    const helloWorld = await client.hello("Hello");
})()

// make sure to export your client and access it from anywhere!
export default client;

Example

Checkout the examples directory to see how to build and bundle your files into a functional web application.

or try on StackBlitz

webpack-tsc-express

Open in StackBlitz

ts-node-esbuild

Open in StackBlitz

react-todo-app

Open in StackBlitz

auth-app

Open in StackBlitz

0.1.1

1 year ago

0.1.0

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.5-test

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago