@psalomo/jsonrpc-client 1.1.0 → 1.2.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/README.md +69 -19
- package/dist/browser-standalone.js +782 -37
- package/dist/browser-standalone.min.js +1 -1
- package/dist/chunk-GCIU6ZLN.mjs +395 -0
- package/dist/convenience-DFvZCHOk.d.mts +212 -0
- package/dist/convenience-DFvZCHOk.d.ts +212 -0
- package/dist/convenience.d.ts +9 -1
- package/dist/convenience.d.ts.map +1 -1
- package/dist/generated-types.d.ts +1 -1
- package/dist/index.d.mts +23 -172
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +626 -51
- package/dist/index.mjs +673 -340
- package/dist/no-validation/index.d.mts +6 -0
- package/dist/no-validation/index.d.ts +9 -0
- package/dist/no-validation/index.d.ts.map +1 -0
- package/dist/no-validation/index.js +471 -0
- package/dist/no-validation/index.mjs +95 -0
- package/dist/validated/index.d.ts +54 -0
- package/dist/validated/index.d.ts.map +1 -0
- package/package.json +7 -2
package/README.md
CHANGED
@@ -51,8 +51,10 @@ getLatestBlock();
|
|
51
51
|
The client includes convenience methods for common query operations:
|
52
52
|
|
53
53
|
```typescript
|
54
|
+
import { viewAccount, viewFunction, viewAccessKey } from '@near-js/jsonrpc-client';
|
55
|
+
|
54
56
|
// View account information
|
55
|
-
const account = await
|
57
|
+
const account = await viewAccount(client, {
|
56
58
|
accountId: 'example.near',
|
57
59
|
finality: 'final',
|
58
60
|
});
|
@@ -60,31 +62,66 @@ console.log('Account balance:', account.amount);
|
|
60
62
|
console.log('Storage used:', account.storageUsage);
|
61
63
|
|
62
64
|
// Call view functions
|
63
|
-
const result = await
|
65
|
+
const result = await viewFunction(client, {
|
64
66
|
accountId: 'contract.near',
|
65
67
|
methodName: 'get_balance',
|
66
68
|
finality: 'final',
|
67
69
|
});
|
68
70
|
|
69
71
|
// View access keys
|
70
|
-
const accessKey = await
|
72
|
+
const accessKey = await viewAccessKey(client, {
|
71
73
|
accountId: 'example.near',
|
72
74
|
publicKey: 'ed25519:...',
|
73
75
|
finality: 'final',
|
74
76
|
});
|
75
77
|
```
|
76
78
|
|
79
|
+
### JSON Parsing Utilities
|
80
|
+
|
81
|
+
Many NEAR contracts return JSON data as byte arrays. We provide convenient utilities to parse these:
|
82
|
+
|
83
|
+
```typescript
|
84
|
+
import { viewFunction, viewFunctionAsJson, parseCallResultToJson } from '@near-js/jsonrpc-client';
|
85
|
+
|
86
|
+
// Manual parsing
|
87
|
+
const result = await viewFunction(client, {
|
88
|
+
accountId: 'contract.near',
|
89
|
+
methodName: 'get_status',
|
90
|
+
});
|
91
|
+
const data = parseCallResultToJson(result); // Converts byte array to JSON
|
92
|
+
|
93
|
+
// Or use the convenience function that does both
|
94
|
+
const data = await viewFunctionAsJson(client, {
|
95
|
+
accountId: 'contract.near',
|
96
|
+
methodName: 'get_status',
|
97
|
+
});
|
98
|
+
|
99
|
+
// With TypeScript types
|
100
|
+
interface Status {
|
101
|
+
version: string;
|
102
|
+
uptime: number;
|
103
|
+
}
|
104
|
+
const status = await viewFunctionAsJson<Status>(client, {
|
105
|
+
accountId: 'contract.near',
|
106
|
+
methodName: 'get_status',
|
107
|
+
});
|
108
|
+
console.log(status.version); // Fully typed!
|
109
|
+
```
|
110
|
+
|
77
111
|
## Runtime Validation
|
78
112
|
|
79
|
-
The client supports runtime validation using Zod schemas to ensure both request parameters and server responses conform to the NEAR RPC specification.
|
113
|
+
The client supports runtime validation using Zod schemas to ensure both request parameters and server responses conform to the NEAR RPC specification.
|
114
|
+
|
115
|
+
### Default Usage (With Validation)
|
116
|
+
|
117
|
+
**By default, all functions include validation for maximum safety:**
|
80
118
|
|
81
119
|
```typescript
|
82
|
-
import { NearRpcClient, status, block
|
120
|
+
import { NearRpcClient, status, block } from '@near-js/jsonrpc-client';
|
83
121
|
|
84
|
-
//
|
122
|
+
// Just create a client - validation is built into the functions
|
85
123
|
const client = new NearRpcClient({
|
86
124
|
endpoint: 'https://rpc.mainnet.near.org',
|
87
|
-
validation: enableValidation()
|
88
125
|
});
|
89
126
|
|
90
127
|
// Request parameters are validated before sending
|
@@ -101,26 +138,39 @@ const result = await status(client);
|
|
101
138
|
// You can trust that 'result' matches the expected schema
|
102
139
|
```
|
103
140
|
|
104
|
-
###
|
105
|
-
|
106
|
-
- **Request Safety**: Catch parameter errors before making network requests
|
107
|
-
- **Response Integrity**: Ensure server responses match the expected schema
|
108
|
-
- **Better Error Messages**: Get clear, descriptive errors instead of cryptic failures
|
109
|
-
- **Development Speed**: Find API usage mistakes immediately during development
|
110
|
-
- **Runtime Protection**: Guard against malformed or unexpected server responses
|
141
|
+
### Minimal Bundle Size (No Validation)
|
111
142
|
|
112
|
-
|
113
|
-
|
114
|
-
For applications where bundle size is critical, validation can be disabled. The validation code is tree-shakable and only included when you import `enableValidation`.
|
143
|
+
For applications where bundle size is critical, use the `/no-validation` export:
|
115
144
|
|
116
145
|
```typescript
|
117
|
-
|
146
|
+
import { NearRpcClient, status, block } from '@near-js/jsonrpc-client/no-validation';
|
147
|
+
|
148
|
+
// Same API, but no runtime validation
|
118
149
|
const client = new NearRpcClient({
|
119
150
|
endpoint: 'https://rpc.mainnet.near.org'
|
120
|
-
// No validation property = no runtime checks
|
121
151
|
});
|
152
|
+
|
153
|
+
// No validation = smaller bundle size
|
154
|
+
await block(client, { finality: 'final' });
|
122
155
|
```
|
123
156
|
|
157
|
+
### Bundle Size Comparison
|
158
|
+
|
159
|
+
The new validation approach uses **per-function schema imports** for optimal tree-shaking:
|
160
|
+
|
161
|
+
- **With validation**: ~60KB for 2 functions (only includes schemas for used functions)
|
162
|
+
- **No validation**: ~7KB (no Zod schemas included)
|
163
|
+
- **Old approach**: ~150KB+ (included all schemas even for unused functions)
|
164
|
+
|
165
|
+
### Benefits of Validation
|
166
|
+
|
167
|
+
- **Request Safety**: Catch parameter errors before making network requests
|
168
|
+
- **Response Integrity**: Ensure server responses match the expected schema
|
169
|
+
- **Better Error Messages**: Get clear, descriptive errors instead of cryptic failures
|
170
|
+
- **Development Speed**: Find API usage mistakes immediately during development
|
171
|
+
- **Runtime Protection**: Guard against malformed or unexpected server responses
|
172
|
+
- **Optimal Tree-Shaking**: Only pay for validation of functions you actually use
|
173
|
+
|
124
174
|
## Features
|
125
175
|
|
126
176
|
- **🔧 Dynamic methods**: All 28+ RPC methods automatically available
|