Skip to content

Commit

Permalink
Update specta
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavenVolkoff committed Aug 10, 2024
1 parent 14c6e3a commit 621ebfd
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 93 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ bytesize = ["specta/bytesize"]
glam = ["specta/glam"]

[dependencies]
specta = { version = "=2.0.0-rc.11", features = ["serde", "serde_json", "typescript", "tokio"] }
specta = { git = "https://github.com/oscartbeaumont/specta", rev = "v2.0.0-rc.20", features = ["derive", "serde", "serde_json"] }
specta-serde = { git = "https://github.com/oscartbeaumont/specta", rev = "v2.0.0-rc.20" }
specta-typescript = { git = "https://github.com/oscartbeaumont/specta", rev = "v2.0.0-rc.20", features = ["function"] }
specta-zod = { git = "https://github.com/oscartbeaumont/specta", rev = "v2.0.0-rc.20" }
specta-datatype-from = { git = "https://github.com/oscartbeaumont/specta", rev = "v2.0.0-rc.20" }
httpz = { path = "./httpz", optional = true }
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95"
serde = { version = "1.0.205", features = ["derive"] }
serde_json = "1.0.122"
thiserror = "1.0.40"
futures = "0.3.28"
tokio = { version = "1.27.0", features = ["sync", "rt", "macros"] }
Expand Down
3 changes: 2 additions & 1 deletion src/alpha_stable/resolver_function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{borrow::Cow, marker::PhantomData};

use serde::de::DeserializeOwned;
use specta::{ts, Type, TypeMap};
use specta::{Type, TypeMap};
use specta_typescript as ts;

use crate::{alpha_stable::AlphaRequestLayer, internal::ProcedureDataType};

Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{error, fmt, sync::Arc};

use serde::Serialize;
use specta::{ts, Type};
use specta::Type;

use crate::internal::jsonrpc::JsonRPCError;

