use axum::response::{IntoResponse, Response};
#[derive(Clone, Debug)]
pub struct PendingPage {
component: &'static str,
props: serde_json::Value,
}
impl PendingPage {
#[must_use]
pub fn new(component: &'static str, props: serde_json::Value) -> Self {
Self { component, props }
}
#[must_use]
pub fn component(&self) -> &'static str {
self.component
}
#[must_use]
pub fn props(&self) -> &serde_json::Value {
&self.props
}
#[must_use]
pub fn into_parts(self) -> (&'static str, serde_json::Value) {
(self.component, self.props)
}
#[must_use]
pub fn into_response(self) -> Response {
let mut response = not_installed(self.component);
response.extensions_mut().insert(self);
response
}
}
fn not_installed(component: &str) -> Response {
crate::api::Problem::of(crate::api::ProblemKind::Internal)
.with_detail(format!(
"The handler returned a page (`{component}`) but no Inertia layer \
is installed to render it. Add `.inertia(InertiaConfig::…)` to \
the application builder."
))
.into_response()
}
#[cfg(test)]
mod tests {
use super::*;
use axum::http::StatusCode;
#[test]
fn the_placeholder_carries_the_record() {
let pending = PendingPage::new("Home", serde_json::json!({"a": 1}));
let response = pending.into_response();
let record = response
.extensions()
.get::<PendingPage>()
.expect("the extension travels with the response");
assert_eq!(record.component(), "Home");
assert_eq!(record.props(), &serde_json::json!({"a": 1}));
}
#[test]
fn an_unrendered_page_is_a_loud_500_not_a_blank_200() {
let response = PendingPage::new("Home", serde_json::Value::Null).into_response();
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
}