@asyncswap/jsonrpc 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +93 -31
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @asyncswap/jsonrpc
|
|
2
2
|
|
|
3
|
+
## 0.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9512db5: update documentation
|
|
8
|
+
|
|
9
|
+
## 0.5.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 7e8300c: changes api for jsonrpc contruction
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 43de065: update docs
|
|
18
|
+
- 7126afa: add `otherHeaders` to inializedClient calls
|
|
19
|
+
|
|
3
20
|
## 0.3.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,52 +1,114 @@
|
|
|
1
1
|
# jsonrpc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A minimal JSON-RPC 2.0 server and client library.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
```
|
|
7
|
+
```bash
|
|
8
8
|
bun add @asyncswap/jsonrpc
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Quick Start
|
|
12
12
|
|
|
13
|
-
###
|
|
13
|
+
### Server
|
|
14
14
|
|
|
15
|
-
```
|
|
16
|
-
import { JsonRpcServer } from
|
|
15
|
+
```typescript
|
|
16
|
+
import { JsonRpcServer } from '@asyncswap/jsonrpc';
|
|
17
17
|
|
|
18
|
-
const server = new JsonRpcServer()
|
|
18
|
+
const server = new JsonRpcServer();
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
server.register(
|
|
20
|
+
// Register methods
|
|
21
|
+
server.register('add', ([a, b]: [number, number]) => a + b);
|
|
22
|
+
server.register('ping', () => 'pong');
|
|
23
|
+
|
|
24
|
+
// Handle requests
|
|
25
|
+
const response = await server.handle({
|
|
26
|
+
jsonrpc: '2.0',
|
|
27
|
+
method: 'add',
|
|
28
|
+
params: [2, 3],
|
|
29
|
+
id: 1
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log(response); // { jsonrpc: '2.0', result: 5, id: 1 }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Client
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { JsonRpcClient } from '@asyncswap/jsonrpc';
|
|
39
|
+
|
|
40
|
+
const client = new JsonRpcClient('http://localhost:4444');
|
|
41
|
+
|
|
42
|
+
// Make a call
|
|
43
|
+
const result = await client.call('add', [2, 3]);
|
|
44
|
+
console.log(result); // 5
|
|
45
|
+
|
|
46
|
+
// Send a notification (no response expected)
|
|
47
|
+
await client.notify('log', ['Hello world']);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API Reference
|
|
51
|
+
|
|
52
|
+
### JsonRpcServer
|
|
53
|
+
|
|
54
|
+
#### Methods
|
|
55
|
+
|
|
56
|
+
- `register(method: string, handler: Handler<any>)` - Register a method handler
|
|
57
|
+
- `handle(request: any)` - Process a JSON-RPC request
|
|
58
|
+
|
|
59
|
+
#### Error Codes
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
enum JsonRpcErrorCodes {
|
|
63
|
+
INVALID_REQUEST = -32600,
|
|
64
|
+
METHOD_NOT_FOUND = -32601,
|
|
65
|
+
INVALID_PARAMS = -32602,
|
|
66
|
+
INTERNAL_ERROR = -32603,
|
|
67
|
+
PARSE_ERROR = -32700,
|
|
68
|
+
REQUEST_ABORTED = -32800,
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### JsonRpcClient
|
|
73
|
+
|
|
74
|
+
#### Constructor
|
|
75
|
+
|
|
76
|
+
- `new JsonRpcClient(url: string)` - Create a client instance
|
|
77
|
+
|
|
78
|
+
#### Methods
|
|
79
|
+
|
|
80
|
+
- `call<Method, Result, Error>(method, params?, headers?)` - Make a JSON-RPC call
|
|
81
|
+
- `notify<Method>(method, params?)` - Send a JSON-RPC notification
|
|
82
|
+
- `buildRequest(method, params?)` - Build a JSON-RPC request object
|
|
83
|
+
|
|
84
|
+
## Examples
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
// Server example
|
|
88
|
+
import { JsonRpcServer } from '@asyncswap/jsonrpc';
|
|
89
|
+
|
|
90
|
+
const server = new JsonRpcServer();
|
|
91
|
+
server.register('add', ([a, b]: [number, number]) => a + b);
|
|
22
92
|
|
|
23
93
|
Bun.serve({
|
|
24
94
|
port: 4444,
|
|
25
95
|
async fetch(req) {
|
|
26
|
-
if (req.method !== "POST") {
|
|
27
|
-
return new Response("JSON-RPC only", { status: 405 })
|
|
28
|
-
}
|
|
29
96
|
const payload = await req.json();
|
|
30
97
|
const response = await server.handle(payload);
|
|
31
|
-
if (!response) {
|
|
32
|
-
return new Response(null, { status: 204 })
|
|
33
|
-
}
|
|
34
98
|
return Response.json(response);
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
console.log("JSON-RPC running on http://localhost:4444")
|
|
99
|
+
},
|
|
100
|
+
});
|
|
39
101
|
```
|
|
40
102
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
"ping",
|
|
49
|
-
[]
|
|
50
|
-
);
|
|
51
|
-
console.log(result)
|
|
103
|
+
```typescript
|
|
104
|
+
// Client example
|
|
105
|
+
import { JsonRpcClient } from '@asyncswap/jsonrpc';
|
|
106
|
+
|
|
107
|
+
const client = new JsonRpcClient('http://localhost:4444');
|
|
108
|
+
const result = await client.call('add', [2, 3]);
|
|
109
|
+
console.log(result); // 5
|
|
52
110
|
```
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|