Skip to main content

modkit/api/
response.rs

1use axum::{
2    Json,
3    http::{StatusCode, Uri, header},
4    response::IntoResponse,
5};
6
7/// Short aliases for JSON responses
8pub type JsonBody<T> = Json<T>;
9pub type JsonPage<T> = Json<modkit_odata::Page<T>>;
10
11/// 200 OK + JSON
12pub fn ok_json<T: serde::Serialize>(value: T) -> impl IntoResponse {
13    (StatusCode::OK, Json(value))
14}
15
16/// 201 Created + JSON with Location header
17pub fn created_json<T: serde::Serialize>(
18    value: T,
19    uri: &Uri,
20    new_id: &str,
21) -> impl IntoResponse + use<T> {
22    let location = [uri.path().trim_end_matches('/'), new_id].join("/");
23    (
24        StatusCode::CREATED,
25        [(header::LOCATION, location)],
26        Json(value),
27    )
28}
29
30/// 204 No Content
31#[must_use]
32pub fn no_content() -> impl IntoResponse {
33    StatusCode::NO_CONTENT
34}