tin-actix-api-resp

Crates.iotin-actix-api-resp
lib.rstin-actix-api-resp
version0.1.2
created_at2025-10-13 00:25:24.678352+00
updated_at2025-10-13 00:25:24.678352+00
descriptionA library for handling API responses in Actix-web applications
homepagehttps://github.com/JiabinTang/tin-actix-api-resp
repositoryhttps://github.com/JiabinTang/tin-actix-api-resp
max_upload_size
id1879864
size100,284
唐加彬 (JiabinTang)

documentation

README

tin-actix-api-resp

基于 Actix-Web 的统一 API 响应封装库

简介

tin-actix-api-resp 提供了统一的 API 响应结构体 ApiRes<T>,支持标准 HTTP 状态码、业务自定义 code、消息 message 及数据 data,便于前后端协作和接口规范化。

  • 支持所有常见 HTTP 状态码的响应类型
  • 兼容 actix-web 的 Responder/ResponseError
  • 响应体自动序列化为 JSON
  • 便于自定义 message/code/data

安装

在你的 Cargo.toml 中添加依赖:

[dependencies]
tin-actix-api-resp = { git = "https://gitee.com/J_Tang/tin-actix-api-resp.git" }

快速开始

use tin_actix_api_resp::{ApiRes, ApiResult};
use actix_web::{get, web, App, HttpServer, Responder};

#[get("/hello")]
async fn hello() -> ApiResult<String> {
    Ok(ApiRes::Ok("Hello, world!".to_string()))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(hello))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

响应结构

成功响应:

{
  "code": 200,
  "message": "Success",
  "data": "Hello, world!"
}

错误响应:

{
  "code": 404,
  "message": "Not Found"
}

API 说明

  • ApiRes<T>:统一响应类型,支持 Ok/OkWith/Custom 及所有常见 HTTP 错误类型
  • ApiResult<T>:类型别名,等价于 Result<ApiRes<T>, ApiErr>
  • ApiErr:类型别名,等价于 ApiRes<()>

常用构造方法

ApiRes::Ok(data) // 200 成功
ApiRes::OkWith(201, "Created".into(), data) // 200+自定义 code/message
ApiRes::NotFound("资源不存在".into()) // 404
ApiRes::Custom(400, 10001, "参数错误".into(), None) // 自定义

License

MIT

Commit count: 0

cargo fmt