From dd7851dffdbf16d520694c073979e510a79ce316 Mon Sep 17 00:00:00 2001 From: YieldRay Date: Mon, 18 Dec 2023 22:02:13 +0800 Subject: [PATCH] add docs && add aria2c for test --- README.md | 13 ++++++++++++- src/client.ts | 23 ++++++++++++++++++++--- src/index.test.ts | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f2d88c4..15a6733 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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') +``` diff --git a/src/client.ts b/src/client.ts index 934e769..ad958a7 100644 --- a/src/client.ts +++ b/src/client.ts @@ -137,7 +137,25 @@ export class JSONRPCClient { /** * 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 @@ -185,8 +203,7 @@ export class JSONRPCClient { const unorderedResponses: JSONRPCResponse[] = jsonValue let errorStartIndex = 0 - // deno-lint-ignore no-explicit-any - const responses: PromiseSettledResult[] = [] // ordered + const responses: JSONRPCSettledResult[] = [] // ordered for (const request of requests) { if (!('id' in request)) { diff --git a/src/index.test.ts b/src/index.test.ts index 61d7e6c..b392704 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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 = { @@ -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' }) + }, +})