Expand description
§JDER axum
A response builder for axum.
This package includes different axum response builders, extractors and layers based on the JSON response structure specified in JSON Data Error Response (JDER). With the builders and extractors provided, various kinds of responses can be created easily instead of sending plain text responses.
§Usage
To create a JSON response, use
CreateJsonResponse
:
use jder_axum::response::{
Response,
json::CreateJsonResponse
};
use serde::Serialize;
#[derive(Serialize)]
struct RouteResponseData {
title: String,
}
async fn route() -> Response {
CreateJsonResponse::success::<RouteResponseData>()
.data(RouteResponseData {
title: "Title".to_string(),
})
.send()
}
If no data is needed, use
dataless
function instead:
use jder_axum::response::{
Response,
json::CreateJsonResponse
};
async fn route() -> Response {
CreateJsonResponse::dataless().send()
}
For returning content other than JSON, use
CreateResponse
:
use axum::http::header;
use jder_axum::response::{
Response,
CreateResponse
};
use serde::Serialize;
async fn route() -> Response {
CreateResponse::success()
.header(header::CONTENT_TYPE, "text/plain")
.body("hi")
}