@psalomo/jsonrpc-client 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,395 @@
1
+ // src/client.ts
2
+ function camelToSnake(str) {
3
+ return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
4
+ }
5
+ function snakeToCamel(str) {
6
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
7
+ }
8
+ function convertKeysToSnakeCase(obj) {
9
+ if (obj === null || typeof obj !== "object") {
10
+ return obj;
11
+ }
12
+ if (Array.isArray(obj)) {
13
+ return obj.map(convertKeysToSnakeCase);
14
+ }
15
+ const converted = {};
16
+ for (const [key, value] of Object.entries(obj)) {
17
+ const snakeKey = camelToSnake(key);
18
+ converted[snakeKey] = convertKeysToSnakeCase(value);
19
+ }
20
+ return converted;
21
+ }
22
+ function convertKeysToCamelCase(obj) {
23
+ if (obj === null || typeof obj !== "object") {
24
+ return obj;
25
+ }
26
+ if (Array.isArray(obj)) {
27
+ return obj.map(convertKeysToCamelCase);
28
+ }
29
+ const converted = {};
30
+ for (const [key, value] of Object.entries(obj)) {
31
+ const camelKey = snakeToCamel(key);
32
+ converted[camelKey] = convertKeysToCamelCase(value);
33
+ }
34
+ return converted;
35
+ }
36
+ var REQUEST_ID = "dontcare";
37
+ var JsonRpcClientError = class extends Error {
38
+ constructor(message, code, data) {
39
+ super(message);
40
+ this.code = code;
41
+ this.data = data;
42
+ this.name = "JsonRpcClientError";
43
+ }
44
+ };
45
+ var JsonRpcNetworkError = class extends Error {
46
+ constructor(message, originalError, responseBody) {
47
+ super(message);
48
+ this.originalError = originalError;
49
+ this.responseBody = responseBody;
50
+ this.name = "JsonRpcNetworkError";
51
+ }
52
+ };
53
+ var NearRpcClient = class _NearRpcClient {
54
+ endpoint;
55
+ headers;
56
+ timeout;
57
+ retries;
58
+ validation;
59
+ constructor(config) {
60
+ if (typeof config === "string") {
61
+ this.endpoint = config;
62
+ this.headers = {};
63
+ this.timeout = 3e4;
64
+ this.retries = 3;
65
+ } else {
66
+ this.endpoint = config.endpoint;
67
+ this.headers = config.headers || {};
68
+ this.timeout = config.timeout || 3e4;
69
+ this.retries = config.retries || 3;
70
+ if (config.validation) {
71
+ this.validation = config.validation;
72
+ }
73
+ }
74
+ }
75
+ /**
76
+ * Make a raw JSON-RPC request
77
+ * This is used internally by the standalone RPC functions
78
+ */
79
+ async makeRequest(method, params) {
80
+ const requestForValidation = {
81
+ jsonrpc: "2.0",
82
+ id: REQUEST_ID,
83
+ method,
84
+ params: params !== void 0 ? params : null
85
+ };
86
+ if (this.validation) {
87
+ if ("validateMethodRequest" in this.validation) {
88
+ this.validation.validateMethodRequest(method, requestForValidation);
89
+ } else {
90
+ this.validation.validateRequest(requestForValidation);
91
+ }
92
+ }
93
+ const snakeCaseParams = params !== void 0 ? convertKeysToSnakeCase(params) : null;
94
+ const request = {
95
+ jsonrpc: "2.0",
96
+ id: REQUEST_ID,
97
+ method,
98
+ params: snakeCaseParams
99
+ };
100
+ let lastError = null;
101
+ for (let attempt = 0; attempt <= this.retries; attempt++) {
102
+ try {
103
+ const controller = new AbortController();
104
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
105
+ const response = await fetch(this.endpoint, {
106
+ method: "POST",
107
+ headers: {
108
+ "Content-Type": "application/json",
109
+ ...this.headers
110
+ },
111
+ body: JSON.stringify(request),
112
+ signal: controller.signal
113
+ });
114
+ clearTimeout(timeoutId);
115
+ let jsonResponse;
116
+ try {
117
+ jsonResponse = await response.json();
118
+ } catch (parseError) {
119
+ if (!response.ok) {
120
+ throw new JsonRpcNetworkError(
121
+ `HTTP error! status: ${response.status} - Failed to parse JSON response`,
122
+ parseError
123
+ );
124
+ }
125
+ throw new JsonRpcNetworkError(
126
+ "Failed to parse JSON response",
127
+ parseError
128
+ );
129
+ }
130
+ if (jsonResponse.error) {
131
+ throw new JsonRpcClientError(
132
+ jsonResponse.error.message,
133
+ jsonResponse.error.code,
134
+ jsonResponse.error.data
135
+ );
136
+ }
137
+ if (!response.ok) {
138
+ throw new JsonRpcNetworkError(
139
+ `HTTP error! status: ${response.status}`,
140
+ void 0,
141
+ jsonResponse
142
+ );
143
+ }
144
+ if (this.validation) {
145
+ this.validation.validateResponse(jsonResponse);
146
+ }
147
+ const camelCaseResult = jsonResponse.result ? convertKeysToCamelCase(jsonResponse.result) : jsonResponse.result;
148
+ if (camelCaseResult && typeof camelCaseResult === "object" && "error" in camelCaseResult) {
149
+ const errorMessage = camelCaseResult.error;
150
+ throw new JsonRpcClientError(
151
+ `RPC Error: ${errorMessage}`,
152
+ -32e3,
153
+ // Generic RPC error code
154
+ camelCaseResult
155
+ );
156
+ }
157
+ if (this.validation && "validateMethodResponse" in this.validation) {
158
+ const camelCaseResponse = {
159
+ ...jsonResponse,
160
+ result: camelCaseResult
161
+ };
162
+ this.validation.validateMethodResponse(method, camelCaseResponse);
163
+ }
164
+ return camelCaseResult;
165
+ } catch (error) {
166
+ lastError = error;
167
+ if (error instanceof JsonRpcClientError) {
168
+ throw error;
169
+ }
170
+ if (attempt === this.retries) {
171
+ break;
172
+ }
173
+ await new Promise(
174
+ (resolve) => setTimeout(resolve, Math.pow(2, attempt) * 1e3)
175
+ );
176
+ }
177
+ }
178
+ throw new JsonRpcNetworkError(
179
+ lastError?.message || "Request failed after all retries",
180
+ lastError || void 0
181
+ );
182
+ }
183
+ /**
184
+ * Create a new client with modified configuration
185
+ */
186
+ withConfig(config) {
187
+ return new _NearRpcClient({
188
+ endpoint: config.endpoint ?? this.endpoint,
189
+ headers: config.headers ?? this.headers,
190
+ timeout: config.timeout ?? this.timeout,
191
+ retries: config.retries ?? this.retries,
192
+ ...config.validation !== void 0 ? { validation: config.validation } : this.validation !== void 0 ? { validation: this.validation } : {}
193
+ });
194
+ }
195
+ };
196
+ var defaultClient = new NearRpcClient({
197
+ endpoint: "https://rpc.mainnet.near.org"
198
+ });
199
+
200
+ // src/types.ts
201
+ var NearRpcError = class extends Error {
202
+ constructor(code, message, data) {
203
+ super(message);
204
+ this.code = code;
205
+ this.data = data;
206
+ this.name = "NearRpcError";
207
+ }
208
+ };
209
+
210
+ // src/generated-types.ts
211
+ async function experimentalChanges(client, params) {
212
+ return client.makeRequest("EXPERIMENTAL_changes", params);
213
+ }
214
+ async function experimentalChangesInBlock(client, params) {
215
+ return client.makeRequest("EXPERIMENTAL_changes_in_block", params);
216
+ }
217
+ async function experimentalCongestionLevel(client, params) {
218
+ return client.makeRequest("EXPERIMENTAL_congestion_level", params);
219
+ }
220
+ async function experimentalGenesisConfig(client, params) {
221
+ return client.makeRequest("EXPERIMENTAL_genesis_config", params);
222
+ }
223
+ async function experimentalLightClientBlockProof(client, params) {
224
+ return client.makeRequest("EXPERIMENTAL_light_client_block_proof", params);
225
+ }
226
+ async function experimentalLightClientProof(client, params) {
227
+ return client.makeRequest("EXPERIMENTAL_light_client_proof", params);
228
+ }
229
+ async function experimentalMaintenanceWindows(client, params) {
230
+ return client.makeRequest("EXPERIMENTAL_maintenance_windows", params);
231
+ }
232
+ async function experimentalProtocolConfig(client, params) {
233
+ return client.makeRequest("EXPERIMENTAL_protocol_config", params);
234
+ }
235
+ async function experimentalReceipt(client, params) {
236
+ return client.makeRequest("EXPERIMENTAL_receipt", params);
237
+ }
238
+ async function experimentalSplitStorageInfo(client, params) {
239
+ return client.makeRequest("EXPERIMENTAL_split_storage_info", params);
240
+ }
241
+ async function experimentalTxStatus(client, params) {
242
+ return client.makeRequest("EXPERIMENTAL_tx_status", params);
243
+ }
244
+ async function experimentalValidatorsOrdered(client, params) {
245
+ return client.makeRequest("EXPERIMENTAL_validators_ordered", params);
246
+ }
247
+ async function block(client, params) {
248
+ return client.makeRequest("block", params);
249
+ }
250
+ async function blockEffects(client, params) {
251
+ return client.makeRequest("block_effects", params);
252
+ }
253
+ async function broadcastTxAsync(client, params) {
254
+ return client.makeRequest("broadcast_tx_async", params);
255
+ }
256
+ async function broadcastTxCommit(client, params) {
257
+ return client.makeRequest("broadcast_tx_commit", params);
258
+ }
259
+ async function changes(client, params) {
260
+ return client.makeRequest("changes", params);
261
+ }
262
+ async function chunk(client, params) {
263
+ return client.makeRequest("chunk", params);
264
+ }
265
+ async function clientConfig(client, params) {
266
+ return client.makeRequest("client_config", params);
267
+ }
268
+ async function gasPrice(client, params) {
269
+ return client.makeRequest("gas_price", params);
270
+ }
271
+ async function genesisConfig(client, params) {
272
+ return client.makeRequest("genesis_config", params);
273
+ }
274
+ async function health(client, params) {
275
+ return client.makeRequest("health", params);
276
+ }
277
+ async function lightClientProof(client, params) {
278
+ return client.makeRequest("light_client_proof", params);
279
+ }
280
+ async function maintenanceWindows(client, params) {
281
+ return client.makeRequest("maintenance_windows", params);
282
+ }
283
+ async function networkInfo(client, params) {
284
+ return client.makeRequest("network_info", params);
285
+ }
286
+ async function nextLightClientBlock(client, params) {
287
+ return client.makeRequest("next_light_client_block", params);
288
+ }
289
+ async function query(client, params) {
290
+ return client.makeRequest("query", params);
291
+ }
292
+ async function sendTx(client, params) {
293
+ return client.makeRequest("send_tx", params);
294
+ }
295
+ async function status(client, params) {
296
+ return client.makeRequest("status", params);
297
+ }
298
+ async function tx(client, params) {
299
+ return client.makeRequest("tx", params);
300
+ }
301
+ async function validators(client, params) {
302
+ return client.makeRequest("validators", params);
303
+ }
304
+
305
+ // src/convenience.ts
306
+ async function viewAccount(client, params) {
307
+ const queryParams = params.blockId ? {
308
+ requestType: "view_account",
309
+ accountId: params.accountId,
310
+ blockId: params.blockId
311
+ } : {
312
+ requestType: "view_account",
313
+ accountId: params.accountId,
314
+ finality: params.finality || "final"
315
+ };
316
+ return query(client, queryParams);
317
+ }
318
+ async function viewFunction(client, params) {
319
+ const baseParams = {
320
+ requestType: "call_function",
321
+ accountId: params.accountId,
322
+ methodName: params.methodName,
323
+ argsBase64: params.argsBase64 ?? ""
324
+ // Default to empty string if no arguments
325
+ };
326
+ const queryParams = params.blockId ? { ...baseParams, blockId: params.blockId } : { ...baseParams, finality: params.finality || "final" };
327
+ return query(client, queryParams);
328
+ }
329
+ async function viewAccessKey(client, params) {
330
+ const queryParams = params.blockId ? {
331
+ requestType: "view_access_key",
332
+ accountId: params.accountId,
333
+ publicKey: params.publicKey,
334
+ blockId: params.blockId
335
+ } : {
336
+ requestType: "view_access_key",
337
+ accountId: params.accountId,
338
+ publicKey: params.publicKey,
339
+ finality: params.finality || "final"
340
+ };
341
+ return query(client, queryParams);
342
+ }
343
+ function parseCallResultToJson(callResult) {
344
+ const bytes = new Uint8Array(callResult.result);
345
+ const text = new TextDecoder().decode(bytes);
346
+ return JSON.parse(text);
347
+ }
348
+ async function viewFunctionAsJson(client, params) {
349
+ const result = await viewFunction(client, params);
350
+ return parseCallResultToJson(result);
351
+ }
352
+
353
+ export {
354
+ JsonRpcClientError,
355
+ JsonRpcNetworkError,
356
+ NearRpcClient,
357
+ defaultClient,
358
+ NearRpcError,
359
+ experimentalChanges,
360
+ experimentalChangesInBlock,
361
+ experimentalCongestionLevel,
362
+ experimentalGenesisConfig,
363
+ experimentalLightClientBlockProof,
364
+ experimentalLightClientProof,
365
+ experimentalMaintenanceWindows,
366
+ experimentalProtocolConfig,
367
+ experimentalReceipt,
368
+ experimentalSplitStorageInfo,
369
+ experimentalTxStatus,
370
+ experimentalValidatorsOrdered,
371
+ block,
372
+ blockEffects,
373
+ broadcastTxAsync,
374
+ broadcastTxCommit,
375
+ changes,
376
+ chunk,
377
+ clientConfig,
378
+ gasPrice,
379
+ genesisConfig,
380
+ health,
381
+ lightClientProof,
382
+ maintenanceWindows,
383
+ networkInfo,
384
+ nextLightClientBlock,
385
+ query,
386
+ sendTx,
387
+ status,
388
+ tx,
389
+ validators,
390
+ viewAccount,
391
+ viewFunction,
392
+ viewAccessKey,
393
+ parseCallResultToJson,
394
+ viewFunctionAsJson
395
+ };