@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.
Files changed (3) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +93 -31
  3. 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
- ## `@asyncswap/jsonrpc`
3
+ A minimal JSON-RPC 2.0 server and client library.
4
4
 
5
- A minimal jsonrpc server and client library.
5
+ ## Installation
6
6
 
7
- ```sh
7
+ ```bash
8
8
  bun add @asyncswap/jsonrpc
9
9
  ```
10
10
 
11
- ## Usage
11
+ ## Quick Start
12
12
 
13
- ### RPC Server
13
+ ### Server
14
14
 
15
- ```ts
16
- import { JsonRpcServer } from "@asyncswap/jsonrpc";
15
+ ```typescript
16
+ import { JsonRpcServer } from '@asyncswap/jsonrpc';
17
17
 
18
- const server = new JsonRpcServer()
18
+ const server = new JsonRpcServer();
19
19
 
20
- server.register("add", ([a,b]: [number, number]) => a + b)
21
- server.register("ping", () => "pong")
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
- ### RPC Client
42
-
43
- ```ts
44
- import { initializeRpcClient } from "@asyncswap/jsonrpc";
45
- const url = "http://localhost:4444";
46
- const client = initializeRpcClient(url);
47
- const result = await client.call(
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
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@asyncswap/jsonrpc",
3
3
  "description": "A minimal jsonrpc spec implementation.",
4
4
  "author": "Meek Msaki",
5
- "version": "0.4.0",
5
+ "version": "0.4.2",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.mjs",