From 2bdc60266adc1920a31488309657dd8db13b33f0 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Tue, 14 Feb 2023 00:00:47 +0100 Subject: refactor: Get TS package API response from backend (#168) * refactor: Get TS package API response from backend Previously Thunderstore package index was done in frontend. Should be moved to backend instead as backend is reponsible for such tasks while frontend should just be used to store and display information. * refactor: Filter TS API response in backend * refactor: Rename function Makes it more descriptive what it does * refactor: Use gen. binds instead of duped struct Replaces the current TypeScript interface defintions with autogenerated bindings. * fix: Properly type variable * fix: Correct imported path of interface file * fix: Update struct field types to fix typing issue i32 should be big enough unless Thunderstore and Northstar suddenly becomes really huge and we start seeing over 4 million downloads on some mod * fix: Correct imported path of interface file --- src-tauri/src/thunderstore/mod.rs | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src-tauri/src/thunderstore/mod.rs (limited to 'src-tauri/src/thunderstore/mod.rs') diff --git a/src-tauri/src/thunderstore/mod.rs b/src-tauri/src/thunderstore/mod.rs new file mode 100644 index 00000000..c07c27ff --- /dev/null +++ b/src-tauri/src/thunderstore/mod.rs @@ -0,0 +1,78 @@ +//! For interacting with Thunderstore API +use app::constants::APP_USER_AGENT; +use serde::{Deserialize, Serialize}; +use std::collections::HashSet; +use ts_rs::TS; + +use crate::mod_management::BLACKLISTED_MODS; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, TS)] +#[ts(export)] +pub struct ThunderstoreMod { + pub name: String, + pub full_name: String, + pub owner: String, + pub package_url: String, + pub date_created: String, + pub date_updated: String, + pub uuid4: String, + pub rating_score: i32, + pub is_pinned: bool, + pub is_deprecated: bool, + pub has_nsfw_content: bool, + pub categories: Vec, + pub versions: Vec, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, TS)] +#[ts(export)] +pub struct ThunderstoreModVersion { + pub name: String, + pub full_name: String, + pub description: String, + pub icon: String, + pub version_number: String, + pub dependencies: Vec, + pub download_url: String, + pub downloads: i32, + pub date_created: String, + pub website_url: String, + pub is_active: bool, + pub uuid4: String, + pub file_size: i64, +} + +/// Queries Thunderstore packages API +#[tauri::command] +pub async fn query_thunderstore_packages_api() -> Result, String> { + println!("Fetching Thunderstore API"); + + // Fetches + let url = "https://northstar.thunderstore.io/api/v1/package/"; + + let client = reqwest::Client::new(); + let res = client + .get(url) + .header(reqwest::header::USER_AGENT, APP_USER_AGENT) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + + // Parse response + let parsed_json: Vec = match serde_json::from_str(&res) { + Ok(res) => res, + Err(err) => return Err(err.to_string()), + }; + + // Remove some mods from listing + let to_remove_set: HashSet<&str> = BLACKLISTED_MODS.iter().map(|s| s.as_ref()).collect(); + let filtered_packages = parsed_json + .into_iter() + .filter(|package| !to_remove_set.contains(&package.full_name.as_ref())) + .collect::>(); + + Ok(filtered_packages) +} -- cgit v1.2.3 From 52a1f9da0b0bc71b8db09f344b175c2752f98f34 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Tue, 14 Feb 2023 00:06:17 +0100 Subject: fix: Resolve variable import error --- src-tauri/src/thunderstore/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src-tauri/src/thunderstore/mod.rs') diff --git a/src-tauri/src/thunderstore/mod.rs b/src-tauri/src/thunderstore/mod.rs index c07c27ff..9151ba7c 100644 --- a/src-tauri/src/thunderstore/mod.rs +++ b/src-tauri/src/thunderstore/mod.rs @@ -1,11 +1,9 @@ //! For interacting with Thunderstore API -use app::constants::APP_USER_AGENT; +use app::constants::{APP_USER_AGENT, BLACKLISTED_MODS}; use serde::{Deserialize, Serialize}; use std::collections::HashSet; use ts_rs::TS; -use crate::mod_management::BLACKLISTED_MODS; - #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, TS)] #[ts(export)] pub struct ThunderstoreMod { -- cgit v1.2.3