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, orexport_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§
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§
- Param
Kind - Parameter encoding mode mirroring
jsonrpsee’sparam_kind.
Functions§
- type_
name - Return the TypeScript name for a
ts-rstype. - void_
type - Return the TypeScript
voidtype used for methods without a return value.
Attribute Macros§
- export_
schema - Generate a
<Trait>Schematype for ajsonrpseeRPC trait.