Kit
Functions

createRpcSubscriptionsApi

createRpcSubscriptionsApi<TRpcSubscriptionsApiMethods>(config): RpcSubscriptionsApi<TRpcSubscriptionsApiMethods>

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

  • calls the supplied RpcSubscriptionsApiConfig.planExecutor with a JSON RPC v2 payload object with the requested methodName and params properties, optionally transformed by RpcSubscriptionsApiConfig.requestTransformer.

Type Parameters

Type Parameter
TRpcSubscriptionsApiMethods extends RpcSubscriptionsApiMethods

Parameters

ParameterType
configRpcSubscriptionsApiConfig<TRpcSubscriptionsApiMethods>

Returns

RpcSubscriptionsApi<TRpcSubscriptionsApiMethods>

Example

// For example, given this `RpcSubscriptionsApi`:
const rpcSubscriptionsApi = createJsonRpcSubscriptionsApi({
    async planExecutor({ channel, request }) {
        await channel.send(request);
        return {
            ...channel,
            on(type, listener, options) {
                if (type !== 'message') {
                    return channel.on(type, listener, options);
                }
                return channel.on(
                    'message',
                    function resultGettingListener(message) {
                        listener(message.result);
                    },
                    options,
                );
            }
        }
    },
    requestTransformer: (...rawParams) => rawParams.reverse(),
});
 
// ...the following function call:
rpcSubscriptionsApi.foo('bar', { baz: 'bat' });
 
// ...will produce a `RpcSubscriptionsPlan` that:
// -   Uses the following payload: { id: 1, jsonrpc: '2.0', method: 'foo', params: [{ baz: 'bat' }, 'bar'] }.
// -   Emits the "result" property of each RPC Subscriptions message.

On this page