Skip to content

Commit

Permalink
add docs && add aria2c for test
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Dec 18, 2023
1 parent d578e82 commit dd7851d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

> Specification <https://www.jsonrpc.org/specification>
Expand Down Expand Up @@ -37,3 +37,14 @@ assertEquals(
],
)
```

```ts
const client = new JSONRPCClient((json) =>
fetch('http://localhost:6800/jsonrpc', {
method: 'POST',
body: json,
}).then((res) => res.text())
)

const aria2cMethods = await client.request('system.listMethods')
```
23 changes: 20 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,25 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {

/**
* You should use the `createRequest()` or `createNotifaction()` method to
* create the requests array
* create the requests array.
*
* Throws `JSONRPCClientParseError` if server response cannot be parsed,
* note that it does not throws for any `JSONRPCErrorResponse`, in this
* case it will be a single object: `{ status: 'rejected', reason: {...} }`
*
* Usually it returns be like (same as the `Promise.allSettled()` method):
* ```js
* [
* { status: 'fulfilled', value: '...' },
* {
* status: 'rejected',
* reason: {
* code: -32601,
* message: 'Method not found',
* },
* },
* ]
* ```
*/
async batch(
...requests: Array<JSONRPCRequest | JSONRPCNotification>
Expand Down Expand Up @@ -185,8 +203,7 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {

const unorderedResponses: JSONRPCResponse[] = jsonValue
let errorStartIndex = 0
// deno-lint-ignore no-explicit-any
const responses: PromiseSettledResult<any>[] = [] // ordered
const responses: JSONRPCSettledResult[] = [] // ordered

for (const request of requests) {
if (!('id' in request)) {
Expand Down
40 changes: 38 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { assertEquals, assertObjectMatch } from 'std/assert/mod.ts'
import { JSONRPCClient, JSONRPCServer } from './index.ts'
import {
assertEquals,
assertInstanceOf,
assertObjectMatch,
} from 'std/assert/mod.ts'
import {
JSONRPCClient,
JSONRPCFulfilledResult,
JSONRPCServer,
} from './index.ts'

Deno.test('JSONRPCClient/JSONRPCServer', async () => {
const methodSet = {
Expand Down Expand Up @@ -45,3 +53,31 @@ Deno.test('JSONRPCClient/JSONRPCServer', async () => {
}],
)
})

Deno.test({
name: 'JSONRPCClient/aria2',
// also need to run `aria2c --enable-rpc`
ignore: Deno.permissions.querySync({ name: 'net', host: 'localhost:6800' })
.state !==
'granted',
fn: async () => {
const client = new JSONRPCClient((json) =>
fetch('http://localhost:6800/jsonrpc', {
method: 'POST',
body: json,
}).then((res) => res.text())
)

assertInstanceOf(await client.request('system.listMethods'), Array)

assertEquals(await client.notify('system.listMethods'), undefined)

const [r1, r2] = await client.batch(
client.createRequest('system.listMethods'),
client.createRequest('system.listMethods'),
) as JSONRPCFulfilledResult[]

assertObjectMatch(r1, { status: 'fulfilled' })
assertObjectMatch(r2, { status: 'fulfilled' })
},
})

0 comments on commit dd7851d

Please sign in to comment.