jsonrpsee-ts 0.1.1

Generate rpckit-compatible TypeScript schemas from jsonrpsee RPC traits
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Generate an [`rpckit`](https://rpckit.dev) 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
//!
//! ```ignore
//! 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`.
extern crate self as jsonrpsee_ts;

use std::fmt::{Display, Formatter};

/// Generate a `<Trait>Schema` type for a `jsonrpsee` RPC trait.
///
/// See the crate-level documentation for a complete example.
pub use jsonrpsee_ts_macros::export_schema;

/// Parameter encoding mode mirroring `jsonrpsee`'s `param_kind`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParamKind {
    Array,
    Map,
}

#[macro_export]
macro_rules! ts_ident {
    ($rs_ident:ty) => {
        <$rs_ident as ::ts_rs::TS>::ident(&Default::default())
    };
}

/// Return the TypeScript name for a `ts-rs` type.
pub fn type_name<T: ::ts_rs::TS>(cfg: &::ts_rs::Config) -> String {
    T::name(cfg)
}

/// Return the TypeScript `void` type used for methods without a return value.
pub fn void_type() -> String {
    "void".to_string()
}

/// A single rpckit parameter entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Param {
    ident: String,
    optional: bool,
    ts_ident: String,
}

impl Param {
    /// Create a required parameter.
    pub fn new(ident: &str, ts_ident: &str) -> Self {
        Self {
            ident: ident.to_string(),
            optional: false,
            ts_ident: ts_ident.to_string(),
        }
    }

    /// Mark this parameter as optional.
    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }

    fn render_array(&self) -> String {
        let mut rendered = self.ident.clone();
        if self.optional {
            rendered.push('?');
        }
        rendered.push_str(": ");
        rendered.push_str(&self.ts_ident);
        rendered
    }

    fn render_map(&self) -> String {
        let mut rendered = ts_property_name(&self.ident);
        if self.optional {
            rendered.push('?');
        }
        rendered.push_str(": ");
        rendered.push_str(&self.ts_ident);
        rendered
    }
}

fn ts_property_name(name: &str) -> String {
    if is_ts_identifier(name) {
        name.to_string()
    } else {
        format!("'{name}'")
    }
}

fn is_ts_identifier(name: &str) -> bool {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return false;
    };

    if !(first == '_' || first == '$' || first.is_ascii_alphabetic()) {
        return false;
    }

    chars.all(|ch| ch == '_' || ch == '$' || ch.is_ascii_alphanumeric())
}

/// A single rpckit schema entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Method {
    name: String,
    params: Vec<Param>,
    param_kind: ParamKind,
    return_ts_ident: String,
}

impl Method {
    /// Create a new schema entry with array-style params by default.
    pub fn new(name: &str, return_ts_ident: &str) -> Self {
        Self {
            name: name.to_string(),
            params: vec![],
            param_kind: ParamKind::Array,
            return_ts_ident: return_ts_ident.to_string(),
        }
    }

    /// Set the parameter encoding mode.
    pub fn with_param_kind(mut self, param_kind: ParamKind) -> Self {
        self.param_kind = param_kind;
        self
    }

    /// Append one parameter.
    pub fn param(mut self, param: Param) -> Self {
        self.params.push(param);
        self
    }

    fn render_params(&self) -> String {
        match self.param_kind {
            ParamKind::Array => {
                let params = self
                    .params
                    .iter()
                    .map(Param::render_array)
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("[{params}]")
            }
            ParamKind::Map => {
                let params = self
                    .params
                    .iter()
                    .map(Param::render_map)
                    .collect::<Vec<_>>()
                    .join("; ");
                format!("{{ {params} }}")
            }
        }
    }
}

impl Display for Method {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ method: '{}'; params: {}; return: {} }}",
            self.name,
            self.render_params(),
            self.return_ts_ident
        )
    }
}

/// In-memory rpckit schema builder used by the generated macro output.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Schema {
    requests: Vec<Method>,
    subscriptions: Vec<Method>,
}

impl Display for Schema {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.render_inline())
    }
}

impl Schema {
    /// Create an empty schema.
    pub fn new() -> Self {
        Self {
            requests: vec![],
            subscriptions: vec![],
        }
    }

    /// Append a request entry.
    pub fn request(mut self, method: Method) -> Self {
        self.requests.push(method);
        self
    }

    /// Append a subscription entry.
    pub fn subscription(mut self, subscription: Method) -> Self {
        self.subscriptions.push(subscription);
        self
    }

    /// Merge two schemas together.
    pub fn merge(mut self, mut other: Self) -> Self {
        self.requests.append(&mut other.requests);
        self.subscriptions.append(&mut other.subscriptions);
        self
    }

    /// Render the schema body as an inline TypeScript object.
    pub fn render_inline(&self) -> String {
        let requests = self.render_entries(&self.requests, 2);
        let subscriptions = self.render_entries(&self.subscriptions, 2);

        format!("{{\n  requests: {requests};\n  subscriptions: {subscriptions};\n}}")
    }

    /// Render `type <ident> = ...`.
    pub fn render_type_alias(&self, ident: &str) -> String {
        format!("type {ident} = {};", self.render_inline())
    }

    fn render_entries(&self, entries: &[Method], indent: usize) -> String {
        if entries.is_empty() {
            return "[]".to_string();
        }

        let padding = " ".repeat(indent);
        let inner_padding = " ".repeat(indent + 2);
        let rendered_entries = entries
            .iter()
            .map(|entry| format!("{inner_padding}{entry},"))
            .collect::<Vec<_>>()
            .join("\n");

        format!("[\n{rendered_entries}\n{padding}]")
    }
}

