Skip to main content

karbon_framework/http/
response.rs

1use axum::http::StatusCode;
2use axum::response::IntoResponse;
3use serde::Serialize;
4
5/// Helper for common JSON responses
6pub struct JsonResponse;
7
8impl JsonResponse {
9    /// 200 OK with data
10    pub fn ok<T: Serialize>(data: T) -> impl IntoResponse {
11        (StatusCode::OK, axum::Json(data))
12    }
13
14    /// 201 Created with data
15    pub fn created<T: Serialize>(data: T) -> impl IntoResponse {
16        (StatusCode::CREATED, axum::Json(data))
17    }
18
19    /// 204 No Content (for deletions)
20    pub fn no_content() -> impl IntoResponse {
21        StatusCode::NO_CONTENT
22    }
23
24    /// Simple message response
25    pub fn message(status: StatusCode, msg: &str) -> impl IntoResponse {
26        (
27            status,
28            axum::Json(serde_json::json!({ "message": msg })),
29        )
30    }
31
32    /// Success message shorthand
33    pub fn success(msg: &str) -> impl IntoResponse {
34        Self::message(StatusCode::OK, msg)
35    }
36}