1.1.1 • Published 6 years ago

koa-jsonrpc v1.1.1

Weekly downloads
10
License
MIT
Repository
github
Last release
6 years ago

koa-jsonrpc

Json-RPC 2 middleware implementation on Koa v2

Requirements

  • Koa@2+
  • Node@8+

Installation

npm install koa-jsonrpc --save

Options

  • limitString - sets the maximum size allowed for requests
  • authObject - requires requests to have a valid HMAC256 of the username and password as its authorization header

Authorization

//generating authorization header
const crypto = require('crypto');
const token = crypto.createHmac('sha256','mypass').update('myuser').digest('hex')
console.log(token) // will print out the token, and you can add this on the header of your request.

Usage

const Koa = require('koa');
const app = new Koa();
const koaJsonRpc = require('koa-jsonrpc');
const options = {
  limit: '20mb', // optional, defaults to 1mb
  auth: { // optional, will require authorization header
    username: 'myuser',
    password: 'mypass'
  }
}
const jrpc2 = koaJsonRpc(options);

// Add methods
jrpc2.use('user', async function user() {
  return 'root';
});

jrpc2.use('sum', async function sum(params) {
  return params.reduce((prev, curr) => prev + curr,
        0);
});

jrpc2.use('internal', async function internal() {
  throw new Error();
});

app.use(jrpc2.app());
1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago