1.0.0-beta.6 • Published 3 months ago

@tinkink/koa-rpc-client v1.0.0-beta.6

Weekly downloads
-
License
-
Repository
-
Last release
3 months ago

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
})();
1.0.0-beta.6

3 months ago

1.0.0-beta.5

1 year ago

1.0.0-beta.2

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.4

2 years ago

1.0.0-beta.1

2 years ago