simple-jrpc-client
v0.0.4
Published
A lightweight JavaScript client for making JSON-RPC 2.0 requests over HTTP.
Readme
simple-jrpc-client
A lightweight JavaScript client for making JSON-RPC 2.0 requests over HTTP.
Features
- Send single or batch JSON-RPC requests
- Handles responses and errors
- Simple API for integration
Installation
Install via npm:
npm install simple-jrpc-clientUsage
Import
const { JRPCRequest, JRPCBatch, JRPCClient } = require('simple-jrpc-client');Single Request
const request = new JRPCRequest('methodName', { param1: 'value' }, 1);
const response = await JRPCClient.execute('http://your-rpc-endpoint', request);
console.log(response.getAll());Batch Request
const batch = new JRPCBatch();
batch.add(new JRPCRequest('method1', { foo: 'bar' }, 1));
batch.add(new JRPCRequest('method2', { baz: 42 }, 2));
const response = await JRPCClient.execute('http://your-rpc-endpoint', batch);
console.log(response.getAll());API
Classes
- JRPCRequest: Create a single JSON-RPC request.
- JRPCBatch: Create a batch of JSON-RPC requests.
- JRPCClient: Static method
execute(url, request)to send requests. - JRPCResponse: Handles and formats the response.
JRPCResponse Methods
getById(id): Get response by request ID.getAll(): Get all responses.getStatus(): Get HTTP status code.getResponses(): Get all successful responses (with aresultfield).getErrors(): Get all error responses (with anerrorfield).
License
MIT
