1.3.1 • Published 5 years ago

@bifot/json-rpc v1.3.1

Weekly downloads
19
License
MIT
Repository
github
Last release
5 years ago

@bifot/json-rpc

Solution for communication between services using HTTP protocol via JSON RPC. 🔬

Install

$ npm i @bifot/json-rpc -S

Usage

Server

const { Server } = require('@bifot/json-rpc');
const { Users } = require('../db');

const server = new Server();

server.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.error = {
      code: 500,
      message: 'Server error.',
    };
  }
});

server.on('get', async (ctx) => {
  ctx.body = await Users.findOne({
    id: ctx.params.id,
  });
});

server.listen(process.env.APP_PORT);

Client

const { Client } = require('@bifot/json-rpc');

const client = new Client({
  services: {
    users: process.env.USERS_ADDRESS,
  },
});

const user = await client.ask('users.get', {
  id: 10,
}, {
  retries: 5,
  retryDelay: 500,
});

// {
//   error: undefined,
//   result: {
//     id: 10,
//     name: 'Mikhail Semin',
//     age: 16,
//   },
// }
1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago