Coder Social home page Coder Social logo

lightrpc's Introduction

LightRPC

Tiny JSON-RPC library - batteries not included.

build status npm version npm downloads

This project is not stable yet!

Getting Started

LightRPC is a minimal library for interacting with JSON RPC. It is designed to be small and work in browser and server.

Installing

Install using npm or yarn

npm install lightrpc
# or if you are using yarn
yarn add lightrpc

Obtain development copy

git clone https://github.com/Sekhmet/lightrpc.git
cd lightrpc
npm install

Usage

Usage in browser

<script src="https://unpkg.com/lightrpc/dist/lightrpc.min.js"></script>

Or if you want to use not minified version

<script src="https://unpkg.com/lightrpc/dist/lightrpc.js"></script>
// using UMD (browser)
const client = new window.LightRPC('https://api.steemit.com');

// using CommonJS
const Client = require('lightrpc');
const client = new Client('https://api.steemit.com');

// using ES6 modules
import Client from 'lightrpc';
const client = new Client('https://api.steemit.com');

// sending requests
client.call('get_accounts', [['sekhmet']], function(err, result) {
  if (err !== null) console.error(err);
  console.log('response', result);
});

const request = {
  method: 'get_accounts',
  params: [['sekhmet']],
};
client.send(request, function(err, result) {
  if (err !== null) console.error(err);
  console.log('response', result);
});

const requests = [
  {
    method: 'get_accounts',
    params: [['sekhmet']],
  },
  {
    method: 'get_dynamic_global_properties',
    params: [],
  },
];
client.sendBatch(requests, function(err, result) {
  if (err !== null) console.error(err);
  console.log('response', result);
});

Options

You can configure client by using optional options parameter to Client constructor.

const options = {
  timeout: 5000,
};

const client = new Client('https://api.steemit.com', options);

You can change some (or all) options for specific request using 3 parameter to send method.

Option Default value Description
timeout 5000 Time after request should timeout.
headers {'Content-Type': 'application/json'} Headers to be included in requests.

Running the tests

You can run lint and tests using npm script

npm run test

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

lightrpc's People

Contributors

bonustrack avatar sekhmet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lightrpc's Issues

Add retry feature

Would be nice to have a feature to automatically try if a request fail or is timeouted. There should be a way to set a custom number of retries.

Create methods dynamically

Blocked by #9

Based on the file created from #9, we should create function from steemd API dynamically. The goal is to allow this kind of possibility with lightrpc:

// with callback
client.getAccounts(['fabien'], (err, result) => {});

// with promise
client.getAccounts(['fabien']).then(result => {}); 

// with named params
client.getAccounts({ names: ['fabien'] }, (err, result) => {}); 

// with named params and promise
client.getAccounts({ names: ['fabien'] }).then(result => {});

// empty param should display description of the method
client.getAccounts();

Promise should be embed in lightrpc

Would be nice to have the promise directly embed in lightrpc, i would just recommend to not embed bluebird because it's quite heavy, we can use native Promise for it.

Using timeout option?

You'll probably see this on the busy issue tracker but I'm having trouble using the options on this. Not sure if I'm doing it wrong or a bug.

https://github.com/busyorg/busy/pull/1621/files

const options = {
  timeout: 15000,
};

const steemUrl = process.env.STEEMJS_URL || 'https://api.steemit.com';

const client = createClient(steemUrl, options);

Result:

screenshot from 2018-03-08 19-05-00

screenshot from 2018-03-08 19-05-05

Handle batch requests

With jussi (api.steemit.com) it's possible to send batch requests. LightRPC should allow this aswell. It would be easier for replay blockchain or could be used in Busy to reduce the number of requests. Instead of sending one operation per request, we can send one array of multiple operation per request.

Multiple endpoints

Give ability to add multiple endpoint to fallback if some endpoint become unavailable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.