impl Default for Schema {
    fn default() -> Self {
        Self::new()
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __jsonrpsee_ts_return_type {
    ($cfg:ident, void) => {
        ::jsonrpsee_ts::void_type()
    };
    ($cfg:ident, ty($ty:ty)) => {
        ::jsonrpsee_ts::type_name::<$ty>($cfg)
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __jsonrpsee_ts_param {
    ($cfg:ident, $name:literal, $ty:ty, required) => {
        ::jsonrpsee_ts::Param::new($name, &::jsonrpsee_ts::type_name::<$ty>($cfg))
    };
    ($cfg:ident, $name:literal, $ty:ty, optional) => {
        ::jsonrpsee_ts::Param::new($name, &::jsonrpsee_ts::type_name::<$ty>($cfg)).optional()
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __jsonrpsee_ts_method {
    (
        cfg = $cfg:ident,
        name = $name:literal,
        param_kind = $param_kind:ident,
        params = [$(($param_name:literal, $param_ty:ty, $optional:ident)),* $(,)?],
        return = $($return:tt)+
    ) => {{
        ::jsonrpsee_ts::Method::new($name, &::jsonrpsee_ts::__jsonrpsee_ts_return_type!($cfg, $($return)+))
            .with_param_kind(::jsonrpsee_ts::ParamKind::$param_kind)
            $(.param(::jsonrpsee_ts::__jsonrpsee_ts_param!($cfg, $param_name, $param_ty, $optional)))*
    }};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __jsonrpsee_ts_schema_impl {
    (
        schema = $schema_ident:ident,
        builder = $builder_fn:ident,
        builder_generics = [$($builder_generics:tt)*],
        struct_generics = [$($struct_generics:tt)*],
        marker = [$($marker:tt)*],
        impl_generics = [$($impl_generics:tt)*],
        type_generics = [$($type_generics:tt)*],
        where_clause = [$($where_clause:tt)*],
        used_types = [$($used_ty:ty),* $(,)?]
    ) => {
        #[doc(hidden)]
        pub struct $schema_ident $($struct_generics)* $($marker)* $($where_clause)*;

        impl $($impl_generics)* $schema_ident $($type_generics)* $($where_clause)* {
            pub fn schema(cfg: &::ts_rs::Config) -> ::jsonrpsee_ts::Schema {
                $builder_fn $($builder_generics)*(cfg)
            }

            pub fn export(cfg: &::ts_rs::Config) -> ::std::result::Result<(), ::ts_rs::ExportError>
            where
                Self: 'static,
            {
                <Self as ::ts_rs::TS>::export(cfg)
            }

            pub fn export_all(cfg: &::ts_rs::Config) -> ::std::result::Result<(), ::ts_rs::ExportError>
            where
                Self: 'static,
            {
                <Self as ::ts_rs::TS>::export_all(cfg)
            }

            pub fn export_to_string(
                cfg: &::ts_rs::Config,
            ) -> ::std::result::Result<::std::string::String, ::ts_rs::ExportError>
            where
                Self: 'static,
            {
                <Self as ::ts_rs::TS>::export_to_string(cfg)
            }
        }

        impl $($impl_generics)* ::ts_rs::TS for $schema_ident $($type_generics)* $($where_clause)* {
            type WithoutGenerics = Self;
            type OptionInnerType = Self;

            fn ident(_: &::ts_rs::Config) -> ::std::string::String {
                stringify!($schema_ident).to_owned()
            }

            fn name(cfg: &::ts_rs::Config) -> ::std::string::String {
                <Self as ::ts_rs::TS>::ident(cfg)
            }

            fn decl(cfg: &::ts_rs::Config) -> ::std::string::String {
                $builder_fn $($builder_generics)*(cfg).render_type_alias(&<Self as ::ts_rs::TS>::ident(cfg))
            }

            fn decl_concrete(cfg: &::ts_rs::Config) -> ::std::string::String {
                <Self as ::ts_rs::TS>::decl(cfg)
            }

            fn inline(cfg: &::ts_rs::Config) -> ::std::string::String {
                $builder_fn $($builder_generics)*(cfg).render_inline()
            }

            fn visit_dependencies(v: &mut impl ::ts_rs::TypeVisitor)
            where
                Self: 'static,
            {
                $(
                    v.visit::<$used_ty>();
                    <$used_ty as ::ts_rs::TS>::visit_generics(v);
                )*
            }

            fn output_path() -> ::std::option::Option<::std::path::PathBuf> {
                ::std::option::Option::Some(::std::path::PathBuf::from(format!(
                    "{}.ts",
                    stringify!($schema_ident),
                )))
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::{Method, Param, ParamKind, Schema};

    #[test]
    fn renders_array_and_map_params() {
        let schema = Schema::new()
            .request(
                Method::new("state_getKeys", "Array<string>")
                    .param(Param::new("storage_key", "string"))
                    .param(Param::new("hash", "string").optional()),
            )
            .request(
                Method::new("state_query", "number")
                    .with_param_kind(ParamKind::Map)
                    .param(Param::new("type", "number"))
                    .param(Param::new("include-proofs", "boolean").optional()),
            );

        assert_eq!(
            schema.render_inline(),
            "{\n  requests: [\n    { method: 'state_getKeys'; params: [storage_key: string, hash?: string]; return: Array<string> },\n    { method: 'state_query'; params: { type: number; 'include-proofs'?: boolean }; return: number },\n  ];\n  subscriptions: [];\n}"
        );
    }
}