Files
imphnen-backend-service/imphnen-utils/src/response_format.rs
T

63 lines
1.3 KiB
Rust
Raw Normal View History

//! Standardized response formatting utilities.
//!
//! This module provides consistent response formatting for API endpoints,
//! including success responses, error responses, and list responses with
//! configurable versioning from Cargo.toml.
2025-02-07 22:26:32 +07:00
use axum::{
Json,
http::StatusCode,
response::{IntoResponse, Response},
2025-02-07 22:26:32 +07:00
};
use serde::Serialize;
use serde_json::json;
use crate::{ResponseListSuccessDto, ResponseSuccessDto};
pub fn success_response<T: Serialize>(params: ResponseSuccessDto<T>) -> Response {
2025-02-16 01:09:55 +07:00
(
StatusCode::OK,
Json(json!({
"data": params.data,
"version": env!("CARGO_PKG_VERSION"),
2025-02-16 01:09:55 +07:00
})),
)
.into_response()
2025-02-07 22:26:32 +07:00
}
2025-02-16 01:09:55 +07:00
pub fn success_list_response<T: Serialize>(
params: ResponseListSuccessDto<T>,
) -> Response {
(
StatusCode::OK,
Json(json!({
"data": params.data,
"meta": params.meta,
"version": env!("CARGO_PKG_VERSION"),
2025-02-16 01:09:55 +07:00
})),
)
.into_response()
2025-02-07 22:26:32 +07:00
}
pub fn common_response(status: StatusCode, message: &str) -> Response {
2025-02-16 01:09:55 +07:00
(
status,
Json(json!({
"message": message,
"version": env!("CARGO_PKG_VERSION"),
2025-02-16 01:09:55 +07:00
})),
)
.into_response()
2025-02-07 22:26:32 +07:00
}
pub fn success_created_response<T: Serialize>(params: ResponseSuccessDto<T>) -> Response {
(
StatusCode::CREATED,
Json(json!({
"data": params.data,
"version": env!("CARGO_PKG_VERSION"),
})),
)
.into_response()
}