IssueRaiser

Feature: Generate Typescript functions for Rust commands

Goal: Atomatically generate Typescript functions for Rust commands in Typescript environment Why: Use Typescript with type checking for Rust commands rather than `invoke('some string', somedata)` How: Assume these Rust commands are available ```rust #[tauri::command] async fn my_first_custom_command(message: String) -> Result<String, String> { Ok("simple type command".into()) } #[derive(serde::Serialize)] struct CustomResponse { message: String, other_val: usize, } #[derive(serde::Deserialize)] struct CustomRequest { message: String, request: usize, } #[tauri::command] async fn my_second_custom_command(req: CustomRequest, hello: String) -> Result<CustomResponse, String> { Ok(CustomResponse { message: "custom response".into(), other_val: 42, }) } ``` Using Typescript, we can create functions that conform to the Rust commands: ```typescript import tauriapi from "@tauri-apps/api"; const { invoke } = tauriapi.tauri; function my_first_custom_command(message: String): Promise<String> { return invoke('my_first_custom_command', { message }) } type CustomRequest = { message: String; request: Number; } type CustomResponse = { message: String; other_val: Number; } function my_second_custom_command(req: CustomRequest, hello: String): Promise<CustomResponse>{ return invoke('my_second_custom_command', {req, hello}) } export { my_first_custom_command, my_second_custom_command } ``` Now we can call `my_first_custom_command` and `my

Posted by nganhkhoa on Apr 16, 2021, 10:22 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Other)

Feature: Generate Typescript functions for Rust commands

nganhkhoaApr 16, 2021, 10:22 AM

Goal: Atomatically generate Typescript functions for Rust commands in Typescript environment Why: Use Typescript with type checking for Rust commands rather than `invoke('some string', somedata)` How: Assume these Rust commands are available ```rust #[tauri::command] async fn my_first_custom_command(message: String) -> Result<String, String> { Ok("simple type command".into()) } #[derive(serde::Serialize)] struct CustomResponse { message: String, other_val: usize, } #[derive(serde::Deserialize)] struct CustomRequest { message: String, request: usize, } #[tauri::command] async fn my_second_custom_command(req: CustomRequest, hello: String) -> Result<CustomResponse, String> { Ok(CustomResponse { message: "custom response".into(), other_val: 42, }) } ``` Using Typescript, we can create functions that conform to the Rust commands: ```typescript import tauriapi from "@tauri-apps/api"; const { invoke } = tauriapi.tauri; function my_first_custom_command(message: String): Promise<String> { return invoke('my_first_custom_command', { message }) } type CustomRequest = { message: String; request: Number; } type CustomResponse = { message: String; other_val: Number; } function my_second_custom_command(req: CustomRequest, hello: String): Promise<CustomResponse>{ return invoke('my_second_custom_command', {req, hello}) } export { my_first_custom_command, my_second_custom_command } ``` Now we can call `my_first_custom_command` and `my

Comments (0)

No comments yet. Start the discussion.

Comment on this issue