functions.rs

The src/functions.rs file is the heart of your backend in NoApi. This is where you define your Rust Server Functions (RSFs), which are the functions that handle your backend logic. These functions can perform tasks like processing data, interacting with a database, or executing business logic.

  • When you add or modify functions in src/functions.rs, NoApi automatically generates corresponding TypeScript definitions in the functions.ts file. This allows you to call these Rust functions directly from your frontend using TypeScript, with full type safety. For example, if you define a function get_user in src/functions.rs, you can import and use it in your frontend like this:

Rust code( functions.rs )

#![allow(unused)]
fn main() {
fn get_user(id:u32)->User{
 ///
}

}

Typescript code( your frontend )

import { get_user } from "@functions";

let id: number = 1;
let user: User = await get_user(id);

NoApi also ensures that the types used in your Rust functions are correctly converted to TypeScript types, making communication between the frontend and backend seamless. This eliminates the need for manual type definitions or additional boilerplate code.

Why this approach?

This tight integration between Rust and TypeScript simplifies development and reduces the risk of runtime errors.