Skip to content

A small utility that generates a typescript declaration file for the invoke function from functions found in code by Tauri commands. Thanks to this, there is no mistaking the name of the command.

License

Notifications You must be signed in to change notification settings

Ertanic/tauri-typed-invoke

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

tauri-named-invoke is a small utility that generates a typescript declaration file for the invoke function from functions found in code by Tauri commands. Thanks to this, there is no mistaking the name of the command.

Example

main.rs:

fn main() {
    tauri::Builder::default()
        .invoke_handler(generate_handler![get_weather, get_config])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

#[tauri::command]
fn get_weather() -> String {
    "sunny".to_string()
}
// or
use tauri::command;
#[command]
fn get_config() -> String {
    "config".to_string()
}

build.rs:

fn main() {
    tauri_named_invoke::build("ui").unwrap();
    tauri_build::build();
}

The file will be generated at the following path:

project root
├── ui
│   └── invoke.d.ts
├── src
│   └── main.rs
└── Cargo.toml

The generated file will contain:

import * as tauri from '@tauri-apps/api/tauri';
declare module '@tauri-apps/api' {
    type Commands = 
          'get_weather'
        | 'get_config';

    function invoke<T>(cmd: Commands, args?: InvokeArgs): Promise<T>;
}

About

A small utility that generates a typescript declaration file for the invoke function from functions found in code by Tauri commands. Thanks to this, there is no mistaking the name of the command.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages