Expand description
§Typeway — Type-Level Web Framework for Rust
Typeway is a web framework where your entire API is described as a Rust type. Servers, clients, and OpenAPI schemas are all derived from that single type definition. If the types compile, the pieces fit together.
Built on Tokio, Tower, and Hyper — fully compatible with the Axum ecosystem.
§Quick Start
ⓘ
use typeway::prelude::*;
// 1. Define path types
typeway_path!(type HelloPath = "hello");
typeway_path!(type GreetPath = "greet" / String);
// 2. Define the API as a type
type API = (
GetEndpoint<HelloPath, String>,
GetEndpoint<GreetPath, String>,
);
// 3. Write handlers
async fn hello() -> &'static str { "Hello, world!" }
async fn greet(path: Path<GreetPath>) -> String {
let (name,) = path.0;
format!("Hello, {name}!")
}
// 4. Serve — the compiler verifies every endpoint has a handler
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Server::<API>::new((
bind!(hello),
bind!(greet),
))
.serve("0.0.0.0:3000".parse()?)
.await?;
Ok(())
}§Feature Flags
| Flag | Default | Description |
|---|---|---|
server | yes | HTTP server (Tower/Hyper) |
client | no | Type-safe HTTP client (reqwest) |
openapi | no | OpenAPI 3.1 spec generation |
full | no | All features |
Re-exports§
pub use http;pub use serde;pub use serde_json;pub use typeway_server::tower_http;
Modules§
- api
- The
ApiSpectrait — marks a type as a valid API specification. - docs
- Handler documentation metadata for OpenAPI spec generation.
- effects
- Type-level middleware effects for compile-time enforcement.
- endpoint
- The
Endpointtype — a type-level HTTP endpoint descriptor. - method
- HTTP method types for type-level route encoding.
- negotiate
- Content negotiation type-level primitives.
- path
- Type-level path segment encoding using heterogeneous lists.
- prelude
- Convenience prelude — import everything you typically need.
- session
- Session type primitives for protocol-typed WebSocket channels.
- versioning
- Type-level API versioning with typed deltas and compile-time compatibility checks.
Macros§
- assert_
api_ compatible - Verify at compile time that every endpoint in the old API exists in the new API (type equality on the full endpoint type).
- bind
- Convenience macro to bind a handler to an endpoint without turbofish.
- endpoint
- Defines an endpoint type with builder-style options.
- path
- Constructs a path type expression. Unlike
typeway_path!, this does NOT generate marker types — it references markers that were already defined by atypeway_path!ortypeway_api!invocation. - typeway_
api - Defines a complete API type with inline route definitions.
- typeway_
path - Defines a path type with auto-generated literal segment markers.
Structs§
- Added
- Marks an endpoint as added in this version (not present in the base).
- Auth
Required - Authentication middleware required.
- Bound
Handler - A handler bound to a specific endpoint type, with the handler’s arg types erased.
- Capture
- A captured path segment of type
T. - Capture
Rest - Matches all remaining path segments as a
Vec<String>. - Cors
Required - CORS middleware required.
- Delete
- HTTP DELETE method.
- Deprecated
- Marks an endpoint as deprecated but still present.
- ECons
- Type-level cons cell for effect lists.
- EHere
- Type-level index: the effect is at this position in the list.
- ENil
- Type-level empty effect list.
- EThere
- Type-level index: the effect is at a later position in the list.
- Effectful
Layered Server - An effectful server with Tower middleware layers applied.
- Effectful
Server - A server builder that tracks which middleware effects have been provided.
- End
- Protocol termination. No further messages can be sent or received.
- Endpoint
- A type-level HTTP endpoint descriptor.
- Extension
- Extracts a value from request extensions.
- Get
- HTTP GET method.
- HCons
- Heterogeneous list cons cell.
- HNil
- Heterogeneous list terminator.
- Handler
Doc - Documentation metadata extracted from a handler function’s doc comments.
- Head
- HTTP HEAD method.
- Header
- Extracts a single header value by name.
- Here
- Type-level index: the endpoint is at this position in the tuple.
- Json
- A JSON response wrapper.
- Json
Error - A structured JSON error response.
- Layered
Server - A server with Tower middleware layers applied.
- Lit
- A literal path segment wrapper in the HList.
- Negotiated
- A response that supports multiple content types via negotiation.
- NoBody
- Marker type indicating no request body.
- Offer
- Offer a choice between two protocol branches.
- Options
- HTTP OPTIONS method.
- Patch
- HTTP PATCH method.
- Path
- Extracts typed path captures from the URL.
- Post
- HTTP POST method.
- Put
- HTTP PUT method.
- Query
- Extracts typed query string parameters.
- Rate
Limit Required - Rate limiting middleware required.
- Rec
- Recursive protocol marker.
- Recv
- Receive a message of type
T, then continue with protocolNext. - Removed
- Marks an endpoint as removed in this version.
- Replaced
- Marks an endpoint as replaced: the old signature becomes the new one.
- Requires
- An endpoint that requires a specific middleware effect.
- Router
- A runtime HTTP router.
- Router
Service - A
tower::Servicewrapper around a sharedRouter. - Secure
Headers Layer - A Tower layer that adds security headers to every response.
- Select
- Select one of two protocol branches.
- Server
- A type-safe HTTP server parameterized by an API specification.
- State
- Extracts shared application state.
- There
- Type-level index: the endpoint is at a later position in the tuple.
- Tracing
Required - Tracing/logging middleware required.
- Var
- Variable referencing the enclosing
Rec. - Versioned
Api - A versioned API type carrying its lineage as type parameters.
Traits§
- AllProvided
- Asserts that all effects required by an API type are present in the
provided effects list
P. - ApiChangelog
- Describes the changes between two API versions.
- ApiSpec
- Marker trait for valid API specifications.
- Backward
Compatible - Marker trait asserting that a newer API version is backward-compatible with an older one.
- Captures
Prepend - Runtime counterpart of
Prepend— constructs a tuple value by prepending a value onto an existing tuple value. - Change
Marker - Sealed helper trait implemented by each change marker.
Used internally by
ApiChangelogcounting logic. - Content
Format - Trait implemented by format marker types that represent a content type.
- Dual
- Compute the dual (mirror) of a session type.
- Effect
- Marker trait for middleware effects.
- Extract
Path - Runtime path parsing, paired with the compile-time
PathSpec. - From
Request - Extract a value by consuming the request body.
- From
Request Parts - Extract a value from request metadata (URI, headers, extensions).
- Handler
- A handler that can process an HTTP request and produce a response.
- HasEffect
- Trait asserting that an effect list contains a specific effect.
- HasEndpoint
- Trait asserting that an API tuple contains a specific endpoint type.
- Http
Method - Associates a zero-sized method type with its
http::Methodvalue. - Into
Response - Trait for types that can be converted into an HTTP response.
- IsEndpoint
- Helper trait that is satisfied when
Selfis the same type asE. - LitSegment
- Trait implemented by literal path segment marker types.
- Named
Header - Trait for types that can be extracted from a named HTTP header.
- Path
Spec - Computes the tuple of captured types from a path HList.
- Prepend
- Prepends a type
Tonto a tuple, producing a tuple one element larger. - Serves
- A tuple of bound handlers that fully covers an API specification.
- Session
Type - Marker trait for valid session types.
- Version
Info - Metadata about an API version, used by OpenAPI generation and documentation tooling.
Functions§
- bind
- Bind a handler to an endpoint type, erasing the handler’s arg types.
- body_
from_ stream - Create a streaming
BoxBodyfrom aStreamofResult<Frame<Bytes>, E>. - empty_
body - Create an empty
BoxBody. - serve
- Convenience function to create and serve an API.
- sse_
body - Create an SSE (Server-Sent Events) body from a stream of event strings.
Type Aliases§
- Delete
Endpoint DELETEendpoint with no request body.- GetEndpoint
GETendpoint with no request body.- Patch
Endpoint PATCHendpoint with a request body.- Post
Endpoint POSTendpoint with a request body.- PutEndpoint
PUTendpoint with a request body.
Attribute Macros§
- api_
description - Defines an API as a trait, generating endpoint types and a
Servesbridge. - documented_
handler - Extracts doc comments from a handler function and generates a companion
constof typeHandlerDoc(fromtypeway_core) containing the summary, description, operation ID, and tags. - handler
- Validates a handler function at its definition site.
Derive Macros§
- ToProto
Type - Derives a
ToProtoTypeimplementation for a struct with named fields. - Typestate
Builder - Derive a typestate builder for a struct.
- Typeway
Codec - Derive
TypewayEncodeandTypewayDecodefor a struct. - Typeway
Schema - Derives a
ToSchemaimplementation for a struct with named fields.