rpc_rs/router/
func.rs

1//! A custom version of the [`specta::ts::export_function_header`] function.
2
3use specta::{
4    function::FunctionDataType,
5    ts::{datatype, ExportConfig, Result},
6    TypeMap,
7};
8
9/// Convert a [FunctionDataType] into a function header like would be used in a `.d.ts` file.
10/// If your function requires a function body you can copy this function into your own codebase.
11///
12/// Eg. `function name()`
13/// 
14/// This had to be modified to support async `Promise`s and to not include the final semicolon.
15pub fn export_function_header(dt: FunctionDataType, config: &ExportConfig) -> Result<String> {
16    let type_map = TypeMap::default();
17    let mut s = String::new();
18
19    s.push_str("export ");
20
21    if dt.asyncness {
22        s.push_str("async ");
23    }
24
25    s.push_str("function ");
26    s.push_str(&dt.name);
27    s.push_str("(");
28
29    for (i, (name, ty)) in dt.args.into_iter().enumerate() {
30        if i != 0 {
31            s.push_str(", ");
32        }
33
34        s.push_str(&name);
35        s.push_str(": ");
36        s.push_str(&datatype(config, &ty, &type_map)?);
37    }
38
39    s.push_str(")");
40
41    if let Some(ty) = dt.result {
42        s.push_str(": ");
43
44        if dt.asyncness {
45            s.push_str(&format!("Promise<{}>", datatype(config, &ty, &type_map)?));
46        } else {
47            s.push_str(&datatype(config, &ty, &type_map)?);
48        }
49    }
50
51    Ok(s)
52}