Skip to main content

roam_codegen/targets/typescript/
server.rs

1//! TypeScript server/handler generation.
2//!
3//! Generates the handler interface and a Dispatcher class that routes calls
4//! to handler methods. All encode/decode is handled by the runtime via the
5//! service descriptor — no serialization code in generated output.
6
7use heck::{ToLowerCamelCase, ToUpperCamelCase};
8use roam_types::{ServiceDescriptor, ShapeKind, classify_shape};
9
10use super::types::{ts_type_server_arg, ts_type_server_return};
11
12/// Generate handler interface (for implementing the service).
13///
14/// r[impl rpc.channel.binding] - Handler binds channels in args.
15pub fn generate_handler_interface(service: &ServiceDescriptor) -> String {
16    let mut out = String::new();
17    let service_name = service.service_name.to_upper_camel_case();
18
19    out.push_str(&format!("// Handler interface for {service_name}\n"));
20    out.push_str(&format!("export interface {service_name}Handler {{\n"));
21
22    for method in service.methods {
23        let method_name = method.method_name.to_lower_camel_case();
24        let args = method
25            .args
26            .iter()
27            .map(|a| {
28                format!(
29                    "{}: {}",
30                    a.name.to_lower_camel_case(),
31                    ts_type_server_arg(a.shape)
32                )
33            })
34            .collect::<Vec<_>>()
35            .join(", ");
36        let ret_ty = ts_type_server_return(method.return_shape);
37
38        out.push_str(&format!(
39            "  {method_name}({args}): Promise<{ret_ty}> | {ret_ty};\n"
40        ));
41    }
42
43    out.push_str("}\n\n");
44    out
45}
46
47/// Generate the Dispatcher class.
48///
49/// Implements `ChannelingDispatcher` from roam-core:
50/// - `getDescriptor()` returns the service descriptor
51/// - `dispatch(method, args, call)` routes by method ID and calls handler methods
52///
53/// The runtime handles all arg decoding (using method.args tuple schema) and
54/// response encoding (using method.result schema via call.reply/replyErr).
55/// Generated dispatch code only does type casts and handler invocation.
56pub fn generate_dispatcher_class(service: &ServiceDescriptor) -> String {
57    use crate::render::hex_u64;
58
59    let mut out = String::new();
60    let service_name = service.service_name.to_upper_camel_case();
61    let service_name_lower = service.service_name.to_lower_camel_case();
62
63    out.push_str(&format!("// Dispatcher for {service_name}\n"));
64    out.push_str(&format!(
65        "export class {service_name}Dispatcher implements ChannelingDispatcher {{\n"
66    ));
67    out.push_str(&format!(
68        "  constructor(private readonly handler: {service_name}Handler) {{}}\n\n"
69    ));
70
71    // getDescriptor()
72    out.push_str("  getDescriptor(): ServiceDescriptor {\n");
73    out.push_str(&format!("    return {service_name_lower}_descriptor;\n"));
74    out.push_str("  }\n\n");
75
76    // dispatch()
77    out.push_str(
78        "  async dispatch(method: MethodDescriptor, args: unknown[], call: RoamCall): Promise<void> {\n",
79    );
80
81    let mut first = true;
82    for method in service.methods {
83        let method_name = method.method_name.to_lower_camel_case();
84        let id = crate::method_id(method);
85        let is_fallible = matches!(
86            classify_shape(method.return_shape),
87            ShapeKind::Result { .. }
88        );
89
90        // Build typed arg list from args array
91        let arg_names: Vec<_> = method
92            .args
93            .iter()
94            .map(|a| a.name.to_lower_camel_case())
95            .collect();
96        let typed_args: Vec<_> = method
97            .args
98            .iter()
99            .enumerate()
100            .map(|(i, a)| format!("args[{i}] as {}", ts_type_server_arg(a.shape)))
101            .collect();
102
103        // Find Tx arg indices for closing after handler returns
104        let tx_arg_indices: Vec<usize> = method
105            .args
106            .iter()
107            .enumerate()
108            .filter(|(_, a)| matches!(classify_shape(a.shape), ShapeKind::Tx { .. }))
109            .map(|(i, _)| i)
110            .collect();
111
112        let keyword = if first { "if" } else { "} else if" };
113        first = false;
114
115        out.push_str(&format!(
116            "    {keyword} (method.id === {}n) {{\n",
117            hex_u64(id)
118        ));
119        out.push_str("      try {\n");
120        out.push_str(&format!(
121            "        const result = await this.handler.{method_name}({});\n",
122            typed_args.join(", ")
123        ));
124
125        // Close Tx args before replying (ensures Close messages precede Response)
126        for i in &tx_arg_indices {
127            let arg_name = &arg_names[*i];
128            out.push_str(&format!(
129                "        (args[{i}] as {{ close(): void }}).close(); // close {arg_name} before reply\n"
130            ));
131        }
132
133        if is_fallible {
134            out.push_str("        if (result.ok) call.reply(result.value); else call.replyErr(result.error);\n");
135        } else {
136            out.push_str("        call.reply(result);\n");
137        }
138
139        out.push_str("      } catch {\n");
140        out.push_str("        call.replyInternalError();\n");
141        out.push_str("      }\n");
142    }
143
144    if !first {
145        out.push_str("    }\n");
146    }
147
148    out.push_str("  }\n");
149    out.push_str("}\n\n");
150    out
151}
152
153/// Generate complete server code (handler interface + dispatcher class).
154pub fn generate_server(service: &ServiceDescriptor) -> String {
155    let mut out = String::new();
156    out.push_str(&generate_handler_interface(service));
157    out.push_str(&generate_dispatcher_class(service));
158    out
159}