Git Product home page Git Product logo

rocket-ts's Introduction

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.

rocket-ts's People

Contributors

avaterclasher avatar dteare avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.