ts-rpc is a simple RPC library written in TypeScript with support for sharing types between the client and server.
yarn add @omaksi/ts-rpc
Configure RPC type:
type HelloWorldRPC = {
method: 'hello'
params: {
world: string
}
result: {
message: string
}
}
Server (express middleware)
const rpcHandlerMiddleware = RpcHandlerMiddleware({
hello: Handle<HelloWorldRPC> = (params) => ({
message: `Hello ${params.world}`
})},
})
Client:
const result = await Call<HelloWorldRPC>('hello', { world: 'Neptune' })
console.log(result.message) // Hello Neptune
See examples directory for full example.
MIT