gluer
A wrapper for rust frameworks which addresses the persistent issue of redundant type and function definitions between the frontend and backend. At present, it exclusively supports the axum framework.
Installation
Add this to your Cargo.toml:
[]
= "0.3.0"
Disclaimer
Please be informed that this crate is in a very early state and is expected to work in not every case. Open a Issue if you encounter one! What works is:
- Defining the routing and api generation as outlined in How to use
- Inferring the input and output types of functions (but only
Json<...>for inputs) - Converting them to ts types
- Generating the ts file with the functions and data types
How to use
gluer generates an api endpoint .ts file which expects that you build your frontend statically and host it via axum's static file serving. To use it, follow these steps:
Step 1: Define Parameters and Functions
Start by using the #[param] and fun! macros to define your data structures and functions. These macros give gluer access to the necessary code for type inference and conversion.
use ;
use ;
// Define a parameter with the param macro
// Define functions with the fun macro
fun!
Step 2: Add Routes
Use the add_route! macro to add API-important routes to your router. Note that inline functions cannot generally be used due to Rust's limitations in inferring types in macros.
use ;
use ;
// a part of the function above
fun!
let mut app: = new;
// Add non-API-important route without macro
app = app.route;
// Add API-important routes with the add_route macro
add_route!;
Step 3: Generate API Specification
Generate the API specification file using the gen_ts! macro. This macro generates the TypeScript file at compile time.
use gen_ts;
// Generate the TypeScript API specification
gen_ts!;
Complete Example
Below is a complete example demonstrating the use of gluer with axum:
use ;
use ;
fun!
async