@@ -253,14 +253,22 @@ export function createServer<T extends Methods, C = void>(
253
253
/**
254
254
* Map methods to valid client methods.
255
255
*/
256
- type ClientMethods < T extends Methods > = {
256
+ export type ClientRequests < T extends Methods > = {
257
257
[ K in keyof T ] : {
258
258
method : K ;
259
259
params : TypeOf < T [ K ] [ "request" ] > ;
260
260
async ?: boolean ;
261
261
} ;
262
262
} [ keyof T & string ] ;
263
263
264
+ /**
265
+ * Map client requests to response types.
266
+ */
267
+ export type ClientResponse <
268
+ T extends Methods ,
269
+ P extends ClientRequests < T >
270
+ > = P [ "async" ] extends true ? undefined : TypeOf < T [ P [ "method" ] ] [ "response" ] > ;
271
+
264
272
/**
265
273
* Configure client options.
266
274
*/
@@ -283,7 +291,7 @@ export function createClient<T extends Methods, U = void>(
283
291
let counter = 0 ;
284
292
const jsonrpc = "2.0" ;
285
293
286
- function prepare < U extends ClientMethods < T > > ( payload : U ) {
294
+ function prepare < U extends ClientRequests < T > > ( payload : U ) {
287
295
const { method, params, async } = payload ;
288
296
const { request, response } = methods [ method ] ;
289
297
@@ -334,28 +342,24 @@ export function createClient<T extends Methods, U = void>(
334
342
} ;
335
343
}
336
344
337
- async function rpcClient < P extends ClientMethods < T > > (
345
+ async function rpcClient < P extends ClientRequests < T > > (
338
346
payload : P ,
339
347
options : U
340
- ) : Promise <
341
- P [ "async" ] extends true ? undefined : TypeOf < T [ P [ "method" ] ] [ "response" ] >
342
- > {
348
+ ) : Promise < ClientResponse < T , P > > {
343
349
const { params, id, method, process } = prepare ( payload ) ;
344
350
const data = await send ( { jsonrpc, method, params, id } , options ) ;
345
351
const response = process ( data ) as any ;
346
352
if ( response instanceof RpcError ) throw response ; // Throw RPC errors.
347
353
return response ;
348
354
}
349
355
350
- rpcClient . many = async < P extends ClientMethods < T > [ ] > (
356
+ rpcClient . many = async < P extends ClientRequests < T > [ ] > (
351
357
payload : P ,
352
358
options : U
353
359
) : Promise <
354
360
{
355
- [ K in keyof P ] : P [ K ] extends ClientMethods < T >
356
- ? P [ K ] [ "async" ] extends true
357
- ? undefined
358
- : TypeOf < T [ P [ K ] [ "method" ] ] [ "response" ] > | RpcError
361
+ [ K in keyof P ] : K extends number
362
+ ? ClientResponse < T , P [ K ] > | RpcError
359
363
: P [ K ] ;
360
364
}
361
365
> => {
0 commit comments