/rocket-ts

Generate types directly from Rocket request handlers

Primary LanguageRustMIT LicenseMIT

Rocket 💫 TypeScript

License

rocket-ts generates TypeScript interfaces directly from Rocket route handlers. Using generated types on your frontend ensures it remains in sync with every 🚀 endpoint, enabling you to quickly find errors and add additional safety to your frontend.

Inspired by TypeShare, it aims to bring kindness to frontend engineers working with Rocket. 🤗

Features

  • Generates TypeScript interfaces directly from Rocket routes and handlers
  • Supports exclusion of parameters created via Rocket request guards
  • Flexible command-line interface
  • Fast and efficient generation

rocket-ts in action

Imagine an email service with a /thread API endpoint containing various routes, such as get_thread:

#[get("/thread/<kid_or_ticket_mask>", format = "json")]
async fn get_thread(kid_or_ticket_mask: &str, service: AgentService) -> K7Response<Thread> {
    service.get_thread(kid_or_ticket_mask).await.into()
}

This and similar routes can be found in example-handlers/thread.rs. Assume that AgentService implements FromRequest, enabling Rocket to create it automatically, so we exclude it during generation:

cargo run generate -i example-handlers -e ./example-handlers/exclude.txt
/*
 * Generated by rocket-ts 0.1.0 🚀 🌎
 */
export interface k7 {
	// thread.rs
	// handler "/thread/<kid_or_ticket_mask>"
	get_thread: (kid_or_ticket_mask:string) => Thread;
	// handler "/debug/thread/<kid>"
	get_thread_debug: (kid:string) => ThreadDebug;
	// handler "/thread/<thread_id>/comments"
	get_thread_comments: (thread_id:number) => Comment[];
	// handler "/thread/<thread_id>/insights"
	get_thread_insights: (thread_id:number) => MessageInsights;
	// handler "/thread/escalate"
	escalate_thread: (ThreadEscalation) => any;
}

Installation

  1. Ensure Rust is installed on your system. If not, download and install it from the official Rust website: Rust Installation Guide.

  2. Clone this repository:

git clone https://github.com/Kindness-Works/rocket-ts.git
  1. Navigate to the project directory:
cd rocket-ts
  1. Build the project:
cargo build --release

The binary will be available in the target/release directory.

Usage

To generate TypeScript interfaces, utilize the generate subcommand:

Usage: rocket-ts generate [OPTIONS] --input <INPUT>

Options:
  -i, --input <INPUT>           Input directory or file to parse for interface generation.
  -o, --output <OUTPUT>         Optional output file. STDOUT if not provided.
  -e, --exclude-type <EXCLUDE>  File listing parameters to exclude (e.g., Request Guards).

Example project

Suppose you have a Rocket project structured as follows:

my-rocket-project/
├── src/
│   ├── main.rs
│   ├── routes/
│   │   ├── users.rs
│   │   └── posts.rs
│   └── guards/
│       └── auth.rs
└── exclude.txt

To generate TypeScript interfaces for the users and posts modules, excluding parameters specified in exclude.txt, run:

rocket-ts generate -i src/routes -o server-api.ts -e exclude.txt

This command generates a server-api.ts file containing TypeScript interfaces for each request handler in users.rs and posts.rs, excluding parameters specified in exclude.txt (e.g., the auth guard).


Contributing

Contributions are welcomed! Feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.