Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit be0d6c0

Browse files
authored
changed startLedger and endLedger of getEvents to be string types (#36)
* changed startLedger and endLedger of getEvents to be string types for compatibility with rpc interface * removed params array usage if only one param value, rpc json unmarshal was not working when params was sent as array with one element * clean up the fix for passing string ledgers and object as params * #38: new jsonrpc.postObject method for getEvents to use
1 parent ca6fb07 commit be0d6c0

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/jsonrpc.ts

+25
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export interface Error<E = any> {
2727
data?: E;
2828
}
2929

30+
/**
31+
* Sends the jsonrpc 'params' as an array.
32+
*/
3033
export async function post<T>(
3134
url: string,
3235
method: string,
@@ -45,3 +48,25 @@ export async function post<T>(
4548
return response.data?.result;
4649
}
4750
}
51+
52+
/**
53+
* Sends the jsonrpc 'params' as the single 'param' obj, no array wrapper is applied.
54+
*/
55+
export async function postObject<T>(
56+
url: string,
57+
method: string,
58+
param: any,
59+
): Promise<T> {
60+
const response = await axios.post<Response<T>>(url, {
61+
jsonrpc: "2.0",
62+
// TODO: Generate a unique request id
63+
id: 1,
64+
method,
65+
params: param,
66+
});
67+
if (hasOwnProperty(response.data, "error")) {
68+
throw response.data.error;
69+
} else {
70+
return response.data?.result;
71+
}
72+
}

src/server.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ export class Server {
202202
*
203203
* @example
204204
* server.getEvents(
205-
* 1000,
206-
* 1010,
205+
* "1000",
206+
* "1010",
207207
* [
208208
* {
209209
* type: "contract",
@@ -243,9 +243,9 @@ export class Server {
243243
// The difficulty comes in matching up the correct integer primitives.
244244
//
245245
// It also means this library will rely on the XDR definitions.
246-
return await jsonrpc.post(this.serverURL.toString(), "getEvents", {
247-
startLedger,
248-
endLedger,
246+
return await jsonrpc.postObject(this.serverURL.toString(), "getEvents", {
247+
startLedger: String(startLedger),
248+
endLedger: String(endLedger),
249249
filters: filters ?? [],
250250
pagination: {
251251
...(cursor && { cursor }), // add fields only if defined

test/unit/server/get_events_test.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ describe("Server#getEvents", function() {
1616
setupMock(
1717
this.axiosMock,
1818
{
19-
endLedger: 2,
19+
endLedger: "2",
2020
filters: [],
2121
pagination: {},
22-
startLedger: 1,
22+
startLedger: "1",
2323
},
2424
result,
2525
);
@@ -41,8 +41,8 @@ describe("Server#getEvents", function() {
4141
setupMock(
4242
this.axiosMock,
4343
{
44-
startLedger: 1,
45-
endLedger: 10,
44+
startLedger: "1",
45+
endLedger: "10",
4646
filters: [
4747
{
4848
topics: [["*", "*"]],
@@ -76,8 +76,8 @@ describe("Server#getEvents", function() {
7676
setupMock(
7777
this.axiosMock,
7878
{
79-
startLedger: 1,
80-
endLedger: 10,
79+
startLedger: "1",
80+
endLedger: "10",
8181
filters: [
8282
{
8383
topics: [["AAAABQAAAAh0cmFuc2Zlcg==", "AAAAAQB6Mcc="]],
@@ -109,8 +109,8 @@ describe("Server#getEvents", function() {
109109
setupMock(
110110
this.axiosMock,
111111
{
112-
startLedger: 1,
113-
endLedger: 2,
112+
startLedger: "1",
113+
endLedger: "2",
114114
filters: [
115115
{
116116
topics: [["AAAABQAAAAh0cmFuc2Zlcg==", "*"]],
@@ -142,8 +142,8 @@ describe("Server#getEvents", function() {
142142
setupMock(
143143
this.axiosMock,
144144
{
145-
startLedger: 1,
146-
endLedger: 2,
145+
startLedger: "1",
146+
endLedger: "2",
147147
filters: [
148148
{
149149
topics: [["*", "*"]],
@@ -196,7 +196,7 @@ function setupMock(axiosMock, params, result) {
196196
jsonrpc: "2.0",
197197
id: 1,
198198
method: "getEvents",
199-
params: [params],
199+
params: params,
200200
})
201201
.returns(Promise.resolve({ data: { result } }));
202202
}

0 commit comments

Comments
 (0)