Skip to main content

Crate jsonrpsee_ts

Crate jsonrpsee_ts 

Source
Expand description

Generate an rpckit schema from a jsonrpsee RPC trait.

Add export_schema next to #[rpc(...)] and the macro generates a <Trait>Schema type that:

  • builds an in-memory schema with schema(&Config)
  • implements ts_rs::TS
  • can be exported with export, export_all, or export_to_string

The generated schema reuses the original RPC trait metadata: namespace, method(name), subscription(name, item), param_kind, and #[argument(rename = "...")].

§Example

use jsonrpsee::core::SubscriptionResult;
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee_ts::export_schema;
use ts_rs::TS;

#[derive(TS)]
#[ts(export)]
struct Hash {
    value: String,
}

#[derive(TS)]
#[ts(export)]
struct StorageKey {
    bytes: String,
}

#[export_schema]
#[rpc(server, client, namespace = "state")]
trait StateRpc<HashTy, StorageKeyTy> {
    #[method(name = "getKeys")]
    async fn storage_keys(
        &self,
        storage_key: StorageKeyTy,
        hash: Option<HashTy>,
    ) -> Result<Vec<StorageKeyTy>, ErrorObjectOwned>;

    #[subscription(name = "subscribeStorage", item = Vec<HashTy>)]
    async fn subscribe_storage(
        &self,
        keys: Option<Vec<StorageKeyTy>>,
    ) -> SubscriptionResult;
}

let cfg = ts_rs::Config::default();
let schema = StateRpcSchema::<Hash, StorageKey>::schema(&cfg);
println!("{}", schema.render_type_alias("StateRpcSchema"));

Option<T> parameters become optional TypeScript parameters. Result<T, E> and RpcResult<T> use the success type as the generated return type. Referenced ts-rs types are imported and exported automatically when using export_all.

Macros§

ts_ident

Structs§

Method
A single rpckit schema entry.
Param
A single rpckit parameter entry.
Schema
In-memory rpckit schema builder used by the generated macro output.

Enums§

ParamKind
Parameter encoding mode mirroring jsonrpsee’s param_kind.

Functions§

type_name
Return the TypeScript name for a ts-rs type.
void_type
Return the TypeScript void type used for methods without a return value.

Attribute Macros§

export_schema
Generate a <Trait>Schema type for a jsonrpsee RPC trait.