https://github.com/mahsumurebe/jrpc-client
JSONRPC 2.0 NodeJS Client written in TypeScript
https://github.com/mahsumurebe/jrpc-client
http-client jsonrpc jsonrpc-client jsonrpc-lib jsonrpc2 jsonrpc2-ws websocket
Last synced: over 1 year ago
JSON representation
JSONRPC 2.0 NodeJS Client written in TypeScript
- Host: GitHub
- URL: https://github.com/mahsumurebe/jrpc-client
- Owner: mahsumurebe
- License: mit
- Created: 2020-03-23T20:31:54.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-23T08:40:51.000Z (over 3 years ago)
- Last Synced: 2025-02-20T19:48:18.820Z (over 1 year ago)
- Topics: http-client, jsonrpc, jsonrpc-client, jsonrpc-lib, jsonrpc2, jsonrpc2-ws, websocket
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@mahsumurebe/jrpc-client
- Size: 418 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# JRPC Client
JSONRPC 2.0 Client package for Nodejs. Fully tested to comply with the official [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification)










## Quick Overview
It is used to quickly create JSONRPC Client. Method call is very simple.
#### To Create a Server
You can use the [@mahsumurebe/jrpc-server](https://www.npmjs.com/package/@mahsumurebe/jrpc-server) package to create a server.
## Install
For installation, you can run the following command in the directory of your project.
```shell script
npm install @mahsumurebe/jrpc-client
```
## Usage
It should not create a JRPCClient instance.
```typescript
import { JRPCClient, HttpAdapter } from '@mahsumurebe/jrpc-client';
// Create instance
const clientInstance = new JRPCClient(new HttpAdapter('http://localhost:3000'));
// Call start method for connection
await clientInstance.start();
```
### Call Method
Method calls are made after the JRPCClient instance is created.
```typescript
// Call foo method with "bar" and "baz" parameters
const response = await clientInstance.call({
id: 1,
jsonrpc: "2.0",
method: "foo",
params: ["bar", "baz"],
});
console.log(response);
// Response Object
// { id: 1, jsonrpc: "2.0", response: "foo response" }
```
The call method returns the JSONRPC response. Returns the ErrorResponse class instance if the request response is an error.
### Batch Call Method
Call method can be used to call requests. You can review the usage example below.
```typescript
const batchResponse = await clientInstance.call([
{ id: 1, jsonrpc: "2.0", method: "foo", params: ["bar", "baz"] },
{ id: 2, jsonrpc: "2.0", method: "bar", params: ["bar", "baz"] },
{ jsonrpc: "2.0", method: "notification", params: ["bar", "baz"] }, // Notification request does not return value
]);
console.log(batchResponse);
// [
// { id: 1, jsonrpc: "2.0", response: "foo response" },
// { id: 2, jsonrpc: "2.0", response: "bar response" },
// ]
```
### Notification Method
Check out the sample code below to make a method notification.
```typescript
// Notification foo method with "bar" and "baz" parameters
await clientInstance.notification({
jsonrpc: "2.0",
method: "foo",
params: ["bar", "baz"],
});
```
**Note:** The notification method does not return any response. The request is sent to the server. No response is expected from the server.
### Batch Notification Method
Check out the example below for sending batch notifications.
```typescript
await clientInstance.notification([
{ jsonrpc: "2.0", method: "foo", params: ["bar", "baz"] },
{ jsonrpc: "2.0", method: "bar", params: ["bar", "baz"] },
{ jsonrpc: "2.0", method: "baz", params: ["bar", "baz"] },
]);
```
## Adapters
There are HTTP and Websocket adapters available.
### HTTP
HTTP Adapter is used to connect to JRPC Servers served over HTTP Protocol.
```typescript
// Adapter Instance
import {JRPCClient, HttpAdapter} from '@mahsumurebe/jrpc-client';
const adapter = new HttpAdapter('http://localhost:3000');
// Client Instance
const clientInstance = new JRPCClient(adapter);
```
#### Configuration List
Configurations are defined in the object in the first parameter of the construction method when creating the HttpAdapter.
| KEY | DEFAULT | DESCRIPTION | Type |
|---------|-----------|-----------------|----------|
| parser | null | Response parser | Function |
| headers | null | HTTP Headers | object |
| timeout | 10_000 | Timeout | number |
### Websocket
Websocket Adapter is used to connect to JRPC Servers served over Websocket Protocol.
```typescript
// Adapter Instance
import { JRPCClient, WebsocketAdapter } from '@mahsumurebe/jrpc-client';
const adapter = new WebsocketAdapter('ws:/localhost:3000');
// Client Instance
const clientInstance = new JRPCClient(adapter);
```
#### Configuration List
Configurations are defined in the object in the first parameter of the construction method when creating the WebsocketAdapter.
| KEY | DEFAULT | DESCRIPTION | Type |
|---------|-----------|-----------------|----------|
| parser | null | Response parser | Function |
| headers | null | HTTP Headers | object |
| timeout | 10_000 | Timeout | number |
#### Custom Adapters
For custom adapters, you need to extend the adapter class with the `AdapterAbstract` abstract class.
You have to create the abstract functions request, connect and destroy inside the class.
**connect**: A piece of code that provides the protocol connection should be added to this method.
**destroy**: A piece of code that breaks the protocol connection should be added to this method.
**request**: A piece of code that sends the body to the server and parses the output should be added to this method.
_When there is a response from the server, the response object should be sent to the `adapter.config.parse` function._
## Resources
- [Changelog](https://github.com/mahsumurebe/jrpc-client/blob/development/CHANGELOG.md)