From b8c522fecac6bdd506eb87f7fb2195c5d2c3a489 Mon Sep 17 00:00:00 2001 From: natalie <22222885+espeon@users.noreply.github.com> Date: Sun, 1 Dec 2024 11:41:25 -0600 Subject: [PATCH 1/3] Output non-Procedures TypeScript types --- src/legacy/resolver.rs | 3 +++ src/legacy/router.rs | 18 +++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/legacy/resolver.rs b/src/legacy/resolver.rs index c54ff0b5..6ede16c2 100644 --- a/src/legacy/resolver.rs +++ b/src/legacy/resolver.rs @@ -122,5 +122,8 @@ pub fn typedef(defs: &mut TypeMap) -> ProcedureDataTy let mut type_map = TypeMap::default(); let arg_ty = TArg::reference(&mut type_map, &[]).inner; let result_ty = TResult::reference(&mut type_map, &[]).inner; + + defs.append(&mut type_map); + ProcedureDataType { arg_ty, result_ty } } diff --git a/src/legacy/router.rs b/src/legacy/router.rs index 321bcfde..eb693e49 100644 --- a/src/legacy/router.rs +++ b/src/legacy/router.rs @@ -161,15 +161,19 @@ export type Procedures = {{ }};"# )?; - for export in self.type_map.iter().map(|(_, ty)| { - datatype( - &config, - &FunctionResultVariant::Value(ty.inner.clone()), - &self.type_map, + // Generate type exports (non-Procedures) + for (name, export) in self.type_map.iter().map(|(_, ty)| { + ( + ty.name(), + datatype( + &config, + &FunctionResultVariant::Value(ty.inner.clone()), + &self.type_map, + ) + .unwrap(), ) - .unwrap() }) { - writeln!(file, "\n{}", export)?; + writeln!(file, "\nexport type {} = {};", name, export)?; } Ok(()) From e91fa65c93a8289d44a63512053ee184433f3ea4 Mon Sep 17 00:00:00 2001 From: natalie <22222885+espeon@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:37:31 -0600 Subject: [PATCH 2/3] Use export_named_datatype instead of string template --- src/legacy/router.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/legacy/router.rs b/src/legacy/router.rs index eb693e49..cebf50a7 100644 --- a/src/legacy/router.rs +++ b/src/legacy/router.rs @@ -11,7 +11,7 @@ use std::{ use futures::Stream; use serde_json::Value; use specta::{datatype::FunctionResultVariant, DataType, TypeMap}; -use specta_typescript::{self as ts, datatype, Typescript}; +use specta_typescript::{self as ts, datatype, export_named_datatype, Typescript}; use crate::{ internal::{Procedure, ProcedureKind, ProcedureStore, RequestContext, ValueOrStream}, @@ -162,18 +162,12 @@ export type Procedures = {{ )?; // Generate type exports (non-Procedures) - for (name, export) in self.type_map.iter().map(|(_, ty)| { - ( - ty.name(), - datatype( - &config, - &FunctionResultVariant::Value(ty.inner.clone()), - &self.type_map, - ) - .unwrap(), - ) - }) { - writeln!(file, "\nexport type {} = {};", name, export)?; + for export in self + .type_map + .iter() + .map(|(_, ty)| export_named_datatype(&config, ty, &self.type_map).unwrap()) + { + writeln!(file, "\n{}", export)?; } Ok(()) From edd828c48dab13d397a57c4bd4897045ebfb6bc8 Mon Sep 17 00:00:00 2001 From: natalie <22222885+espeon@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:40:35 -0600 Subject: [PATCH 3/3] Pass in defs instead of appending --- src/legacy/resolver.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/legacy/resolver.rs b/src/legacy/resolver.rs index 6ede16c2..145dc4a1 100644 --- a/src/legacy/resolver.rs +++ b/src/legacy/resolver.rs @@ -119,11 +119,8 @@ where } pub fn typedef(defs: &mut TypeMap) -> ProcedureDataType { - let mut type_map = TypeMap::default(); - let arg_ty = TArg::reference(&mut type_map, &[]).inner; - let result_ty = TResult::reference(&mut type_map, &[]).inner; + let arg_ty = TArg::reference(defs, &[]).inner; + let result_ty = TResult::reference(defs, &[]).inner; - defs.append(&mut type_map); - ProcedureDataType { arg_ty, result_ty } }