Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/horprogs/js-jsonrpc-request
Isomorphic JSON-RPC 2.0 client for browser and Node.js with logging curl and time.
https://github.com/horprogs/js-jsonrpc-request
js json-rpc-client json-rpc2 node-js
Last synced: about 1 month ago
JSON representation
Isomorphic JSON-RPC 2.0 client for browser and Node.js with logging curl and time.
- Host: GitHub
- URL: https://github.com/horprogs/js-jsonrpc-request
- Owner: horprogs
- Created: 2018-08-09T09:26:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-03T23:20:55.000Z (about 2 years ago)
- Last Synced: 2024-11-11T12:14:05.730Z (about 2 months ago)
- Topics: js, json-rpc-client, json-rpc2, node-js
- Language: JavaScript
- Homepage:
- Size: 1.01 MB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Isomorphic JSON-RPC 2.0 client for browser and Node.js with logging curl and time.
# Installation
```shell
yarn add js-jsonrpc-request
```or
```shell
npm install js-jsonrpc-request --save
```# Usage
```js
import JsonRpcClient from 'js-jsonrpc-request';const jsonrpc = new JsonRpcClient({
apiRoute: '/api/rpc/v1.0',
headers: {
'X-API-CLIENT': 'key',
},
withMeta: true,
});jsonrpc
.request('info.getSomething', { data: 'something' })
.then(({ data, meta }) => {
console.log('Data', data);
console.log('Curl', meta.curl);
console.log('Time for request', `${meta.timeRequest} ms`);
})
.catch((err) => console.log('ERROR', err));
````data` - data response from API
`curl` - curl for request API, for example
```shell
curl -i -X POST -H 'Content-Type: application/json' -H 'X-API-CLIENT: key' --data-binary '{"jsonrpc":"2.0","method":"info.getSomething","params":{"data":"something"},"id":1}' 'http://example.com/api/rpc/v1.0'
````timeRequest` - time in ms for request (for example 305 ms)
Also, you can add additional options to request. Support options: `headers`. It will be added to headers in constructor.
```js
jsonrpc.request('info.getSomething', { data: 'something' }, { headers: {'X-ACCESS_TOKEN': 'token' }});
```