diff options
author | GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> | 2023-02-14 00:00:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 00:00:47 +0100 |
commit | 2bdc60266adc1920a31488309657dd8db13b33f0 (patch) | |
tree | 9c8c73cdcc4fa97a0688af8f192a1e871f2a8c8d | |
parent | cb638c556594a996c94f501fcbd009984a63fd55 (diff) | |
download | FlightCore-2bdc60266adc1920a31488309657dd8db13b33f0.tar.gz FlightCore-2bdc60266adc1920a31488309657dd8db13b33f0.zip |
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
-rw-r--r-- | src-tauri/bindings/ThunderstoreMod.ts | 4 | ||||
-rw-r--r-- | src-tauri/bindings/ThunderstoreModVersion.ts | 3 | ||||
-rw-r--r-- | src-tauri/src/main.rs | 4 | ||||
-rw-r--r-- | src-tauri/src/thunderstore/mod.rs | 78 | ||||
-rw-r--r-- | src-vue/src/components/ThunderstoreModCard.vue | 4 | ||||
-rw-r--r-- | src-vue/src/plugins/store.ts | 22 | ||||
-rw-r--r-- | src-vue/src/utils/thunderstore/ThunderstoreMod.d.ts | 12 | ||||
-rw-r--r-- | src-vue/src/utils/thunderstore/ThunderstoreModVersion.d.ts | 9 | ||||
-rw-r--r-- | src-vue/src/views/mods/ThunderstoreModsView.vue | 4 |
9 files changed, 108 insertions, 32 deletions
diff --git a/src-tauri/bindings/ThunderstoreMod.ts b/src-tauri/bindings/ThunderstoreMod.ts new file mode 100644 index 00000000..25d119cb --- /dev/null +++ b/src-tauri/bindings/ThunderstoreMod.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { ThunderstoreModVersion } from "./ThunderstoreModVersion"; + +export interface ThunderstoreMod { name: string, full_name: string, owner: string, package_url: string, date_created: string, date_updated: string, uuid4: string, rating_score: number, is_pinned: boolean, is_deprecated: boolean, has_nsfw_content: boolean, categories: Array<string>, versions: Array<ThunderstoreModVersion>, }
\ No newline at end of file diff --git a/src-tauri/bindings/ThunderstoreModVersion.ts b/src-tauri/bindings/ThunderstoreModVersion.ts new file mode 100644 index 00000000..7e76308e --- /dev/null +++ b/src-tauri/bindings/ThunderstoreModVersion.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface ThunderstoreModVersion { name: string, full_name: string, description: string, icon: string, version_number: string, dependencies: Array<string>, download_url: string, downloads: number, date_created: string, website_url: string, is_active: boolean, uuid4: string, file_size: bigint, }
\ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 63b30e9e..12fa477a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -33,6 +33,9 @@ use mod_management::{ mod northstar; use northstar::get_northstar_version_number; +mod thunderstore; +use thunderstore::query_thunderstore_packages_api; + use tauri::Manager; use tauri_plugin_store::PluginBuilder; use tokio::time::sleep; @@ -111,6 +114,7 @@ fn main() { delete_northstar_mod, get_server_player_count, delete_thunderstore_mod, + query_thunderstore_packages_api, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); 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<String>, + pub versions: Vec<ThunderstoreModVersion>, +} + +#[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<String>, + 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<Vec<ThunderstoreMod>, 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<ThunderstoreMod> = 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::<Vec<ThunderstoreMod>>(); + + Ok(filtered_packages) +} diff --git a/src-vue/src/components/ThunderstoreModCard.vue b/src-vue/src/components/ThunderstoreModCard.vue index d125e9f5..c9f6768c 100644 --- a/src-vue/src/components/ThunderstoreModCard.vue +++ b/src-vue/src/components/ThunderstoreModCard.vue @@ -66,8 +66,8 @@ <script lang="ts"> import {defineComponent} from "vue"; -import {ThunderstoreMod} from "../utils/thunderstore/ThunderstoreMod"; -import {ThunderstoreModVersion} from "../utils/thunderstore/ThunderstoreModVersion"; +import {ThunderstoreMod} from "../../../src-tauri/bindings/ThunderstoreMod"; +import {ThunderstoreModVersion} from "../../../src-tauri/bindings/ThunderstoreModVersion"; import {invoke, shell} from "@tauri-apps/api"; import {ThunderstoreModStatus} from "../utils/thunderstore/ThunderstoreModStatus"; import {NorthstarMod} from "../../../src-tauri/bindings/NorthstarMod"; diff --git a/src-vue/src/plugins/store.ts b/src-vue/src/plugins/store.ts index d4371fb8..e44b1c3f 100644 --- a/src-vue/src/plugins/store.ts +++ b/src-vue/src/plugins/store.ts @@ -13,7 +13,7 @@ import { open } from '@tauri-apps/api/dialog'; import { Store } from 'tauri-plugin-store-api'; import { router } from "../main"; import { ReleaseInfo } from "../../../src-tauri/bindings/ReleaseInfo"; -import { ThunderstoreMod } from '../utils/thunderstore/ThunderstoreMod'; +import { ThunderstoreMod } from "../../../src-tauri/bindings/ThunderstoreMod"; import { NorthstarMod } from "../../../src-tauri/bindings/NorthstarMod"; import { searchModule } from './modules/search'; @@ -242,14 +242,22 @@ export const store = createStore<FlightCoreStore>({ await store.commit('loadInstalledMods'); if (state.thunderstoreMods.length !== 0) return; - const response = await fetch('https://northstar.thunderstore.io/api/v1/package/'); - let mods = JSON.parse(await (await response.blob()).text()); + let mods: ThunderstoreMod[] = []; + await invoke<ThunderstoreMod[]>("query_thunderstore_packages_api") + .then((message) => { + mods = message; + }) + .catch((error) => { + console.error(error); + return; + }); + + if (mods == undefined) { + return; + } // Remove some mods from listing - const removedMods = ['Northstar', 'NorthstarReleaseCandidate', 'r2modman']; - state.thunderstoreMods = mods.filter((mod: ThunderstoreMod) => { - return !removedMods.includes(mod.name) && !mod.is_deprecated; - }); + state.thunderstoreMods = mods; // Retrieve categories from mods state.thunderstoreModsCategories = mods diff --git a/src-vue/src/utils/thunderstore/ThunderstoreMod.d.ts b/src-vue/src/utils/thunderstore/ThunderstoreMod.d.ts deleted file mode 100644 index 6387c47e..00000000 --- a/src-vue/src/utils/thunderstore/ThunderstoreMod.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { ThunderstoreModVersion } from "./ThunderstoreModVersion"; - -export interface ThunderstoreMod { - name: string; - owner: string; - date_updated: string; - rating_score: number; - package_url: string; - is_deprecated: boolean; - versions: ThunderstoreModVersion[]; - categories: string[]; -} diff --git a/src-vue/src/utils/thunderstore/ThunderstoreModVersion.d.ts b/src-vue/src/utils/thunderstore/ThunderstoreModVersion.d.ts deleted file mode 100644 index f53f0362..00000000 --- a/src-vue/src/utils/thunderstore/ThunderstoreModVersion.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -export interface ThunderstoreModVersion { - full_name: string; - description: string; - icon: string; - version_number: string; - download_url: string; - downloads: number; - date_created: string; -} diff --git a/src-vue/src/views/mods/ThunderstoreModsView.vue b/src-vue/src/views/mods/ThunderstoreModsView.vue index aaf15220..19809f3e 100644 --- a/src-vue/src/views/mods/ThunderstoreModsView.vue +++ b/src-vue/src/views/mods/ThunderstoreModsView.vue @@ -46,11 +46,11 @@ <script lang="ts">
import { defineComponent, ref } from 'vue';
-import { ThunderstoreMod } from "../../utils/thunderstore/ThunderstoreMod";
+import { ThunderstoreMod } from "../../../../src-tauri/bindings/ThunderstoreMod";
import ThunderstoreModCard from "../../components/ThunderstoreModCard.vue";
import { ElScrollbar, ScrollbarInstance } from "element-plus";
import { SortOptions } from "../../utils/SortOptions.d";
-import { ThunderstoreModVersion } from '../../utils/thunderstore/ThunderstoreModVersion';
+import { ThunderstoreModVersion } from "../../../../src-tauri/bindings/ThunderstoreModVersion";
export default defineComponent({
name: "ThunderstoreModsView",
|