3 releases (breaking)

0.3.0 Oct 26, 2024
0.2.0 Oct 14, 2024
0.1.0 Oct 4, 2024

#937 in Parser implementations

MIT license

64KB
1K SLoC

JDER Axum

A response builder for Axum.

This package includes several Axum response builders and different extractors 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.

Quick Start

Create a JSON response for an Axum route:

use jder_axum::response::{
    Response,
    json::CreateJsonResponse,
};

async fn route() -> Response {
    CreateJsonResponse::dataless().send()
}

And the response will be shown as below:

{
    "success": true,
    "data": null,
    "error": null
}

License

This project is MIT licensed, you can find the license file here.


lib.rs:

JDER Axum

A response builder for Axum.

This package includes several Axum response builders and different extractors 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")
}

Dependencies

~12–23MB
~404K SLoC