Skip to content

Commit 04ad50d

Browse files
committed
fix: rpc batch type errors
1 parent 01f48ef commit 04ad50d

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/batch/default.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { BatchClientOptions } from './interface';
1+
import { BatchClientInterface, BatchClientOptions } from './interface';
22
import { stringify } from '../utils/json';
33
import { RPC } from '../types';
44
import { 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
}

src/batch/interface.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@ export abstract class BatchClientInterface {
1919
params?: RPC.Methods[T]['params'],
2020
id?: string | number
2121
): Promise<
22-
JRPC.ResponseBody &
23-
(
24-
| {
25-
result?: RPC.Methods[T]['result'];
26-
}
27-
| {
28-
error?: RPC.Methods[T] extends { error: infer E } ? E : never;
29-
}
30-
)
22+
JRPC.ResponseBody & {
23+
result?: RPC.Methods[T]['result'];
24+
error?: JRPC.Error;
25+
}
3126
>;
3227
}

0 commit comments

Comments
 (0)