1- import { BatchClientOptions } from './interface' ;
1+ import { BatchClientInterface , BatchClientOptions } from './interface' ;
22import { stringify } from '../utils/json' ;
33import { RPC } from '../types' ;
44import { JRPC } from '../types/api' ;
55
6- export class BatchClient {
6+ export class BatchClient implements BatchClientInterface {
77 public nodeUrl : string ;
88
99 public headers : object ;
@@ -81,11 +81,13 @@ export class BatchClient {
8181 return raw . json ( ) ;
8282 }
8383
84- public async fetch < T extends keyof RPC . Methods > (
85- method : T ,
86- params ?: RPC . Methods [ T ] [ 'params' ] ,
87- id ?: string | number
88- ) {
84+ public async fetch <
85+ T extends keyof RPC . Methods ,
86+ TResponse extends JRPC . ResponseBody & {
87+ result ?: RPC . Methods [ T ] [ 'result' ] ;
88+ error ?: JRPC . Error ;
89+ } ,
90+ > ( method : T , params ?: RPC . Methods [ T ] [ 'params' ] , id ?: string | number ) : Promise < TResponse > {
8991 const requestId = this . addPendingRequest ( method , params , id ) ;
9092
9193 // Wait for the interval to pass before sending the batch
@@ -107,6 +109,9 @@ export class BatchClient {
107109 delete this . batchPromises [ requestId ] ;
108110
109111 // Find this request in the results and return it
110- return results . find ( ( result : any ) => result . id === requestId ) ;
112+ const result = results . find ( ( res : any ) => res . id === requestId ) ;
113+ if ( ! result ) throw new Error ( `Couldn't find the result for the request. Method: ${ method } ` ) ;
114+
115+ return result as TResponse ;
111116 }
112117}
0 commit comments