jsonrpsee-ts
Generate an rpckit schema from a jsonrpsee RPC trait.
This crate is meant for the common case where your Rust trait is already the source of truth. Add #[export_schema] next to #[rpc(...)], and the same trait can drive:
jsonrpseeserver/client code- an rpckit-compatible TypeScript schema
ts-rsexport of the schema and any referenced types
Quick start
use SubscriptionResult;
use rpc;
use ErrorObjectOwned;
use export_schema;
use TS;
That generates StateRpcSchema<HashTy, StorageKeyTy>.
Exporting
let cfg = default;
let schema = schema;
let ts = export_to_string?;
export_all?;
The generated type implements ts_rs::TS, so it works with the usual ts-rs export flow.
What gets generated
For the trait above, the schema looks like this:
export type StateRpcSchema = {
requests: [
{
method: 'state_getKeys'
params: [storage_key: StorageKey, hash?: Hash]
return: Array<StorageKey>
}
]
subscriptions: [
{
method: 'state_subscribeStorage'
params: [keys?: Array<StorageKey>]
return: Array<Hash>
}
]
}
When exported, the schema file also includes import type statements for referenced ts-rs types.
Mapping rules
rpc(namespace = "...")prefixes the generated RPC method name#[method(name = "...")]becomes a request entry#[subscription(name = "...", item = T)]becomes a subscription entryOption<T>parameters become optional TypeScript parametersResult<T, E>andRpcResult<T>useTas thereturntypeparam_kind = maprenders an object parameter shape#[argument(rename = "...")]renames object keys
Current limitations
- lifetime generics on the RPC trait are not supported
- the schema currently targets rpckit's
SchemaEntryshape:method,params, andreturn - subscription notification overrides like
name = "subscribe" => "override"are parsed, but rpckit's current schema format has no separate field for them