@psalomo/jsonrpc-client 0.5.0 → 1.0.1
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 +62 -14
- package/dist/browser-standalone.js +1685 -3655
- package/dist/browser-standalone.min.js +1 -1
- package/dist/client.d.ts +21 -20
- package/dist/client.d.ts.map +1 -1
- package/dist/{convenience.mini.d.ts → convenience.d.ts} +3 -3
- package/dist/convenience.d.ts.map +1 -0
- package/dist/{generated-functions.mini.d.ts → generated-functions.d.ts} +3 -3
- package/dist/generated-functions.d.ts.map +1 -0
- package/dist/generated-types.d.ts +29 -0
- package/dist/generated-types.d.ts.map +1 -1
- package/dist/index.d.mts +133 -71
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +335 -104
- package/dist/index.mjs +307 -109
- package/dist/{validation.mini.d.ts → validation.d.ts} +5 -3
- package/dist/validation.d.ts.map +1 -0
- package/package.json +2 -7
- package/dist/browser-standalone-mini.js +0 -1245
- package/dist/browser-standalone-mini.min.js +0 -1
- package/dist/client.mini.d.ts +0 -58
- package/dist/client.mini.d.ts.map +0 -1
- package/dist/convenience.mini.d.ts.map +0 -1
- package/dist/generated-functions.mini.d.ts.map +0 -1
- package/dist/generated-types.mini.d.ts +0 -84
- package/dist/generated-types.mini.d.ts.map +0 -1
- package/dist/index.mini.cjs +0 -421
- package/dist/index.mini.d.mts +0 -198
- package/dist/index.mini.d.ts +0 -10
- package/dist/index.mini.d.ts.map +0 -1
- package/dist/index.mini.mjs +0 -361
- package/dist/validation.mini.d.ts.map +0 -1
package/README.md
CHANGED
@@ -5,23 +5,23 @@ This package provides a fully-typed, dynamic client for the NEAR Protocol JSON-R
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
```bash
|
8
|
-
|
8
|
+
npm install @near-js/jsonrpc-client
|
9
9
|
```
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
Create a new client instance and
|
13
|
+
Create a new client instance and use the available RPC functions:
|
14
14
|
|
15
15
|
```typescript
|
16
|
-
import { NearRpcClient } from '@near-js/jsonrpc-client';
|
16
|
+
import { NearRpcClient, status } from '@near-js/jsonrpc-client';
|
17
17
|
|
18
18
|
const client = new NearRpcClient({
|
19
|
-
endpoint: 'https://rpc.mainnet.near.org'
|
19
|
+
endpoint: 'https://rpc.mainnet.near.org',
|
20
20
|
});
|
21
21
|
|
22
22
|
async function getNetworkStatus() {
|
23
|
-
const
|
24
|
-
console.log('Network status:',
|
23
|
+
const result = await status(client);
|
24
|
+
console.log('Network status:', result);
|
25
25
|
}
|
26
26
|
|
27
27
|
getNetworkStatus();
|
@@ -32,15 +32,15 @@ getNetworkStatus();
|
|
32
32
|
All method calls return a promise that resolves to a fully typed result object based on the JSON-RPC API specification.
|
33
33
|
|
34
34
|
```typescript
|
35
|
-
import { NearRpcClient } from '@near-js/jsonrpc-client';
|
35
|
+
import { NearRpcClient, block } from '@near-js/jsonrpc-client';
|
36
36
|
|
37
37
|
const client = new NearRpcClient({
|
38
|
-
endpoint: 'https://rpc.mainnet.near.org'
|
38
|
+
endpoint: 'https://rpc.mainnet.near.org',
|
39
39
|
});
|
40
40
|
|
41
41
|
async function getLatestBlock() {
|
42
|
-
const
|
43
|
-
console.log('Latest block height:',
|
42
|
+
const result = await block(client, { finality: 'final' });
|
43
|
+
console.log('Latest block height:', result.header?.height);
|
44
44
|
}
|
45
45
|
|
46
46
|
getLatestBlock();
|
@@ -54,7 +54,7 @@ The client includes convenience methods for common query operations:
|
|
54
54
|
// View account information
|
55
55
|
const account = await client.viewAccount({
|
56
56
|
accountId: 'example.near',
|
57
|
-
finality: 'final'
|
57
|
+
finality: 'final',
|
58
58
|
});
|
59
59
|
console.log('Account balance:', account.amount);
|
60
60
|
console.log('Storage used:', account.storageUsage);
|
@@ -63,14 +63,61 @@ console.log('Storage used:', account.storageUsage);
|
|
63
63
|
const result = await client.viewFunction({
|
64
64
|
accountId: 'contract.near',
|
65
65
|
methodName: 'get_balance',
|
66
|
-
finality: 'final'
|
66
|
+
finality: 'final',
|
67
67
|
});
|
68
68
|
|
69
69
|
// View access keys
|
70
70
|
const accessKey = await client.viewAccessKey({
|
71
71
|
accountId: 'example.near',
|
72
72
|
publicKey: 'ed25519:...',
|
73
|
-
finality: 'final'
|
73
|
+
finality: 'final',
|
74
|
+
});
|
75
|
+
```
|
76
|
+
|
77
|
+
## Runtime Validation
|
78
|
+
|
79
|
+
The client supports runtime validation using Zod schemas to ensure both request parameters and server responses conform to the NEAR RPC specification. **We strongly recommend enabling validation** to catch errors early and ensure data integrity.
|
80
|
+
|
81
|
+
```typescript
|
82
|
+
import { NearRpcClient, status, block, enableValidation } from '@near-js/jsonrpc-client';
|
83
|
+
|
84
|
+
// Create client with validation enabled (recommended)
|
85
|
+
const client = new NearRpcClient({
|
86
|
+
endpoint: 'https://rpc.mainnet.near.org',
|
87
|
+
validation: enableValidation()
|
88
|
+
});
|
89
|
+
|
90
|
+
// Request parameters are validated before sending
|
91
|
+
try {
|
92
|
+
await block(client, { blockId: 'invalid' }); // ❌ Throws validation error
|
93
|
+
await block(client, { finality: 'final' }); // ✅ Valid parameters
|
94
|
+
} catch (error) {
|
95
|
+
console.error('Validation error:', error.message);
|
96
|
+
// "Invalid block request: Expected finality or block_id"
|
97
|
+
}
|
98
|
+
|
99
|
+
// Server responses are also validated
|
100
|
+
const result = await status(client);
|
101
|
+
// You can trust that 'result' matches the expected schema
|
102
|
+
```
|
103
|
+
|
104
|
+
### Benefits of Validation
|
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
|
111
|
+
|
112
|
+
### Disabling Validation
|
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`.
|
115
|
+
|
116
|
+
```typescript
|
117
|
+
// Client without validation (smaller bundle, but less safe)
|
118
|
+
const client = new NearRpcClient({
|
119
|
+
endpoint: 'https://rpc.mainnet.near.org'
|
120
|
+
// No validation property = no runtime checks
|
74
121
|
});
|
75
122
|
```
|
76
123
|
|
@@ -79,5 +126,6 @@ const accessKey = await client.viewAccessKey({
|
|
79
126
|
- **🔧 Dynamic methods**: All 28+ RPC methods automatically available
|
80
127
|
- **📝 Fully typed**: Complete TypeScript support with proper request/response types
|
81
128
|
- **🔄 Auto-updating**: New API methods appear automatically without code changes
|
82
|
-
- **✅ Runtime validation**:
|
129
|
+
- **✅ Runtime validation**: Validates both request parameters and server responses
|
83
130
|
- **🎯 Convenience methods**: Simplified methods for common operations like `viewAccount`
|
131
|
+
- **🌳 Tree-shakable**: Validation is modular - only included when used
|