@@ -13,36 +13,36 @@ import {
1313} from './id.ts'
1414
1515/**
16- * Provide a external `requestForResponse` function to the constructor,
17- * it should accept a `string` (json encoded from one or more json rpc request)
18- * and your customized function should send this string to a json rpc server
19- * for any response represented as `string`
16+ * Provide an external `requestForResponse` function to the constructor.
17+ * This function should accept a `string` (JSON- encoded from one or more JSON-RPC requests),
18+ * and your custom implementation should send this string to a JSON-RPC server,
19+ * returning any response as a `string`.
2020 *
21- * The constructor optionally accept a customized id generator, otherwise it use a
22- * self added number
21+ * The constructor optionally accepts a custom ID generator; otherwise, it uses
22+ * an internally generated number.
2323 *
24- * To customize the request or response, you can extend `JSONRPCClient`.
24+ * To customize requests or responses, extend the `JSONRPCClient` class .
2525 *
26- * To customize request, overwrite `createRequest` and `createNotification` methods.
26+ * To customize requests, override the `createRequest` and `createNotification` methods.
2727 *
28- * To customize response, overwrite `request` `notify` and `batch` methods.
28+ * To customize responses, override the `request`, `notify`, and `batch` methods.
2929 */
3030export class JSONRPCClient <
3131 Methods extends JSONRPCMethods = JSONRPCMethods ,
3232> {
3333 /**
34- * MUST be an infinite iterator
34+ * MUST be an infinite iterator.
3535 */
3636 protected idGenerator : IDGenerator
3737 /**
38- * The external function to send the json string to any rpc server,
39- * and fetch for response as string
38+ * The external function that sends the JSON string to any RPC server
39+ * and fetches the response as a string.
4040 */
4141 protected requestForResponse : ( input : string ) => string | Promise < string >
4242
4343 /**
44- * @param requestForResponse An external function to send the json string to any rpc server,
45- * and fetch for response as string
44+ * @param requestForResponse An external function that sends the JSON string to any RPC server
45+ * and fetches the response as a string.
4646 */
4747 public constructor (
4848 requestForResponse : ( input : string ) => string | Promise < string > ,
@@ -68,6 +68,9 @@ export class JSONRPCClient<
6868 return request
6969 }
7070
71+ /**
72+ * Creates a JSON-RPC notification object.
73+ */
7174 public createNotification < T extends keyof Methods > (
7275 method : T extends string ? T : never ,
7376 params : Parameters < Methods [ T ] > [ 0 ] ,
@@ -80,7 +83,8 @@ export class JSONRPCClient<
8083 }
8184
8285 /**
83- * Send `JSONRPCRequest` to server, returns `JSONRPCValue` or throw `JSONRPCErrorInterface` (or `JSONRPCClientParseError`)
86+ * Sends a `JSONRPCRequest` to the server and returns a `JSONRPCValue`,
87+ * or throws a `JSONRPCErrorInterface` (or `JSONRPCClientParseError`).
8488 */
8589 public async request < T extends keyof Methods > (
8690 method : T extends string ? T : never ,
@@ -120,8 +124,8 @@ export class JSONRPCClient<
120124 }
121125
122126 /**
123- * Send `JSONRPCNotification` to server, no returns,
124- * only throws if your provided `processor` function throws
127+ * Sends a `JSONRPCNotification` to the server. No return value.
128+ * Throws only if your provided `processor` function throws.
125129 */
126130 public async notify < T extends keyof Methods > (
127131 method : T extends string ? T : never ,
@@ -132,14 +136,13 @@ export class JSONRPCClient<
132136 }
133137
134138 /**
135- * You should use the `createRequest()` or `createNotification()` method to
136- * create the requests array. Response order is always matched by id .
139+ * Use the `createRequest()` or `createNotification()` method to create the requests array.
140+ * Response order is always matched by ID .
137141 *
138- * Throws `JSONRPCClientParseError` if server response cannot be parsed,
139- * note that it does not throws for any `JSONRPCErrorResponse`, in this
140- * case it will be a single object: `{ status: 'rejected', reason: {...} }`
142+ * Throws `JSONRPCClientParseError` if the server response cannot be parsed.
143+ * Note: It does not throw for any `JSONRPCErrorResponse`; in this case, it will be a single object: `{ status: 'rejected', reason: {...} }`
141144 *
142- * Usually it returns be like (same as the `Promise.allSettled()` method) :
145+ * The return value is similar to the `Promise.allSettled()` method:
143146 * ```js
144147 * [
145148 * { status: 'fulfilled', value: '...' },
@@ -152,8 +155,9 @@ export class JSONRPCClient<
152155 * },
153156 * ]
154157 * ```
155- * @throws `JSONRPCError` - when server return single JSONRPCErrorResponse
156- * @throws `JSONRPCClientParseError` - when server response cannot be parsed
158+ * @throws `JSONRPCError` - when the server returns a single JSONRPCErrorResponse
159+ * @throws `JSONRPCClientParseError` - when the server response cannot be parsed
160+ * @see https://www.jsonrpc.org/specification#batch
157161 */
158162 public async batch (
159163 ...requests : Array < JSONRPCRequest | JSONRPCNotification >
@@ -244,13 +248,16 @@ export class JSONRPCClient<
244248 }
245249}
246250
251+ /**
252+ * A union type representing any JSON-RPC request, either a notification or a request.
253+ */
247254type JSONRPCAnyRequest =
248255 | JSONRPCNotification
249256 | JSONRPCRequest
250257 | Array < JSONRPCNotification | JSONRPCRequest >
251258
252259/**
253- * The client cannot parse the server response
260+ * Indicates that the client cannot parse the server response.
254261 */
255262export class JSONRPCClientParseError extends Error {
256263 override name = 'JSONRPCClientParseError'
@@ -262,7 +269,8 @@ export class JSONRPCClientParseError extends Error {
262269}
263270
264271/**
265- * Just wrap the JSON.parse function, with potential `JSONRPCClientParseError`
272+ * Wraps JSON.parse to provide better error handling.
273+ * Throws JSONRPCClientParseError if the response is not valid JSON.
266274 */
267275function parseJSON (
268276 text : string ,
0 commit comments