@psalomo/jsonrpc-client 0.4.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.
@@ -0,0 +1,198 @@
1
+ import { RpcStateChangesInBlockByTypeRequest, RpcStateChangesInBlockResponse, RpcStateChangesInBlockRequest, RpcStateChangesInBlockByTypeResponse, RpcCongestionLevelRequest, RpcCongestionLevelResponse, GenesisConfigRequest, GenesisConfig, RpcLightClientBlockProofRequest, RpcLightClientBlockProofResponse, RpcLightClientExecutionProofRequest, RpcLightClientExecutionProofResponse, RpcMaintenanceWindowsRequest, EXPERIMENTALMaintenanceWindowsResponse, RpcProtocolConfigRequest, RpcProtocolConfigResponse, RpcReceiptRequest, RpcReceiptResponse, RpcSplitStorageInfoRequest, RpcSplitStorageInfoResponse, RpcTransactionStatusRequest, RpcTransactionResponse, RpcValidatorsOrderedRequest, EXPERIMENTALValidatorsOrderedResponse, RpcBlockRequest, RpcBlockResponse, RpcSendTransactionRequest, CryptoHash, RpcChunkRequest, RpcChunkResponse, RpcClientConfigRequest, RpcClientConfigResponse, RpcGasPriceRequest, RpcGasPriceResponse, RpcHealthRequest, RpcHealthResponse, RpcNetworkInfoRequest, RpcNetworkInfoResponse, RpcLightClientNextBlockRequest, RpcLightClientNextBlockResponse, RpcQueryRequest, RpcQueryResponse, RpcStatusRequest, RpcStatusResponse, RpcValidatorRequest, RpcValidatorResponse, AccountView, CallResult, AccessKeyView } from '@near-js/jsonrpc-types';
2
+ import { RpcStateChangesInBlockByTypeRequest as RpcStateChangesInBlockByTypeRequest$1, RpcStateChangesInBlockResponse as RpcStateChangesInBlockResponse$1, RpcStateChangesInBlockRequest as RpcStateChangesInBlockRequest$1, RpcStateChangesInBlockByTypeResponse as RpcStateChangesInBlockByTypeResponse$1, RpcCongestionLevelRequest as RpcCongestionLevelRequest$1, RpcCongestionLevelResponse as RpcCongestionLevelResponse$1, GenesisConfigRequest as GenesisConfigRequest$1, GenesisConfig as GenesisConfig$1, RpcLightClientBlockProofRequest as RpcLightClientBlockProofRequest$1, RpcLightClientBlockProofResponse as RpcLightClientBlockProofResponse$1, RpcLightClientExecutionProofRequest as RpcLightClientExecutionProofRequest$1, RpcLightClientExecutionProofResponse as RpcLightClientExecutionProofResponse$1, RpcMaintenanceWindowsRequest as RpcMaintenanceWindowsRequest$1, EXPERIMENTALMaintenanceWindowsResponse as EXPERIMENTALMaintenanceWindowsResponse$1, RpcProtocolConfigRequest as RpcProtocolConfigRequest$1, RpcProtocolConfigResponse as RpcProtocolConfigResponse$1, RpcReceiptRequest as RpcReceiptRequest$1, RpcReceiptResponse as RpcReceiptResponse$1, RpcSplitStorageInfoRequest as RpcSplitStorageInfoRequest$1, RpcSplitStorageInfoResponse as RpcSplitStorageInfoResponse$1, RpcTransactionStatusRequest as RpcTransactionStatusRequest$1, RpcTransactionResponse as RpcTransactionResponse$1, RpcValidatorsOrderedRequest as RpcValidatorsOrderedRequest$1, EXPERIMENTALValidatorsOrderedResponse as EXPERIMENTALValidatorsOrderedResponse$1, RpcBlockRequest as RpcBlockRequest$1, RpcBlockResponse as RpcBlockResponse$1, RpcSendTransactionRequest as RpcSendTransactionRequest$1, CryptoHash as CryptoHash$1, RpcChunkRequest as RpcChunkRequest$1, RpcChunkResponse as RpcChunkResponse$1, RpcClientConfigRequest as RpcClientConfigRequest$1, RpcClientConfigResponse as RpcClientConfigResponse$1, RpcGasPriceRequest as RpcGasPriceRequest$1, RpcGasPriceResponse as RpcGasPriceResponse$1, RpcHealthRequest as RpcHealthRequest$1, RpcHealthResponse as RpcHealthResponse$1, RpcNetworkInfoRequest as RpcNetworkInfoRequest$1, RpcNetworkInfoResponse as RpcNetworkInfoResponse$1, RpcLightClientNextBlockRequest as RpcLightClientNextBlockRequest$1, RpcLightClientNextBlockResponse as RpcLightClientNextBlockResponse$1, RpcQueryRequest as RpcQueryRequest$1, RpcQueryResponse as RpcQueryResponse$1, RpcStatusRequest as RpcStatusRequest$1, RpcStatusResponse as RpcStatusResponse$1, RpcValidatorRequest as RpcValidatorRequest$1, RpcValidatorResponse as RpcValidatorResponse$1, AccountView as AccountView$1, CallResult as CallResult$1, AccessKeyView as AccessKeyView$1 } from '@near-js/jsonrpc-types/mini';
3
+ export { JsonRpcRequestSchema, JsonRpcResponseSchema, RPC_METHODS } from '@near-js/jsonrpc-types/mini';
4
+
5
+ interface ValidationResult {
6
+ validateRequest: (request: JsonRpcRequest) => void;
7
+ validateResponse: (response: JsonRpcResponse) => void;
8
+ }
9
+ /**
10
+ * Enable validation for the mini client
11
+ * This function should only be called if you want to include schema validation
12
+ * Calling this function will include Zod schemas in your bundle
13
+ */
14
+ declare function enableValidation(): ValidationResult;
15
+
16
+ interface MiniClientConfig {
17
+ endpoint: string;
18
+ headers?: Record<string, string>;
19
+ timeout?: number;
20
+ retries?: number;
21
+ validation?: ValidationResult;
22
+ }
23
+ interface JsonRpcRequest<T = unknown> {
24
+ jsonrpc: '2.0';
25
+ id: string;
26
+ method: string;
27
+ params?: T;
28
+ }
29
+ interface JsonRpcResponse<T = unknown> {
30
+ jsonrpc: '2.0';
31
+ id: string;
32
+ result?: T;
33
+ error?: JsonRpcError;
34
+ }
35
+ interface JsonRpcError {
36
+ code: number;
37
+ message: string;
38
+ data?: unknown;
39
+ }
40
+ declare class JsonRpcClientError extends Error {
41
+ code?: number | undefined;
42
+ data?: unknown | undefined;
43
+ constructor(message: string, code?: number | undefined, data?: unknown | undefined);
44
+ }
45
+ declare class JsonRpcNetworkError extends Error {
46
+ cause?: Error | undefined;
47
+ constructor(message: string, cause?: Error | undefined);
48
+ }
49
+ /**
50
+ * Simplified NEAR RPC Client for mini version
51
+ * This client only holds configuration and provides a makeRequest method
52
+ * Individual RPC methods are provided as standalone functions that take this client as a parameter
53
+ */
54
+ declare class NearRpcClient {
55
+ readonly endpoint: string;
56
+ readonly headers: Record<string, string>;
57
+ readonly timeout: number;
58
+ readonly retries: number;
59
+ private readonly validation?;
60
+ constructor(config: MiniClientConfig);
61
+ /**
62
+ * Make a raw JSON-RPC request
63
+ * This is used internally by the standalone RPC functions
64
+ */
65
+ makeRequest<TParams = unknown, TResult = unknown>(method: string, params?: TParams): Promise<TResult>;
66
+ /**
67
+ * Create a new client with modified configuration
68
+ */
69
+ withConfig(config: Partial<MiniClientConfig>): NearRpcClient;
70
+ }
71
+ declare const defaultClient: NearRpcClient;
72
+
73
+ interface RpcRequest {
74
+ jsonrpc: '2.0';
75
+ id: string | number;
76
+ method: string;
77
+ params: unknown;
78
+ }
79
+ interface RpcResponse<T = unknown> {
80
+ jsonrpc: '2.0';
81
+ id: string | number;
82
+ result?: T;
83
+ error?: RpcError;
84
+ }
85
+ interface RpcError {
86
+ code: number;
87
+ message: string;
88
+ data?: unknown;
89
+ }
90
+ declare class NearRpcError extends Error {
91
+ code: number;
92
+ data?: unknown | undefined;
93
+ constructor(code: number, message: string, data?: unknown | undefined);
94
+ }
95
+
96
+ interface DynamicRpcMethods {
97
+ experimentalChanges(params?: RpcStateChangesInBlockByTypeRequest): Promise<RpcStateChangesInBlockResponse>;
98
+ experimentalChangesInBlock(params?: RpcStateChangesInBlockRequest): Promise<RpcStateChangesInBlockByTypeResponse>;
99
+ experimentalCongestionLevel(params?: RpcCongestionLevelRequest): Promise<RpcCongestionLevelResponse>;
100
+ experimentalGenesisConfig(params?: GenesisConfigRequest): Promise<GenesisConfig>;
101
+ experimentalLightClientBlockProof(params?: RpcLightClientBlockProofRequest): Promise<RpcLightClientBlockProofResponse>;
102
+ experimentalLightClientProof(params?: RpcLightClientExecutionProofRequest): Promise<RpcLightClientExecutionProofResponse>;
103
+ experimentalMaintenanceWindows(params?: RpcMaintenanceWindowsRequest): Promise<EXPERIMENTALMaintenanceWindowsResponse>;
104
+ experimentalProtocolConfig(params?: RpcProtocolConfigRequest): Promise<RpcProtocolConfigResponse>;
105
+ experimentalReceipt(params?: RpcReceiptRequest): Promise<RpcReceiptResponse>;
106
+ experimentalSplitStorageInfo(params?: RpcSplitStorageInfoRequest): Promise<RpcSplitStorageInfoResponse>;
107
+ experimentalTxStatus(params?: RpcTransactionStatusRequest): Promise<RpcTransactionResponse>;
108
+ experimentalValidatorsOrdered(params?: RpcValidatorsOrderedRequest): Promise<EXPERIMENTALValidatorsOrderedResponse>;
109
+ block(params?: RpcBlockRequest): Promise<RpcBlockResponse>;
110
+ broadcastTxAsync(params?: RpcSendTransactionRequest): Promise<CryptoHash>;
111
+ broadcastTxCommit(params?: RpcSendTransactionRequest): Promise<RpcTransactionResponse>;
112
+ changes(params?: RpcStateChangesInBlockByTypeRequest): Promise<RpcStateChangesInBlockResponse>;
113
+ chunk(params?: RpcChunkRequest): Promise<RpcChunkResponse>;
114
+ clientConfig(params?: RpcClientConfigRequest): Promise<RpcClientConfigResponse>;
115
+ gasPrice(params?: RpcGasPriceRequest): Promise<RpcGasPriceResponse>;
116
+ health(params?: RpcHealthRequest): Promise<RpcHealthResponse>;
117
+ lightClientProof(params?: RpcLightClientExecutionProofRequest): Promise<RpcLightClientExecutionProofResponse>;
118
+ networkInfo(params?: RpcNetworkInfoRequest): Promise<RpcNetworkInfoResponse>;
119
+ nextLightClientBlock(params?: RpcLightClientNextBlockRequest): Promise<RpcLightClientNextBlockResponse>;
120
+ query(params?: RpcQueryRequest): Promise<RpcQueryResponse>;
121
+ sendTx(params?: RpcSendTransactionRequest): Promise<RpcTransactionResponse>;
122
+ status(params?: RpcStatusRequest): Promise<RpcStatusResponse>;
123
+ tx(params?: RpcTransactionStatusRequest): Promise<RpcTransactionResponse>;
124
+ validators(params?: RpcValidatorRequest): Promise<RpcValidatorResponse>;
125
+ }
126
+ interface ConvenienceMethods {
127
+ viewAccount(params: {
128
+ accountId: string;
129
+ finality?: 'final' | 'near-final' | 'optimistic';
130
+ blockId?: string | number;
131
+ }): Promise<AccountView>;
132
+ viewFunction(params: {
133
+ accountId: string;
134
+ methodName: string;
135
+ argsBase64?: string;
136
+ finality?: 'final' | 'near-final' | 'optimistic';
137
+ blockId?: string | number;
138
+ }): Promise<CallResult>;
139
+ viewAccessKey(params: {
140
+ accountId: string;
141
+ publicKey: string;
142
+ finality?: 'final' | 'near-final' | 'optimistic';
143
+ blockId?: string | number;
144
+ }): Promise<AccessKeyView>;
145
+ }
146
+ interface CompleteClientInterface extends DynamicRpcMethods, ConvenienceMethods {
147
+ call<TParams = unknown, TResult = unknown>(method: string, params?: TParams): Promise<TResult>;
148
+ }
149
+
150
+ declare function experimentalChanges(client: NearRpcClient, params?: RpcStateChangesInBlockByTypeRequest$1): Promise<RpcStateChangesInBlockResponse$1>;
151
+ declare function experimentalChangesInBlock(client: NearRpcClient, params?: RpcStateChangesInBlockRequest$1): Promise<RpcStateChangesInBlockByTypeResponse$1>;
152
+ declare function experimentalCongestionLevel(client: NearRpcClient, params?: RpcCongestionLevelRequest$1): Promise<RpcCongestionLevelResponse$1>;
153
+ declare function experimentalGenesisConfig(client: NearRpcClient, params?: GenesisConfigRequest$1): Promise<GenesisConfig$1>;
154
+ declare function experimentalLightClientBlockProof(client: NearRpcClient, params?: RpcLightClientBlockProofRequest$1): Promise<RpcLightClientBlockProofResponse$1>;
155
+ declare function experimentalLightClientProof(client: NearRpcClient, params?: RpcLightClientExecutionProofRequest$1): Promise<RpcLightClientExecutionProofResponse$1>;
156
+ declare function experimentalMaintenanceWindows(client: NearRpcClient, params?: RpcMaintenanceWindowsRequest$1): Promise<EXPERIMENTALMaintenanceWindowsResponse$1>;
157
+ declare function experimentalProtocolConfig(client: NearRpcClient, params?: RpcProtocolConfigRequest$1): Promise<RpcProtocolConfigResponse$1>;
158
+ declare function experimentalReceipt(client: NearRpcClient, params?: RpcReceiptRequest$1): Promise<RpcReceiptResponse$1>;
159
+ declare function experimentalSplitStorageInfo(client: NearRpcClient, params?: RpcSplitStorageInfoRequest$1): Promise<RpcSplitStorageInfoResponse$1>;
160
+ declare function experimentalTxStatus(client: NearRpcClient, params?: RpcTransactionStatusRequest$1): Promise<RpcTransactionResponse$1>;
161
+ declare function experimentalValidatorsOrdered(client: NearRpcClient, params?: RpcValidatorsOrderedRequest$1): Promise<EXPERIMENTALValidatorsOrderedResponse$1>;
162
+ declare function block(client: NearRpcClient, params?: RpcBlockRequest$1): Promise<RpcBlockResponse$1>;
163
+ declare function broadcastTxAsync(client: NearRpcClient, params?: RpcSendTransactionRequest$1): Promise<CryptoHash$1>;
164
+ declare function broadcastTxCommit(client: NearRpcClient, params?: RpcSendTransactionRequest$1): Promise<RpcTransactionResponse$1>;
165
+ declare function changes(client: NearRpcClient, params?: RpcStateChangesInBlockByTypeRequest$1): Promise<RpcStateChangesInBlockResponse$1>;
166
+ declare function chunk(client: NearRpcClient, params?: RpcChunkRequest$1): Promise<RpcChunkResponse$1>;
167
+ declare function clientConfig(client: NearRpcClient, params?: RpcClientConfigRequest$1): Promise<RpcClientConfigResponse$1>;
168
+ declare function gasPrice(client: NearRpcClient, params?: RpcGasPriceRequest$1): Promise<RpcGasPriceResponse$1>;
169
+ declare function health(client: NearRpcClient, params?: RpcHealthRequest$1): Promise<RpcHealthResponse$1>;
170
+ declare function lightClientProof(client: NearRpcClient, params?: RpcLightClientExecutionProofRequest$1): Promise<RpcLightClientExecutionProofResponse$1>;
171
+ declare function networkInfo(client: NearRpcClient, params?: RpcNetworkInfoRequest$1): Promise<RpcNetworkInfoResponse$1>;
172
+ declare function nextLightClientBlock(client: NearRpcClient, params?: RpcLightClientNextBlockRequest$1): Promise<RpcLightClientNextBlockResponse$1>;
173
+ declare function query(client: NearRpcClient, params?: RpcQueryRequest$1): Promise<RpcQueryResponse$1>;
174
+ declare function sendTx(client: NearRpcClient, params?: RpcSendTransactionRequest$1): Promise<RpcTransactionResponse$1>;
175
+ declare function status(client: NearRpcClient, params?: RpcStatusRequest$1): Promise<RpcStatusResponse$1>;
176
+ declare function tx(client: NearRpcClient, params?: RpcTransactionStatusRequest$1): Promise<RpcTransactionResponse$1>;
177
+ declare function validators(client: NearRpcClient, params?: RpcValidatorRequest$1): Promise<RpcValidatorResponse$1>;
178
+
179
+ declare function viewAccount(client: NearRpcClient, params: {
180
+ accountId: string;
181
+ finality?: 'final' | 'near-final' | 'optimistic';
182
+ blockId?: string | number;
183
+ }): Promise<AccountView$1>;
184
+ declare function viewFunction(client: NearRpcClient, params: {
185
+ accountId: string;
186
+ methodName: string;
187
+ argsBase64?: string;
188
+ finality?: 'final' | 'near-final' | 'optimistic';
189
+ blockId?: string | number;
190
+ }): Promise<CallResult$1>;
191
+ declare function viewAccessKey(client: NearRpcClient, params: {
192
+ accountId: string;
193
+ publicKey: string;
194
+ finality?: 'final' | 'near-final' | 'optimistic';
195
+ blockId?: string | number;
196
+ }): Promise<AccessKeyView$1>;
197
+
198
+ export { type CompleteClientInterface, type ConvenienceMethods, type DynamicRpcMethods, JsonRpcClientError, type JsonRpcError, JsonRpcNetworkError, type JsonRpcRequest, type JsonRpcResponse, type MiniClientConfig, NearRpcClient, NearRpcError, type RpcError, type RpcRequest, type RpcResponse, block, broadcastTxAsync, broadcastTxCommit, changes, chunk, clientConfig, NearRpcClient as default, defaultClient, enableValidation, experimentalChanges, experimentalChangesInBlock, experimentalCongestionLevel, experimentalGenesisConfig, experimentalLightClientBlockProof, experimentalLightClientProof, experimentalMaintenanceWindows, experimentalProtocolConfig, experimentalReceipt, experimentalSplitStorageInfo, experimentalTxStatus, experimentalValidatorsOrdered, gasPrice, health, lightClientProof, networkInfo, nextLightClientBlock, query, sendTx, status, tx, validators, viewAccessKey, viewAccount, viewFunction };
@@ -0,0 +1,10 @@
1
+ export * from './client.mini';
2
+ export * from './types';
3
+ export { NearRpcClient, defaultClient } from './client.mini';
4
+ export type { DynamicRpcMethods, ConvenienceMethods, CompleteClientInterface, } from './generated-types';
5
+ export { JsonRpcRequestSchema, JsonRpcResponseSchema, } from '@near-js/jsonrpc-types/mini';
6
+ export { RPC_METHODS } from '@near-js/jsonrpc-types/mini';
7
+ export * from './generated-functions.mini';
8
+ export { enableValidation } from './validation.mini.js';
9
+ export { NearRpcClient as default } from './client.mini';
10
+ //# sourceMappingURL=index.mini.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mini.d.ts","sourceRoot":"","sources":["../src/index.mini.ts"],"names":[],"mappings":"AAGA,cAAc,eAAe,CAAC;AAC9B,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAG7D,YAAY,EACV,iBAAiB,EACjB,kBAAkB,EAClB,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAG1D,cAAc,4BAA4B,CAAC;AAG3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGxD,OAAO,EAAE,aAAa,IAAI,OAAO,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,361 @@
1
+ // src/client.mini.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, cause) {
47
+ super(message);
48
+ this.cause = cause;
49
+ this.name = "JsonRpcNetworkError";
50
+ }
51
+ };
52
+ var NearRpcClient = class _NearRpcClient {
53
+ endpoint;
54
+ headers;
55
+ timeout;
56
+ retries;
57
+ validation;
58
+ constructor(config) {
59
+ this.endpoint = config.endpoint;
60
+ this.headers = config.headers || {};
61
+ this.timeout = config.timeout || 3e4;
62
+ this.retries = config.retries || 3;
63
+ if (config.validation) {
64
+ this.validation = config.validation;
65
+ }
66
+ }
67
+ /**
68
+ * Make a raw JSON-RPC request
69
+ * This is used internally by the standalone RPC functions
70
+ */
71
+ async makeRequest(method, params) {
72
+ const snakeCaseParams = params ? convertKeysToSnakeCase(params) : params;
73
+ const request = {
74
+ jsonrpc: "2.0",
75
+ id: REQUEST_ID,
76
+ method,
77
+ params: snakeCaseParams
78
+ };
79
+ if (this.validation) {
80
+ this.validation.validateRequest(request);
81
+ }
82
+ let lastError = null;
83
+ for (let attempt = 0; attempt <= this.retries; attempt++) {
84
+ try {
85
+ const controller = new AbortController();
86
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
87
+ const response = await fetch(this.endpoint, {
88
+ method: "POST",
89
+ headers: {
90
+ "Content-Type": "application/json",
91
+ ...this.headers
92
+ },
93
+ body: JSON.stringify(request),
94
+ signal: controller.signal
95
+ });
96
+ clearTimeout(timeoutId);
97
+ if (!response.ok) {
98
+ throw new JsonRpcNetworkError(
99
+ `HTTP error! status: ${response.status}`
100
+ );
101
+ }
102
+ const jsonResponse = await response.json();
103
+ if (this.validation) {
104
+ this.validation.validateResponse(jsonResponse);
105
+ }
106
+ if (jsonResponse.error) {
107
+ throw new JsonRpcClientError(
108
+ jsonResponse.error.message,
109
+ jsonResponse.error.code,
110
+ jsonResponse.error.data
111
+ );
112
+ }
113
+ const camelCaseResult = jsonResponse.result ? convertKeysToCamelCase(jsonResponse.result) : jsonResponse.result;
114
+ return camelCaseResult;
115
+ } catch (error) {
116
+ lastError = error;
117
+ if (error instanceof JsonRpcClientError) {
118
+ throw error;
119
+ }
120
+ if (attempt === this.retries) {
121
+ break;
122
+ }
123
+ await new Promise(
124
+ (resolve) => setTimeout(resolve, Math.pow(2, attempt) * 1e3)
125
+ );
126
+ }
127
+ }
128
+ throw lastError || new JsonRpcNetworkError("Request failed after all retries");
129
+ }
130
+ /**
131
+ * Create a new client with modified configuration
132
+ */
133
+ withConfig(config) {
134
+ return new _NearRpcClient({
135
+ endpoint: config.endpoint ?? this.endpoint,
136
+ headers: config.headers ?? this.headers,
137
+ timeout: config.timeout ?? this.timeout,
138
+ retries: config.retries ?? this.retries,
139
+ ...config.validation !== void 0 ? { validation: config.validation } : this.validation !== void 0 ? { validation: this.validation } : {}
140
+ });
141
+ }
142
+ };
143
+ var defaultClient = new NearRpcClient({
144
+ endpoint: "https://rpc.mainnet.near.org"
145
+ });
146
+
147
+ // src/types.ts
148
+ var NearRpcError = class extends Error {
149
+ constructor(code, message, data) {
150
+ super(message);
151
+ this.code = code;
152
+ this.data = data;
153
+ this.name = "NearRpcError";
154
+ }
155
+ };
156
+
157
+ // src/index.mini.ts
158
+ import {
159
+ JsonRpcRequestSchema as JsonRpcRequestSchema2,
160
+ JsonRpcResponseSchema as JsonRpcResponseSchema2
161
+ } from "@psalomo/jsonrpc-types/mini";
162
+ import { RPC_METHODS } from "@psalomo/jsonrpc-types/mini";
163
+
164
+ // src/generated-types.mini.ts
165
+ async function experimentalChanges(client, params) {
166
+ return client.makeRequest("EXPERIMENTAL_changes", params);
167
+ }
168
+ async function experimentalChangesInBlock(client, params) {
169
+ return client.makeRequest("EXPERIMENTAL_changes_in_block", params);
170
+ }
171
+ async function experimentalCongestionLevel(client, params) {
172
+ return client.makeRequest("EXPERIMENTAL_congestion_level", params);
173
+ }
174
+ async function experimentalGenesisConfig(client, params) {
175
+ return client.makeRequest("EXPERIMENTAL_genesis_config", params);
176
+ }
177
+ async function experimentalLightClientBlockProof(client, params) {
178
+ return client.makeRequest("EXPERIMENTAL_light_client_block_proof", params);
179
+ }
180
+ async function experimentalLightClientProof(client, params) {
181
+ return client.makeRequest("EXPERIMENTAL_light_client_proof", params);
182
+ }
183
+ async function experimentalMaintenanceWindows(client, params) {
184
+ return client.makeRequest("EXPERIMENTAL_maintenance_windows", params);
185
+ }
186
+ async function experimentalProtocolConfig(client, params) {
187
+ return client.makeRequest("EXPERIMENTAL_protocol_config", params);
188
+ }
189
+ async function experimentalReceipt(client, params) {
190
+ return client.makeRequest("EXPERIMENTAL_receipt", params);
191
+ }
192
+ async function experimentalSplitStorageInfo(client, params) {
193
+ return client.makeRequest("EXPERIMENTAL_split_storage_info", params);
194
+ }
195
+ async function experimentalTxStatus(client, params) {
196
+ return client.makeRequest("EXPERIMENTAL_tx_status", params);
197
+ }
198
+ async function experimentalValidatorsOrdered(client, params) {
199
+ return client.makeRequest("EXPERIMENTAL_validators_ordered", params);
200
+ }
201
+ async function block(client, params) {
202
+ return client.makeRequest("block", params);
203
+ }
204
+ async function broadcastTxAsync(client, params) {
205
+ return client.makeRequest("broadcast_tx_async", params);
206
+ }
207
+ async function broadcastTxCommit(client, params) {
208
+ return client.makeRequest("broadcast_tx_commit", params);
209
+ }
210
+ async function changes(client, params) {
211
+ return client.makeRequest("changes", params);
212
+ }
213
+ async function chunk(client, params) {
214
+ return client.makeRequest("chunk", params);
215
+ }
216
+ async function clientConfig(client, params) {
217
+ return client.makeRequest("client_config", params);
218
+ }
219
+ async function gasPrice(client, params) {
220
+ return client.makeRequest("gas_price", params);
221
+ }
222
+ async function health(client, params) {
223
+ return client.makeRequest("health", params);
224
+ }
225
+ async function lightClientProof(client, params) {
226
+ return client.makeRequest("light_client_proof", params);
227
+ }
228
+ async function networkInfo(client, params) {
229
+ return client.makeRequest("network_info", params);
230
+ }
231
+ async function nextLightClientBlock(client, params) {
232
+ return client.makeRequest("next_light_client_block", params);
233
+ }
234
+ async function query(client, params) {
235
+ return client.makeRequest("query", params);
236
+ }
237
+ async function sendTx(client, params) {
238
+ return client.makeRequest("send_tx", params);
239
+ }
240
+ async function status(client, params) {
241
+ return client.makeRequest("status", params);
242
+ }
243
+ async function tx(client, params) {
244
+ return client.makeRequest("tx", params);
245
+ }
246
+ async function validators(client, params) {
247
+ return client.makeRequest("validators", params);
248
+ }
249
+
250
+ // src/convenience.mini.ts
251
+ async function viewAccount(client, params) {
252
+ const queryParams = params.blockId ? {
253
+ requestType: "view_account",
254
+ accountId: params.accountId,
255
+ blockId: params.blockId
256
+ } : {
257
+ requestType: "view_account",
258
+ accountId: params.accountId,
259
+ finality: params.finality || "final"
260
+ };
261
+ return query(client, queryParams);
262
+ }
263
+ async function viewFunction(client, params) {
264
+ const baseParams = {
265
+ requestType: "call_function",
266
+ accountId: params.accountId,
267
+ methodName: params.methodName,
268
+ argsBase64: params.argsBase64 ?? ""
269
+ // Default to empty string if no arguments
270
+ };
271
+ const queryParams = params.blockId ? { ...baseParams, blockId: params.blockId } : { ...baseParams, finality: params.finality || "final" };
272
+ return query(client, queryParams);
273
+ }
274
+ async function viewAccessKey(client, params) {
275
+ const queryParams = params.blockId ? {
276
+ requestType: "view_access_key",
277
+ accountId: params.accountId,
278
+ publicKey: params.publicKey,
279
+ blockId: params.blockId
280
+ } : {
281
+ requestType: "view_access_key",
282
+ accountId: params.accountId,
283
+ publicKey: params.publicKey,
284
+ finality: params.finality || "final"
285
+ };
286
+ return query(client, queryParams);
287
+ }
288
+
289
+ // src/validation.mini.ts
290
+ import {
291
+ JsonRpcRequestSchema,
292
+ JsonRpcResponseSchema
293
+ } from "@psalomo/jsonrpc-types/mini";
294
+ function enableValidation() {
295
+ const requestSchema = JsonRpcRequestSchema();
296
+ const responseSchema = JsonRpcResponseSchema();
297
+ return {
298
+ validateRequest: (request) => {
299
+ try {
300
+ requestSchema.parse(request);
301
+ } catch (error) {
302
+ throw new JsonRpcNetworkError(
303
+ `Invalid request format: ${error instanceof Error ? error.message : "Unknown error"}`,
304
+ error
305
+ );
306
+ }
307
+ },
308
+ validateResponse: (response) => {
309
+ try {
310
+ responseSchema.parse(response);
311
+ } catch (error) {
312
+ throw new JsonRpcClientError(
313
+ `Invalid response format: ${error instanceof Error ? error.message : "Unknown error"}`
314
+ );
315
+ }
316
+ }
317
+ };
318
+ }
319
+ export {
320
+ JsonRpcClientError,
321
+ JsonRpcNetworkError,
322
+ JsonRpcRequestSchema2 as JsonRpcRequestSchema,
323
+ JsonRpcResponseSchema2 as JsonRpcResponseSchema,
324
+ NearRpcClient,
325
+ NearRpcError,
326
+ RPC_METHODS,
327
+ block,
328
+ broadcastTxAsync,
329
+ broadcastTxCommit,
330
+ changes,
331
+ chunk,
332
+ clientConfig,
333
+ NearRpcClient as default,
334
+ defaultClient,
335
+ enableValidation,
336
+ experimentalChanges,
337
+ experimentalChangesInBlock,
338
+ experimentalCongestionLevel,
339
+ experimentalGenesisConfig,
340
+ experimentalLightClientBlockProof,
341
+ experimentalLightClientProof,
342
+ experimentalMaintenanceWindows,
343
+ experimentalProtocolConfig,
344
+ experimentalReceipt,
345
+ experimentalSplitStorageInfo,
346
+ experimentalTxStatus,
347
+ experimentalValidatorsOrdered,
348
+ gasPrice,
349
+ health,
350
+ lightClientProof,
351
+ networkInfo,
352
+ nextLightClientBlock,
353
+ query,
354
+ sendTx,
355
+ status,
356
+ tx,
357
+ validators,
358
+ viewAccessKey,
359
+ viewAccount,
360
+ viewFunction
361
+ };
package/dist/index.mjs CHANGED
@@ -59,7 +59,6 @@ var NearRpcClient = class {
59
59
  timeout;
60
60
  retries;
61
61
  validateResponses;
62
- requestIdCounter = 0;
63
62
  constructor(config) {
64
63
  if (typeof config === "string") {
65
64
  this.endpoint = config;
@@ -81,17 +80,17 @@ var NearRpcClient = class {
81
80
  }
82
81
  }
83
82
  /**
84
- * Generate a unique request ID
83
+ * Get request ID matching NEAR RPC documentation examples
85
84
  */
86
- generateRequestId() {
87
- return `near-rpc-${Date.now()}-${++this.requestIdCounter}`;
85
+ getRequestId() {
86
+ return "dontcare";
88
87
  }
89
88
  /**
90
89
  * Make a raw JSON-RPC call
91
90
  * This method is public to allow dynamic calls to any RPC method
92
91
  */
93
92
  async call(method, params) {
94
- const requestId = this.generateRequestId();
93
+ const requestId = this.getRequestId();
95
94
  const snakeCaseParams = params ? convertKeysToSnakeCase(params) : void 0;
96
95
  const request = {
97
96
  jsonrpc: "2.0",
@@ -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"}