gluer
A wrapper for Rust frameworks that eliminates redundant type and function definitions between the frontend and backend. Currently, it supports only the axum framework.
Origin of the Name
The name "gluer" is inspired by the tool's primary function, gluing together different parts of a Rust-based web application. Just as glue binds different materials together to form a cohesive whole, gluer integrates various components of the frontend and backend, ensuring they work seamlessly without redundant code.
Installation
Add this to your Cargo.toml:
[]
= "0.3.1"
Features
Note: This crate is in an early stage and may not work in all cases. Please open an issue if you encounter any problems!
- Define routing and api generation as outlined in How to use.
- Infer input and output types of functions (currently supports only
Json<...>for inputs). - Convert Rust structs to TypeScript interfaces.
- Generate a TypeScript file with functions and data types.
- The generated TypeScript file does not depend on any external TypeScript libraries.
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 Structs and Functions
Start by using the #[cached] macro to define your data structures and functions. This macro gives gluer access to the necessary code for type inference and conversion.
use ;
use cached;
// Define a struct with the cached macro
// Define the functions with the cached macro
async
async
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 ;
// done like above
async
async
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
Generate the API file using the api! macro. This macro generates the TypeScript file at compile time.
use api;
// Generate the TypeScript API
api!;
Complete Example
Below is a complete example demonstrating the use of gluer with axum:
use ;
use ;
async
async
async