@psalomo/jsonrpc-client 0.3.0 → 0.5.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.
package/dist/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  // src/client.ts
2
2
  import {
3
3
  JsonRpcRequestSchema,
4
- JsonRpcResponseSchema
4
+ JsonRpcResponseSchema,
5
+ RPC_METHODS
5
6
  } from "@psalomo/jsonrpc-types";
6
7
  var JsonRpcClientError = class extends Error {
7
8
  constructor(message, code, data) {
@@ -58,7 +59,6 @@ var NearRpcClient = class {
58
59
  timeout;
59
60
  retries;
60
61
  validateResponses;
61
- requestIdCounter = 0;
62
62
  constructor(config) {
63
63
  if (typeof config === "string") {
64
64
  this.endpoint = config;
@@ -80,16 +80,17 @@ var NearRpcClient = class {
80
80
  }
81
81
  }
82
82
  /**
83
- * Generate a unique request ID
83
+ * Get request ID matching NEAR RPC documentation examples
84
84
  */
85
- generateRequestId() {
86
- return `near-rpc-${Date.now()}-${++this.requestIdCounter}`;
85
+ getRequestId() {
86
+ return "dontcare";
87
87
  }
88
88
  /**
89
89
  * Make a raw JSON-RPC call
90
+ * This method is public to allow dynamic calls to any RPC method
90
91
  */
91
92
  async call(method, params) {
92
- const requestId = this.generateRequestId();
93
+ const requestId = this.getRequestId();
93
94
  const snakeCaseParams = params ? convertKeysToSnakeCase(params) : void 0;
94
95
  const request = {
95
96
  jsonrpc: "2.0",
@@ -159,162 +160,38 @@ var NearRpcClient = class {
159
160
  lastError
160
161
  );
161
162
  }
162
- // Generated RPC methods will be added here
163
- // This section will be auto-generated based on the OpenAPI spec
164
- /**
165
- * Get node status
166
- */
167
- async status() {
168
- return this.call("status");
169
- }
170
- /**
171
- * Get block information
172
- */
173
- async block(params) {
174
- return this.call("block", params);
175
- }
176
- /**
177
- * Get current gas price
178
- */
179
- async gasPrice(params) {
180
- return this.call("gas_price", params);
181
- }
182
- /**
183
- * Get chunk information
184
- */
185
- async chunk(params) {
186
- return this.call("chunk", params);
187
- }
188
- /**
189
- * Health check
190
- */
191
- async health() {
192
- return this.call("health");
193
- }
194
- /**
195
- * Get network information
196
- */
197
- async networkInfo() {
198
- return this.call("network_info");
199
- }
200
- /**
201
- * Get current validators
202
- */
203
- async validators(params) {
204
- return this.call("validators", params);
205
- }
206
- /**
207
- * Get client configuration
208
- */
209
- async clientConfig() {
210
- return this.call("client_config");
211
- }
212
- /**
213
- * Broadcast transaction asynchronously
214
- */
215
- async broadcastTxAsync(params) {
216
- return this.call("broadcast_tx_async", params);
217
- }
218
- /**
219
- * Broadcast transaction and wait for commit
220
- */
221
- async broadcastTxCommit(params) {
222
- return this.call("broadcast_tx_commit", params);
223
- }
224
- /**
225
- * Send transaction
226
- */
227
- async sendTx(params) {
228
- return await this.call("send_tx", params);
229
- }
230
- /**
231
- * Get transaction status
232
- */
233
- async tx(params) {
234
- return this.call("tx", params);
235
- }
236
- /**
237
- * Query account/contract state
238
- */
239
- async query(params) {
240
- return this.call("query", params);
241
- }
242
- /**
243
- * View account information (convenience method)
244
- */
245
- async viewAccount(params) {
246
- return this.query({
247
- requestType: "view_account",
248
- ...params
249
- });
250
- }
251
- /**
252
- * View function call (convenience method)
253
- */
254
- async viewFunction(params) {
255
- return this.query({
256
- requestType: "call_function",
257
- ...params
258
- });
259
- }
260
- /**
261
- * View access key (convenience method)
262
- */
263
- async viewAccessKey(params) {
264
- return this.query({
265
- requestType: "view_access_key",
266
- ...params
267
- });
268
- }
269
- /**
270
- * Get light client proof
271
- */
272
- async lightClientProof(params) {
273
- return this.call("light_client_proof", params);
274
- }
275
- // Experimental methods
276
- /**
277
- * Get state changes (experimental)
278
- */
279
- async experimentalChanges(params) {
280
- return this.call("EXPERIMENTAL_changes", params);
281
- }
282
- /**
283
- * Get state changes in block (experimental)
284
- */
285
- async experimentalChangesInBlock(params) {
286
- return this.call("EXPERIMENTAL_changes_in_block", params);
287
- }
288
- /**
289
- * Get ordered validators (experimental)
290
- */
291
- async experimentalValidatorsOrdered(params) {
292
- return this.call("EXPERIMENTAL_validators_ordered", params);
293
- }
294
- /**
295
- * Get protocol configuration (experimental)
296
- */
297
- async experimentalProtocolConfig(params) {
298
- return this.call("EXPERIMENTAL_protocol_config", params);
299
- }
300
- /**
301
- * Get genesis configuration (experimental)
302
- */
303
- async experimentalGenesisConfig() {
304
- return this.call("EXPERIMENTAL_genesis_config");
305
- }
306
- /**
307
- * Get receipt information (experimental)
308
- */
309
- async experimentalReceipt(params) {
310
- return this.call("EXPERIMENTAL_receipt", params);
311
- }
312
- /**
313
- * Get transaction status (experimental)
314
- */
315
- async experimentalTxStatus(params) {
316
- return this.call("EXPERIMENTAL_tx_status", params);
163
+ };
164
+ RPC_METHODS.forEach((method) => {
165
+ let methodName = method;
166
+ if (methodName.startsWith("EXPERIMENTAL_")) {
167
+ methodName = "experimental" + methodName.substring(13).replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^([a-z])/, (_, letter) => letter.toUpperCase());
168
+ } else {
169
+ methodName = methodName.replace(
170
+ /_([a-z])/g,
171
+ (_, letter) => letter.toUpperCase()
172
+ );
317
173
  }
174
+ NearRpcClient.prototype[methodName] = function(params) {
175
+ return this.call(method, params);
176
+ };
177
+ });
178
+ NearRpcClient.prototype.viewAccount = function(params) {
179
+ return this.query({
180
+ requestType: "view_account",
181
+ ...params
182
+ });
183
+ };
184
+ NearRpcClient.prototype.viewFunction = function(params) {
185
+ return this.query({
186
+ requestType: "call_function",
187
+ ...params
188
+ });
189
+ };
190
+ NearRpcClient.prototype.viewAccessKey = function(params) {
191
+ return this.query({
192
+ requestType: "view_access_key",
193
+ ...params
194
+ });
318
195
  };
319
196
 
320
197
  // src/types.ts
@@ -328,10 +205,16 @@ var NearRpcError = class extends Error {
328
205
  };
329
206
 
330
207
  // src/index.ts
208
+ import {
209
+ JsonRpcRequestSchema as JsonRpcRequestSchema2,
210
+ JsonRpcResponseSchema as JsonRpcResponseSchema2
211
+ } from "@psalomo/jsonrpc-types";
331
212
  import { RPC_METHODS as RPC_METHODS2 } from "@psalomo/jsonrpc-types";
332
213
  export {
333
214
  JsonRpcClientError,
334
215
  JsonRpcNetworkError,
216
+ JsonRpcRequestSchema2 as JsonRpcRequestSchema,
217
+ JsonRpcResponseSchema2 as JsonRpcResponseSchema,
335
218
  NearRpcClient,
336
219
  NearRpcError,
337
220
  RPC_METHODS2 as RPC_METHODS,
@@ -0,0 +1,23 @@
1
+ export interface RpcRequest {
2
+ jsonrpc: '2.0';
3
+ id: string | number;
4
+ method: string;
5
+ params: unknown;
6
+ }
7
+ export interface RpcResponse<T = unknown> {
8
+ jsonrpc: '2.0';
9
+ id: string | number;
10
+ result?: T;
11
+ error?: RpcError;
12
+ }
13
+ export interface RpcError {
14
+ code: number;
15
+ message: string;
16
+ data?: unknown;
17
+ }
18
+ export declare class NearRpcError extends Error {
19
+ code: number;
20
+ data?: unknown | undefined;
21
+ constructor(code: number, message: string, data?: unknown | undefined);
22
+ }
23
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,KAAK,CAAC;IACf,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,OAAO;IACtC,OAAO,EAAE,KAAK,CAAC;IACf,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,CAAC;IACX,KAAK,CAAC,EAAE,QAAQ,CAAC;CAClB;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,qBAAa,YAAa,SAAQ,KAAK;IAE5B,IAAI,EAAE,MAAM;IAEZ,IAAI,CAAC,EAAE,OAAO;gBAFd,IAAI,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,EACR,IAAI,CAAC,EAAE,OAAO,YAAA;CAKxB"}
@@ -0,0 +1,12 @@
1
+ import { JsonRpcRequest, JsonRpcResponse } from './client.mini.js';
2
+ export interface ValidationResult {
3
+ validateRequest: (request: JsonRpcRequest) => void;
4
+ validateResponse: (response: JsonRpcResponse) => void;
5
+ }
6
+ /**
7
+ * Enable validation for the mini client
8
+ * This function should only be called if you want to include schema validation
9
+ * Calling this function will include Zod schemas in your bundle
10
+ */
11
+ export declare function enableValidation(): ValidationResult;
12
+ //# sourceMappingURL=validation.mini.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.mini.d.ts","sourceRoot":"","sources":["../src/validation.mini.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,cAAc,EACd,eAAe,EAGhB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,WAAW,gBAAgB;IAC/B,eAAe,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;IACnD,gBAAgB,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,CAAC;CACvD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,gBAAgB,CA0BnD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@psalomo/jsonrpc-client",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "TypeScript client for NEAR Protocol JSON-RPC API",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -10,13 +10,21 @@
10
10
  "types": "./dist/index.d.ts",
11
11
  "import": "./dist/index.mjs",
12
12
  "require": "./dist/index.js"
13
+ },
14
+ "./mini": {
15
+ "types": "./dist/index.mini.d.ts",
16
+ "import": "./dist/index.mini.mjs",
17
+ "require": "./dist/index.mini.cjs"
13
18
  }
14
19
  },
15
20
  "files": [
16
21
  "dist"
17
22
  ],
18
23
  "scripts": {
19
- "build": "tsup",
24
+ "prebuild": "cd ../../tools/codegen && npx tsx generate-client-interface.ts",
25
+ "build": "tsup && rollup -c",
26
+ "build:node": "tsup",
27
+ "build:browser": "rollup -c",
20
28
  "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
21
29
  "clean": "rm -rf dist",
22
30
  "typecheck": "tsc --noEmit",
@@ -27,15 +35,17 @@
27
35
  "test:coverage": "vitest run --coverage"
28
36
  },
29
37
  "dependencies": {
30
- "@psalomo/jsonrpc-types": "^0.1.0",
31
- "cross-fetch": "^4.0.0",
32
- "zod": "^3.22.4"
38
+ "@psalomo/jsonrpc-types": "^0.1.0"
33
39
  },
34
40
  "devDependencies": {
41
+ "@rollup/plugin-node-resolve": "^16.0.1",
42
+ "@rollup/plugin-terser": "^0.4.4",
43
+ "@rollup/plugin-typescript": "^12.1.4",
44
+ "@types/node": "^20.11.0",
35
45
  "prettier": "^3.2.5",
46
+ "rollup": "^4.45.1",
36
47
  "tsup": "^8.0.2",
37
48
  "typescript": "^5.3.3",
38
- "@types/node": "^20.11.0",
39
49
  "vitest": "^1.2.2"
40
50
  },
41
51
  "publishConfig": {