@tinkink/koa-rpc-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-beta.6 • Public • Published

Koa RPC

Best RPC lib for client side and koa based server side.

Note: This is a work in progress, the API may change.

Server side

First, install the package:

npm install @tinkink/koa-rpc-server

Then, define your RPC methods, they are very similar to koa middlewares, but accept arguments and return values:

# meth.ts
import { Context, Next } from 'koa';

export async function add(ctx: Context, next: Next, a: number, b: number) {
  return a + b;
}

Then, create a koa app and use these RPC methods:

# app.ts
import { Context, Next } from 'koa';
import Koa from 'koa';
import rpc from '@tinkink/koa-rpc-server';
import * as meth from './meth';

const app = new Koa();
app.use(rpc('/rpc', meth));

Client side

First, install the package:

npm install @tinkink/koa-rpc-client

Then, create a client, you can import the RPC methods definition from the server side:

import Rpc from '@tinkink/koa-rpc-client';
import * as meth from '../server/meth';

type RpcMethods = {
    meth: typeof meth;
};

const client = new Rpc<RpcMethods>('/rpc');

(async () => {
  const result = await client.add(1, 2);
  console.log(result); // 3
})();

Readme

Keywords

none

Package Sidebar

Install

npm i @tinkink/koa-rpc-client

Weekly Downloads

1

Version

1.0.0-beta.6

License

none

Unpacked Size

11 kB

Total Files

8

Last publish

Collaborators

  • tinymemo