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 = Typeandrequest_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
pathto a TypeScript file using a path - export_
all_ default - Export all endpoints registered with
pathto a TypeScript file using defaults - export_
all_ from_ env_ or_ path - Export all endpoints registered with
pathto a TypeScript file using a path with fallbacks