Kits
Pavex provides a rich set of first-party constructors.
To leverage them, you must register them with your application's Blueprint
: after a while,
it gets tedious.
To make your life easier, Pavex provides kits: collections of commonly used constructors,
organized by application type.
ApiKit
ApiKit
is a good starting point for most APIs and it's installed by default in projects created with
pavex new
.
use pavex::blueprint::Blueprint;
use pavex::kit::ApiKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
ApiKit::new().register(&mut bp);
// [...]
}
It registers constructors for:
Customization
Using a kit it's not an all-or-nothing deal: you can cherry-pick the constructors you need and even customize them.
Skip a constructor
If you don't need a particular constructor, you can skip its registration by setting the corresponding
kit field to None
:
use pavex::blueprint::Blueprint;
use pavex::kit::ApiKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
let mut kit = ApiKit::new();
// The constructor for `PathParams` will not be registered.
kit.path_params = None;
kit.register(&mut bp);
// [...]
}
Tweak a constructor
In other cases, you may want to include a constructor, but the default configuration doesn't fit your requirements.
For example, you might want to change the cloning behavior or the associated error handler.
use pavex::blueprint::constructor::CloningStrategy;
use pavex::blueprint::Blueprint;
use pavex::kit::ApiKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
let mut kit = ApiKit::new();
kit.buffered_body = kit
.buffered_body
.map(|b| b.cloning(CloningStrategy::CloneIfNecessary));
kit.register(&mut bp);
// [...]
}
Replace a constructor
You can also replace one of the constructors provided by the kit with a custom one.
use pavex::blueprint::constructor::Constructor;
use pavex::blueprint::Blueprint;
use pavex::f;
use pavex::kit::ApiKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
let mut kit = ApiKit::new();
let c = Constructor::request_scoped(f!(crate::custom_path_params)); // (1)!
kit.path_params = Some(c);
kit.register(&mut bp);
// [...]
}
- When working with a kit,
you configure the constructor without registering it directly with the blueprint.
The kit takes care of the registration for you when itsregister
method is invoked.