Quick Start

    1. Install the VSCode ExtensionπŸ”—

    Install TYML for VSCode from the VSCode Marketplace.

    tyml_vscode

    2. Define Your APIπŸ”—

    Create a file named api.tyml anywhere.

    interface API {
        function hello() -> string {
            return "Hello, world!"
        }
    }
    

    3. Install TYML CLI ToolsπŸ”—

    Install with the following command (requires Rust’s cargo):

    cargo install tyml_core tyml_api_generator
    

    4. Generate Code and ImplementπŸ”—

    In this example, the server is Rust and the client is TypeScript.

    Server (Rust)πŸ”—

    First, create a test crate.

    cargo new api-example-server
    

    Then generate types using the api.tyml defined in step 2.

    tyml-api-gen server rust-axum api.tyml ./api-example-server/api
    

    If you see Success!, it worked.

    Add the generated api plus async-trait and tokio to Cargo.toml:

    [dependencies]
    api = { path = "./api/" }
    async-trait = "0.1"
    tokio = { version = "1", features = ["full"] }
    

    Finally, edit main.rs to implement the API.

    use api::{serve, types::API};
    use async_trait::async_trait;
    
    struct Server {}
    
    #[async_trait]
    impl API for Server {
        async fn hello(&self) -> String {
            "Hello, world!".to_string()
        }
    }
    
    #[tokio::main]
    async fn main() {
        let server = Server {};
    
        serve(server, "localhost:3000").await.unwrap();
    }
    

    ClientπŸ”—

    Generate TypeScript types.

    tyml-api-gen client typescript api.tyml ./api-example-client/api
    

    Then create main.ts inside ./api-example-client and call the API.

    import { API } from "./api/types.ts";
    
    async function main() {
        const api = new API("http://localhost:3000")
    
        /// Call it like a method
        const result = await api.hello()
    
        console.log(result)
    }
    
    main()
    

    5. RunπŸ”—

    Run the following commands in the server and client directories respectively.

    ServerπŸ”—

    cargo run
    

    ClientπŸ”—

    npx tsx main.ts
    

    ResultπŸ”—

    If the client prints Hello, world!, it worked.