Skip to main content

Crate utoipa_ts

Crate utoipa_ts 

Source
Expand description

Generate TypeScript API definitions from utoipa endpoint definitions.

utoipa-ts wraps utoipa::path with path so the same endpoint metadata can be used to generate a TypeScript Api type.

§Quick start

Derive both ts_rs::TS and utoipa::ToSchema for types that should appear in generated TypeScript:

use utoipa::ToSchema;

#[derive(ts_rs::TS, ToSchema)]
struct Todo {
    id: String,
    title: String,
    done: bool,
}

#[utoipa_ts::path(
    get,
    path = "/todos",
    responses(
        (status = 200, description = "Todo list", body = Vec<Todo>),
    )
)]
async fn list_todos() {}

utoipa_ts::export!("types/api.ts");

fn main() {}

Then generate the file with:

cargo test export_api

§Existing utoipa projects

Replace #[utoipa::path(...)] with #[utoipa_ts::path(...)] and add export! somewhere in your crate.

§Export path

export! writes to types.ts by default. You can pass a path:

utoipa_ts::export!("types/api.ts");

The UTOIPA_TS_PATH environment variable overrides the macro path.

§Generated type shape

The generated file exports all collected schema declarations and an Api type indexed by "METHOD /path".

§Supported endpoint metadata

utoipa-ts currently reads:

  • HTTP method
  • path = "..."
  • params(...)
  • request_body = Type and request_body(content = Type, ...)
  • response status/body pairs

Re-exports§

pub use ts_rs;

Macros§

export
Export TypeScript API definitions for all collected endpoints to a file

Constants§

DEFAULT_EXPORT_PATH
Default export path for export!
EXPORT_PATH_ENV
Environment variable that can be used to override the export path for export!

Functions§

export_all
Export all endpoints registered with path to a TypeScript file using a path
export_all_default
Export all endpoints registered with path to a TypeScript file using defaults
export_all_from_env_or_path
Export all endpoints registered with path to a TypeScript file using a path with fallbacks

Attribute Macros§

path
Registers a utoipa endpoint for TypeScript API generation