Skip to main content

toolkit/api/
mod.rs

1//! Type-safe API operation builder with compile-time guarantees
2//!
3//! This gear provides a type-state builder pattern that enforces at compile time
4//! that API operations cannot be registered unless both a handler and at least one
5//! response are specified.
6
7pub mod api_dto;
8pub mod canonical_error_layer;
9pub mod error_layer;
10pub mod odata;
11pub mod openapi_registry;
12pub mod operation_builder;
13pub mod response;
14pub mod select;
15
16#[cfg(test)]
17#[cfg_attr(coverage_nightly, coverage(off))]
18mod odata_policy_tests;
19
20pub use canonical_error_layer::canonical_error_middleware;
21pub use error_layer::{
22    IntoCanonical, error_mapping_middleware, extract_trace_id, map_error_to_canonical,
23};
24pub use openapi_registry::{OpenApiInfo, OpenApiRegistry, OpenApiRegistryImpl, ensure_schema};
25pub use operation_builder::{
26    Missing, OperationBuilder, OperationSpec, ParamLocation, ParamSpec, Present, RateLimitSpec,
27    ResponseSpec, state,
28};
29pub use select::{apply_select, page_to_projected_json, project_json};
30
31/// Prelude that re-exports the canonical error types and common API utilities.
32pub mod canonical_prelude {
33    // Canonical error types
34    pub use toolkit_canonical_errors::{CanonicalError, Problem, resource_error};
35
36    /// Result type alias for handlers using the canonical error catalog.
37    ///
38    /// Returns [`CanonicalError`] (not [`Problem`]) so handler `?` chains
39    /// resolve through `From<DomainError> for CanonicalError` — the
40    /// long-lived per-gear mapping. The canonical error middleware
41    /// (`toolkit::api::canonical_error_middleware`) converts the
42    /// `CanonicalError` to a wire `Problem` and fills `instance` /
43    /// `trace_id` on the way out, so handlers never need to construct a
44    /// `Problem` themselves.
45    pub type ApiResult<T = ()> = std::result::Result<T, CanonicalError>;
46
47    // Same response sugar / OData / axum re-exports as the legacy prelude
48    pub use super::odata::OData;
49    pub use super::response::{JsonBody, JsonPage, created_json, no_content, ok_json};
50    pub use super::select::apply_select;
51    pub use axum::{Json, http::StatusCode, response::IntoResponse};
52}