Skip to content

Commit dd7851d

Browse files
committed
add docs && add aria2c for test
1 parent d578e82 commit dd7851d

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![docs](https://shield.deno.dev/deno/docs)](https://doc.deno.land/https://github.com/YieldRay/json-rpc-ts/raw/main/src/index.ts)
44

5-
A strictly typed json-rpc implemention, zero dependency, minimal abstraction, with simple api
5+
A strictly typed json-rpc(2.0) implemention, zero dependency, minimal abstraction, with simple api
66

77
> Specification <https://www.jsonrpc.org/specification>
88
@@ -37,3 +37,14 @@ assertEquals(
3737
],
3838
)
3939
```
40+
41+
```ts
42+
const client = new JSONRPCClient((json) =>
43+
fetch('http://localhost:6800/jsonrpc', {
44+
method: 'POST',
45+
body: json,
46+
}).then((res) => res.text())
47+
)
48+
49+
const aria2cMethods = await client.request('system.listMethods')
50+
```

src/client.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,25 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {
137137

138138
/**
139139
* You should use the `createRequest()` or `createNotifaction()` method to
140-
* create the requests array
140+
* create the requests array.
141+
*
142+
* Throws `JSONRPCClientParseError` if server response cannot be parsed,
143+
* note that it does not throws for any `JSONRPCErrorResponse`, in this
144+
* case it will be a single object: `{ status: 'rejected', reason: {...} }`
145+
*
146+
* Usually it returns be like (same as the `Promise.allSettled()` method):
147+
* ```js
148+
* [
149+
* { status: 'fulfilled', value: '...' },
150+
* {
151+
* status: 'rejected',
152+
* reason: {
153+
* code: -32601,
154+
* message: 'Method not found',
155+
* },
156+
* },
157+
* ]
158+
* ```
141159
*/
142160
async batch(
143161
...requests: Array<JSONRPCRequest | JSONRPCNotification>
@@ -185,8 +203,7 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {
185203

186204
const unorderedResponses: JSONRPCResponse[] = jsonValue
187205
let errorStartIndex = 0
188-
// deno-lint-ignore no-explicit-any
189-
const responses: PromiseSettledResult<any>[] = [] // ordered
206+
const responses: JSONRPCSettledResult[] = [] // ordered
190207

191208
for (const request of requests) {
192209
if (!('id' in request)) {

src/index.test.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { assertEquals, assertObjectMatch } from 'std/assert/mod.ts'
2-
import { JSONRPCClient, JSONRPCServer } from './index.ts'
1+
import {
2+
assertEquals,
3+
assertInstanceOf,
4+
assertObjectMatch,
5+
} from 'std/assert/mod.ts'
6+
import {
7+
JSONRPCClient,
8+
JSONRPCFulfilledResult,
9+
JSONRPCServer,
10+
} from './index.ts'
311

412
Deno.test('JSONRPCClient/JSONRPCServer', async () => {
513
const methodSet = {
@@ -45,3 +53,31 @@ Deno.test('JSONRPCClient/JSONRPCServer', async () => {
4553
}],
4654
)
4755
})
56+
57+
Deno.test({
58+
name: 'JSONRPCClient/aria2',
59+
// also need to run `aria2c --enable-rpc`
60+
ignore: Deno.permissions.querySync({ name: 'net', host: 'localhost:6800' })
61+
.state !==
62+
'granted',
63+
fn: async () => {
64+
const client = new JSONRPCClient((json) =>
65+
fetch('http://localhost:6800/jsonrpc', {
66+
method: 'POST',
67+
body: json,
68+
}).then((res) => res.text())
69+
)
70+
71+
assertInstanceOf(await client.request('system.listMethods'), Array)
72+
73+
assertEquals(await client.notify('system.listMethods'), undefined)
74+
75+
const [r1, r2] = await client.batch(
76+
client.createRequest('system.listMethods'),
77+
client.createRequest('system.listMethods'),
78+
) as JSONRPCFulfilledResult[]
79+
80+
assertObjectMatch(r1, { status: 'fulfilled' })
81+
assertObjectMatch(r2, { status: 'fulfilled' })
82+
},
83+
})

0 commit comments

Comments
 (0)