Expand Down Expand Up @@ -89,7 +89,7 @@ pub enum ExportError {
#[error("IO error exporting bindings: {0}")]
IOErr(#[from] std::io::Error),
#[error("error exporting typescript bindings: {0}")]
TsExportErr(#[from] ts::ExportError),
TsExportErr(#[from] specta_zod::ExportError),
}

#[derive(Debug, Clone, Serialize, Type)]
Expand Down
66 changes: 0 additions & 66 deletions src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,3 @@ pub use procedure_store::*;

#[cfg(not(feature = "unstable"))]
pub use specta;

#[cfg(test)]
#[cfg(not(feature = "unstable"))]
mod tests {
use std::{fs::File, io::Write, path::PathBuf};

use specta::{ts::export_datatype, DefOpts, Type, TypeDefs};

macro_rules! collect_datatypes {
($( $i:path ),* $(,)? ) => {{
use specta::DataType;

let mut tys = TypeDefs::default();

$({
let def = <$i as Type>::definition(DefOpts {
parent_inline: true,
type_map: &mut tys,
});

if let Ok(def) = def {
if let DataType::Named(n) = def {
if let Some(sid) = n.sid {
tys.insert(sid, Some(n));
}
}
}
})*
tys
}};
}

// rspc has internal types that are shared between the frontend and backend. We use Specta directly to share these to avoid a whole class of bugs within the library itself.
#[test]
fn export_internal_types() {
let mut file = File::create(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("./packages/client/src/bindings.ts"),
)
.unwrap();

file.write_all(
b"// DO NOT MODIFY. This file was generated by Specta and is used to keep rspc internally type safe.\n// Checkout the unit test 'export_internal_types' to see where this files comes from!",
)
.unwrap();

let tys = collect_datatypes! {
super::ProcedureDataType,
// crate::Procedures, // TODO
super::jsonrpc::Request,
// super::jsonrpc::Response, // TODO
};

for (_, ty) in tys
.iter()
.filter_map(|(sid, v)| v.as_ref().map(|v| (sid, v)))
{
file.write_all(b"\n\n").unwrap();
file.write_all(
export_datatype(&Default::default(), &ty, &tys)
.unwrap()
.as_bytes(),
)
.unwrap();
}
}
}
7 changes: 2 additions & 5 deletions src/internal/procedure_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ use std::{borrow::Cow, collections::BTreeMap, future::ready, pin::Pin};

use futures::{stream::once, Stream};
use serde_json::Value;
use specta::{DataType, DataTypeFrom};
use specta::DataType;
use specta_datatype_from::DataTypeFrom;

use crate::ExecError;

use super::{Layer, RequestContext, ValueOrStream};

/// Represents a Typescript procedure file which is generated by the Rust code.
/// This is codegenerated Typescript file is how we can validate the types on the frontend match Rust.
///
/// @internal
#[derive(Debug, Clone, DataTypeFrom)]
#[cfg_attr(test, derive(specta::Type))]
#[cfg_attr(test, specta(rename = "ProcedureDef"))]
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ pub use selection::*;
pub mod integrations;
pub mod internal;

// #[deprecated = "Being removed in `v1.0.0`. Import this directly from the 'specta' crate."] // TODO
#[cfg(not(feature = "unstable"))]
pub use specta::RSPCType as Type;

#[cfg(debug_assertions)]
#[allow(clippy::panic)]
// #[deprecated = "Being removed in `v1.0.0`. You can copy this helper into your own codebase if you still need it."] // TODO
Expand Down
16 changes: 6 additions & 10 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use std::{

use futures::{Stream, StreamExt};
use serde_json::Value;
use specta::{
ts::{self, datatype, ExportConfig},
DataType, TypeMap,
};
use specta::{DataType, TypeMap, internal::detect_duplicate_type_names};
use specta_zod::{BigIntExportBehavior, ExportConfig, export_named_datatype, datatype};

use crate::{
internal::{Procedure, ProcedureKind, ProcedureStore, RequestContext},
Expand Down Expand Up @@ -137,9 +135,7 @@ where
}
writeln!(file, "// This file was generated by [rspc](https://github.com/oscartbeaumont/rspc). Do not edit this file manually.")?;

let config = ExportConfig::new().bigint(
ts::BigIntExportBehavior::BigInt
);
let config = ExportConfig::new().bigint(BigIntExportBehavior::BigInt);

let queries_ts =
generate_procedures_ts(&config, self.queries.store.iter(), &self.typ_store());
Expand All @@ -159,17 +155,17 @@ export type Procedures = {{
}};"#
)?;

for (name, a, b) in specta::detect_duplicate_type_names(&self.typ_store) {
for (name, a, b) in detect_duplicate_type_names(&self.typ_store).into_iter().next() {
return Err(ExportError::TsExportErr(
ts::ExportError::DuplicateTypeName(name, a, b),
specta_zod::ExportError::DuplicateTypeName(name, a, b),
));
}

for (_sid, dt) in self.typ_store.iter() {
writeln!(
file,
"\n{}",
ts::export_named_datatype(&config, dt, &self.typ_store)?
export_named_datatype(&config, dt, &self.typ_store)?
)?;
}

Expand Down
3 changes: 2 additions & 1 deletion src/router_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{borrow::Cow, marker::PhantomData, panic::Location, process};

use serde::de::DeserializeOwned;
use specta::{ts, Type, TypeMap};
use specta::{Type, TypeMap};
use specta_typescript as ts;

use crate::{
internal::{
Expand Down
3 changes: 2 additions & 1 deletion src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ macro_rules! selection {

#[cfg(test)]
mod tests {
use specta::{ts::inline, Type};
use specta::Type;
use specta_typescript::inline;

fn ts_export_ref<T: Type>(_t: &T) -> String {
inline::<T>(&Default::default()).unwrap()
Expand Down

0 comments on commit 621ebfd

Please sign in to comment.