From 129a4c75867c67a7c98ef817987842c910566811 Mon Sep 17 00:00:00 2001 From: spinline Date: Sun, 8 Feb 2026 22:43:35 +0300 Subject: [PATCH] refactor: move AddTorrentRequest to shared library for type safety --- backend/src/handlers/mod.rs | 13 ++----------- backend/src/main.rs | 4 ++-- frontend/src/components/torrent/add_torrent.rs | 6 ++---- shared/src/lib.rs | 6 ++++++ 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/backend/src/handlers/mod.rs b/backend/src/handlers/mod.rs index 2fca00d..c858a26 100644 --- a/backend/src/handlers/mod.rs +++ b/backend/src/handlers/mod.rs @@ -11,12 +11,10 @@ use axum::{ BoxError, }; use rust_embed::RustEmbed; -use serde::Deserialize; use shared::{ - GlobalLimitRequest, SetFilePriorityRequest, SetLabelRequest, TorrentActionRequest, TorrentFile, - TorrentPeer, TorrentTracker, + AddTorrentRequest, GlobalLimitRequest, SetFilePriorityRequest, SetLabelRequest, TorrentActionRequest, + TorrentFile, TorrentPeer, TorrentTracker, }; -use utoipa::ToSchema; pub mod auth; pub mod setup; @@ -25,13 +23,6 @@ pub mod setup; #[folder = "../frontend/dist"] pub struct Asset; -#[derive(Deserialize, ToSchema)] -pub struct AddTorrentRequest { - /// Magnet link or Torrent file URL - #[schema(example = "magnet:?xt=urn:btih:...")] - uri: String, -} - pub async fn static_handler(uri: Uri) -> impl IntoResponse { let mut path = uri.path().trim_start_matches('/').to_string(); diff --git a/backend/src/main.rs b/backend/src/main.rs index 6a3e0b4..bac6fea 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -125,7 +125,7 @@ struct Args { ), components( schemas( - handlers::AddTorrentRequest, + shared::AddTorrentRequest, shared::TorrentActionRequest, shared::Torrent, shared::TorrentStatus, @@ -172,7 +172,7 @@ struct ApiDoc; ), components( schemas( - handlers::AddTorrentRequest, + shared::AddTorrentRequest, shared::TorrentActionRequest, shared::Torrent, shared::TorrentStatus, diff --git a/frontend/src/components/torrent/add_torrent.rs b/frontend/src/components/torrent/add_torrent.rs index ac7b50e..ba6c8e6 100644 --- a/frontend/src/components/torrent/add_torrent.rs +++ b/frontend/src/components/torrent/add_torrent.rs @@ -1,7 +1,7 @@ use leptos::*; use leptos::html::Dialog; use crate::store::{show_toast_with_signal, TorrentStore}; -use shared::NotificationLevel; +use shared::{AddTorrentRequest, NotificationLevel}; #[component] @@ -36,9 +36,7 @@ pub fn AddTorrentModal( set_error_msg.set(None); spawn_local(async move { - let req_body = serde_json::json!({ - "uri": uri_val - }); + let req_body = AddTorrentRequest { uri: uri_val }; match gloo_net::http::Request::post("/api/torrents/add") .json(&req_body) diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 400f820..4dc6361 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -130,3 +130,9 @@ pub struct SetLabelRequest { pub hash: String, pub label: String, } + +#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)] +pub struct AddTorrentRequest { + #[schema(example = "magnet:?xt=urn:btih:...")] + pub uri: String, +}