Seamless
An opinionated library to easily plug RPC style JSON APIs into your existing HTTP framework to enable type safe communication with TypeScript (or similar) clients.
The main USP of this library is that it takes advantage of trait and macro magic to automatically infer the shape of the API (paths, descriptions, and the types of request and response for each route) from just the Rust code you've written, negating the need for external definitions like OpenAPI.
Pros & Cons
Seamless is a library primarily designed to facilitate communication between a Rust backend and a TypeScript (or similar) client via JSON. By using this library you get:
- The ablity to use any async framework of your choice without feature flags and such.
- A self describing API that can automatically provide back enough information to generate
a fully typed client in a language like TypeScript. This leans on a [
macro@ApiBody] macro which is placed on structs/enums you'd like to receive or return from the API, along with trait magic. - Consistent error handling: You can return whatever domain specific errors you like from handlers,
so long as they implement
Into<ApiError>. The provided [macro@ApiError] macro makes this simple. - The ability to pull in state or guard requests using the [
handler::HandlerParam] trait. With this trait, handlers can ask for whatever parameters they need, and know that they won't run if those parameters cannot be obtained (for example, an invalid user session was provided).
This library also has limitations, some of them being:
- Streaming of request and response bodies is not supported. Currently the library doesn't expose
means to stream data in and our of handlers for the sake of simplicity (instead, everything comes in
and leaves as a
Vec<u8>). This is simple to use, but large data transfers should happen outside of this library. - Type information from the [
Api::info()] method is tuned towards generating TypeScript client code, and cannot provide enough detail to generate, for example, a well typed Rust client. - No support for more complex URL matching (eg to extract query params). I don't intend to support this use case. Keeping parameters in the body allows us to type them properly; this would be much more difficult to do with query params. Think of this library as more RPC, less REST.
Example
Below is a basic self contained example of using this library. Please have a look in the examples
folder for more detailed examples.
# new.unwrap.block_on;
State
Most real life use cases will require some sort of state to be accessible inside a handler.
This library follows an approach a little similar to Rocket. Any type that implements the
[handler::HandlerParam] trait can be passed into handler functions. To pass state in, you can
inject it into the http::Request prior to handing it to this library, and then extract it out
of the request again in the [handler::HandlerParam] implementation.
Note: params implementing the RequestParam trait must come before the one that implements
RequestBody (if any) in the handler function argument list.
Here's an example:
use ;
#
#
#
#
#
# async
# new.unwrap.block_on