{"id":24057308,"url":"https://github.com/devashishdxt/htmxtools","last_synced_at":"2025-05-12T14:25:36.801Z","repository":{"id":270684806,"uuid":"911074881","full_name":"devashishdxt/htmxtools","owner":"devashishdxt","description":"A lightweight Rust crate for working with HTMX headers, specifically designed to integrate seamlessly with axum.","archived":false,"fork":false,"pushed_at":"2025-02-02T01:23:26.000Z","size":90,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-12T14:25:22.719Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devashishdxt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-02T07:25:05.000Z","updated_at":"2025-04-22T04:03:29.000Z","dependencies_parsed_at":"2025-01-02T12:34:30.417Z","dependency_job_id":"6cc6a077-509c-43a7-b968-eaf5b9cab1c8","html_url":"https://github.com/devashishdxt/htmxtools","commit_stats":null,"previous_names":["devashishdxt/htmxtools","devashishdxt/htmx-headers"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devashishdxt%2Fhtmxtools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devashishdxt%2Fhtmxtools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devashishdxt%2Fhtmxtools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devashishdxt%2Fhtmxtools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devashishdxt","download_url":"https://codeload.github.com/devashishdxt/htmxtools/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253754390,"owners_count":21958855,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-09T05:41:33.852Z","updated_at":"2025-05-12T14:25:36.771Z","avatar_url":"https://github.com/devashishdxt.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# htmxtools\n\nA lightweight Rust crate for working with HTMX headers, specifically designed to integrate seamlessly with `axum`.\n\nHTMX is a library that enhances HTML with AJAX capabilities, and it relies heavily on custom headers to enable\ndynamic interactions. The `htmxtools` crate simplifies handling these headers in Rust, enabling you to easily\nextract, process, and respond with HTMX-related data.\n\n## Features\n\n- **Request Extractors**: Easily extract HTMX headers from incoming requests.\n- **Response Builders**: Conveniently build responses with HTMX headers.\n- **Axum Integration**: Built on top of `typed-headers` in `axum-extra` for seamless integration with the `axum`.\n- **Auto Vary**: Correctly handle response caching by automatically add the `Vary` header to responses based on the\n  extracted HTMX headers.\n\n## Usage\n\nTo use `htmxtools`, run the following command in your project directory:\n\n```bash\ncargo add htmxtools\n```\n\nIf you don't want to use `axum`, you can still use the `htmxtools` crate with other web frameworks. The crate\nimplements `headers::Header` trait for all HTMX headers, so you can easily extract and build headers in any\nframework that supports `headers::Header`.\n\nTo use `htmxtools` without `axum`, run the following command in your project directory:\n\n```bash\ncargo add htmxtools --no-default-features\n```\n\n### Request Extractors\n\nTo extract HTMX headers from incoming requests in `axum`, you can directly use headers in `htmxtools::request` in your\nhandler functions as they implement `FromRequestParts` and `OptionalFromRequestParts` traits.\n\nHere's an example of extracting the `hx-request` header:\n\n```rust\nuse axum_core::response::IntoResponse;\nuse htmxtools::request::HxRequest;\n\nasync fn handler(hx_request: Option\u003cHxRequest\u003e) -\u003e impl IntoResponse {\n    if hx_request.is_some() {\n        \"This is an HTMX request\"\n    } else {\n        \"This is not an HTMX request\"\n    }\n}\n```\n\nHere's another example of extracting the `hx-target` header:\n\n```rust\nuse axum_core::response::IntoResponse;\nuse htmxtools::request::HxTarget;\n\nasync fn handler(hx_target: HxTarget) -\u003e impl IntoResponse {\n    format!(\"The target is: {}\", hx_target.as_str())\n}\n\nasync fn another_handler(hx_target: Option\u003cHxTarget\u003e) -\u003e impl IntoResponse {\n    match hx_target {\n        Some(target) =\u003e format!(\"The target is: {}\", target.as_str()),\n        None =\u003e \"No target specified\".to_string(),\n    }\n}\n```\n\n### Response Builders\n\nTo build responses with HTMX headers in `axum`, you can use headers in `htmxtools::response` in your handler functions\nas they implement `IntoResponseParts` and `IntoResponse` traits.\n\nHere's an example of building a response with the `hx-push-url` header:\n\n```rust\nuse axum_core::response::IntoResponse;\nuse htmxtools::response::HxPushUrl;\nuse http::Uri;\n\nasync fn handler() -\u003e impl IntoResponse {\n    HxPushUrl::url(Uri::from_static(\"/new-url\"))\n}\n```\n\nHere's another example of building a response with `hx-retarget` and `hx-reswap` header:\n\n```rust\nuse axum_core::response::IntoResponse;\nuse htmxtools::response::{HxReswap, HxRetarget};\n\nasync fn handler() -\u003e impl IntoResponse {\n    (HxReswap::inner_html(), HxRetarget::from_static(\"#body\"), \"\u003cdiv\u003e\u003c/div\u003e\")\n}\n```\n\n### Auto Vary\n\nTo automatically add the `Vary` header to responses based on the extracted HTMX headers in `axum`, you can use the\n`AutoVaryLayer` in `htmxtools::auto_vary`.\n\nTo use the `AutoVaryLayer`, you need to enable the `auto-vary` feature in your `Cargo.toml`.\n\nHere's an example of using the `AutoVaryLayer`:\n\n```rust\nuse axum::Router;\nuse htmxtools::auto_vary::AutoVaryLayer;\n\nfn app() -\u003e Router {\n    Router::new().layer(AutoVaryLayer)\n}\n```\n\n## License\n\nLicensed under either of\n\n- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))\n- MIT license ([LICENSE-MIT](LICENSE-MIT))\n\nat your option.\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as\ndefined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevashishdxt%2Fhtmxtools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevashishdxt%2Fhtmxtools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevashishdxt%2Fhtmxtools/lists"}