#ts bindings
132 messages · Page 1 of 1 (latest)
Have you heard of tauri-specta?
no, i havent
i read over it.
i don't really like the fact that the bindings file actualy allows you to do bindings.command.
i think i could get used to it, it just seams weird
Can you share the full error and the code that produced it?
@distant zodiac
thread 'main' panicked at USER.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-specta-2.0.0-rc.4\src\lib.rs:382:70:
called `Result::unwrap()` on an `Err` value: BigIntForbidden(src\message.rs:18:35::ListMessage.src_size -> u64)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
i'm assuming because i'm using u64?
*this is the size of a file, fs returns a u64.
ok, i'm using a string now and parsing it in frontent :? (if better way pls tell.
one of my types isn't being put into a ts type.
my error type only used to return from my command is not turned into a ts type.
my code uses it though
- also is there a way to config it to do this:
enum Type {a="A", b="B"}
const Thing = {typ:Type.a, ...} | {typ:Typ.b, ...}
instead of
const Thing = {typ:"A", ...} | {typ:"B", ...}
because i use the enums.
BigInt is forbidden by default, you need to permit it in the ExportConfiguration.
i found that
Are you applying the #[derive(Deserialize, Type)] traits?
All the way down to the standard library types?
it works, but it doesn't make it a seperate ts type
also, i can't seem to get config working.
i don't get what i'm suppost to instatiate
@distant zodiac
halo?
Sorry, I've been trying to replicate your issue.
With the code you've provided, I get the expected types:
#[derive(serde::Deserialize, serde::Serialize, specta::Type)]
pub enum ErrorMessage { NonExistingDirectory { passed: String } }
export type ErrorMessage = { NonExistingDirectory: { passed: string } }
do i need deserialize?
Only if you're taking it as a command input.
it might be (for some reason) because it is only used as a type once as the result from a command.
all the other types that work are used as results for multiple backend functions.
It's easy enough to go through the code and clean out any unnecessary traits when adding the final polish.
?
while your here, how do i use the config? like for bigint.
the most docs there are on it is:
.config() adds a config to the builder
#[cfg(debug_assertions)]
tauri_specta::ts::Exporter::new(specta::collect_types![greet], "../src/bindings.ts")
.with_cfg(
specta::ts::ExportConfiguration::new()
.bigint(specta::ts::BigIntExportBehavior::BigInt)
)
.export()
.expect("error while exporting typescript bindings");
this is currently how i have it:
let specta_builder = {
let specta_builder = tauri_specta::ts::builder()
.commands(tauri_specta::collect_commands![run_check, open_path]);
#[cfg(debug_assertions)]
let specta_builder = specta_builder.path("../src/bindings.ts");
specta_builder.into_plugin()
};
tauri::Builder::default()
.plugin(specta_builder)
...
You can do it that way too.
what's the difference? do commands still work?
I'm pretty sure it's just v1 and v2 differences.
which ones v2?
The one you posted.
so, because i'm using v2. most of these types don't exist.
Export, ExportConfiguration
*its just renamed to ExportConfig
What do you have in your Cargo.toml under [dependencies] for Tauri and Specta?
tauri-specta = { version = "=2.0.0-rc.4", features = ["typescript"] }
specta = "=2.0.0-rc.7"
Are you also using Tauri v2?
:/ no.
Good, because it's not ready for that yet.
i didn't even know that existed
I'll switch to the release candidates. I figured most people weren't using them yet.
Specta v2:
let specta_builder = {
let specta_builder = tauri_specta::ts::builder()
.commands(tauri_specta::collect_commands![greet4, greet5])
.config(
specta::ts::ExportConfig::new()
.bigint(specta::ts::BigIntExportBehavior::BigInt)
);
#[cfg(debug_assertions)]
let specta_builder = specta_builder.path("../src/bindings.ts");
specta_builder.into_plugin()
};
yeah, i got that
You can then import it like: import { commands, ErrorMessage } from "./bindings";
- is there a way to shorten it like:
let specta_builder = tauri_specta::ts::builder()
.commands(tauri_specta::collect_commands![run_check, open_path])
.config(specta::ts::ExportConfig::new()
.bigint(BigIntExportBehavior::Number))
.path("../src/bindings.ts")
.into_plugin();
but still have cfg debug assertions?
If you've got the file, you probably don't need to use the plugin option.
what file?
../src/bindings.ts
wdym got it?
Well, it's in your front-end src.
yes, should i track it on git?
I don't think it matters either way.
well, if i don't then the file won't exsist if people clone the repo
It will be generated when the app is run with debug assertions.
You can track it for reference purposes.
if i use the non-plugin version
- it creates the file still?
- does it update when i change code, or just when first run
As for the plugin, I'm pretty sure all that does is inject the JS code at runtime instead of baking it into the app.
but i'm using ts
yes, but all my other ts gets converted to js automagicly
1. Yes.
2. Every time your app is restarted; the default is every time you change a file in src-tauri.
to restate q2.
i don't have to close and re run npm run tauri dev to retranspile the bindings?
No, it will restart the app automatically whenver you change the Rust files.
I'll take a closer look at the plugin code to see whether it's necessary. I'm not very familiar with specta v2.
me brain just thought about it. of course when the code reruns, it reruns main.
🧠
i'm using #[serde(tag = "typ")] to add the type of the enum into it.
is it possible for tauri_specta to create a seperate ts enum for the type? like this #1199215868693991484 message
Not to my knowledge but you might be better off asking the maintainers.
for now, last thing. the errormessage type still doesn't show
Can you share the file it generates? You can drag-and-drop it into Discord and it will allow you to preview most text files in the app.
It's including it in-line as the return for runCheck. It seems to be an optimisation of some kind.
yes, but i need to use it somewere else in my frontend.
How do you reference it in Rust?
once, in the result from a command
Similar to fn greet(name: &str) -> Result<(), ErrorMessage> {?
i refrence it other places like ErrorMessage::MyType {}
exactly
Ah, I'm having the same problem now.
what?
It's optimising away the reference when I make it a Result<T, E>. When the command returns the type directly, it exports as expected.
why?
Not entirely sure. Taking a look at the source code now.
what have you found?
I think it's Rust that's inlining it.
Maybe. It didn't have the same behaviour when I had two options in it.
Wrapping it in a struct seemed to have done the trick.
wdym?
yeah, my other ones all have a parent struct (and the parent struct is returned from a function)
#[derive(serde::Deserialize, serde::Serialize, specta::Type)]
pub struct ErrorMessageStruct {
error: ErrorMessage
}
#[derive(serde::Deserialize, serde::Serialize, specta::Type)]
#[serde(tag = "typ")]
pub enum ErrorMessage {
NonExistingDirectory { passed: String }
}
huh
Yes, it's weird.
is it rust or specta or tauri_specta?
is there anything you know of which could lead to one of them?
do you know its not tauri_specta?
It's rather unlikely to be tauri_specta as that's just a thin wrapper to bring commands and events into the scope.
yeah
i'm going to make an issue on specta.
you know what i think it is:
it might be that for some reason specta inlines the result value and error.
look: it's inlining both
that's why my other return type (the tree:folder list:Listmessage[]) is not showing.
even though they have specta::Type on it
That might be related. I'm not entirely sure how the types are parsed.
i'll look
these prs are bad. they don't explain anything.
eh, i'll make a new issue, if it's a dupe it'll close
thanks, you helped alot.
I certainly did my best. I'd never used it before so I've learned a lot in the process.
well we both learned :)