Kit
Functions

createJsonRpcApi

createJsonRpcApi<TRpcMethods>(config?): RpcApi<TRpcMethods>

Creates a JavaScript proxy that converts any function call called on it to a RpcPlan by creating an execute function that:

  • sets the transport payload to a JSON RPC v2 payload object with the requested methodName and params properties, optionally transformed by RpcApiConfig.requestTransformer.
  • transforms the transport's response using the RpcApiConfig.responseTransformer function, if provided.

Type Parameters

Type Parameter
TRpcMethods extends RpcApiMethods

Parameters

ParameterType
config?Readonly<{ requestTransformer?: RpcRequestTransformer; responseTransformer?: RpcResponseTransformer; }>

Returns

RpcApi<TRpcMethods>

Example

// For example, given this `RpcApi`:
const rpcApi = createJsonRpcApi({
    requestTransformer: (...rawParams) => rawParams.reverse(),
    responseTransformer: response => response.result,
});
 
// ...the following function call:
rpcApi.foo('bar', { baz: 'bat' });
 
// ...will produce a `RpcPlan` that:
// -   Uses the following payload: { id: 1, jsonrpc: '2.0', method: 'foo', params: [{ baz: 'bat' }, 'bar'] }.
// -   Returns the "result" property of the RPC response.

On this page