1- import { type JSONRPCMethodSet } from './types.ts'
1+ import type { JSONRPCMethodSet , JSONRPCSettledResult } from './types.ts'
22import { JSONRPCNotification , JSONRPCRequest } from './dto/request.ts'
33import { JSONRPCErrorResponse , JSONRPCSuccessResponse } from './dto/response.ts'
44import { isJSONRPCResponse , JSONRPCResponse } from './dto/response.ts'
@@ -14,7 +14,7 @@ type JSONRPCAnyRequest =
1414 | Array < JSONRPCNotification | JSONRPCRequest >
1515
1616/**
17- * the client cannot parse the server response
17+ * The client cannot parse the server response
1818 */
1919export class JSONRPCClientParseError extends Error {
2020 name = 'JSONRPCClientParseError'
@@ -26,7 +26,7 @@ export class JSONRPCClientParseError extends Error {
2626}
2727
2828/**
29- * just wrap the JSON.parse function
29+ * Just wrap the JSON.parse function, with potential `JSONRPCClientParseError`
3030 */
3131function parseJSON (
3232 text : string ,
@@ -42,13 +42,22 @@ function parseJSON(
4242 }
4343}
4444
45+ /**
46+ * Provide a external `processor` function to the constructor,
47+ * it should accept a string (json encoded from one or more json rpc request)
48+ * and your customized code should send this string to a json rpc server
49+ * for any response represented as **string**
50+ *
51+ * The constructor optionally accept a customized id generator, otherwise it use a
52+ * self added number
53+ */
4554export class JSONRPCClient < MethodSet extends JSONRPCMethodSet > {
4655 /**
4756 * MUST be an infinite iterator
4857 */
4958 private idGenerator : IDGenerator
5059 /**
51- * the extern function to request the server for response
60+ * The extern function to request the server for response
5261 */
5362 private processor : ( input : string ) => Promise < string >
5463
@@ -84,19 +93,9 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {
8493 return notification
8594 }
8695
87- private processOneJsonValue (
88- jsonValue : unknown ,
89- associatedRequest : JSONRPCAnyRequest ,
90- ) : JSONRPCResponse {
91- if ( ! isJSONRPCResponse ( jsonValue ) ) {
92- throw new JSONRPCClientParseError (
93- `The server sent an incorrect response object` ,
94- associatedRequest ,
95- )
96- }
97- return jsonValue
98- }
99-
96+ /**
97+ * Send `JSONRPCRequest` to server, returns `JSONRPCValue` or throw `JSONRPCErrorInterface` (or `JSONRPCClientParseError`)
98+ */
10099 async request < T extends keyof MethodSet > (
101100 method : T extends string ? T : never ,
102101 params ?: Parameters < MethodSet [ T ] > [ 0 ] ,
@@ -105,15 +104,29 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {
105104 // responsed json string
106105 const jsonString = await this . processor ( JSON . stringify ( request ) )
107106 const jsonValue = parseJSON ( jsonString , request )
107+
108108 // parsed response
109- const response = this . processOneJsonValue ( jsonValue , request )
109+ if ( ! isJSONRPCResponse ( jsonValue ) ) {
110+ throw new JSONRPCClientParseError (
111+ `The server sent an incorrect response object` ,
112+ request ,
113+ )
114+ }
115+ // now jsonValue become JSONRPCResponse
116+ const response : JSONRPCResponse = jsonValue
117+
110118 if ( 'error' in response ) {
111- return Promise . reject ( response . error )
119+ throw response . error
112120 } else {
113- return response . result
121+ // response.result is now JSONRPCValue
122+ return response . result as ReturnType < MethodSet [ T ] >
114123 }
115124 }
116125
126+ /**
127+ * Send `JSONRPCNotification` to server, no returns,
128+ * only throws if your provided `processor` function throws
129+ */
117130 async notify < T extends keyof MethodSet > (
118131 method : T extends string ? T : never ,
119132 params ?: Parameters < MethodSet [ T ] > [ 0 ] ,
@@ -123,13 +136,12 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {
123136 }
124137
125138 /**
126- * You should use the createRequest() or createNotifaction() method to
139+ * You should use the ` createRequest()` or ` createNotifaction()` method to
127140 * create the requests array
128141 */
129142 async batch (
130143 ...requests : Array < JSONRPCRequest | JSONRPCNotification >
131- // deno-lint-ignore no-explicit-any
132- ) : Promise < PromiseSettledResult < any > [ ] | PromiseSettledResult < any > > {
144+ ) : Promise < JSONRPCSettledResult | JSONRPCSettledResult [ ] > {
133145 // responsed json string
134146 const jsonString = await this . processor ( JSON . stringify ( requests ) )
135147 const requestCount = requests . filter ( ( r ) => 'id' in r ) . length
@@ -166,7 +178,7 @@ export class JSONRPCClient<MethodSet extends JSONRPCMethodSet> {
166178
167179 if ( ! jsonValue . every ( isJSONRPCResponse ) ) {
168180 throw new JSONRPCClientParseError (
169- `The server returned batch response contains invalid value ` ,
181+ `The server returned batch response contains invalid one ` ,
170182 requests ,
171183 )
172184 }
0 commit comments