From 29002341e166c7ff59e8a08c9386b732284edc9f Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sat, 4 Apr 2026 22:19:49 +0200 Subject: [PATCH] Implement simple modrinth search #4 --- src/lib/saturn.ts | 59 ++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/lib/saturn.ts b/src/lib/saturn.ts index 58f28cb..47609dc 100644 --- a/src/lib/saturn.ts +++ b/src/lib/saturn.ts @@ -11,40 +11,35 @@ export async function toggleMod(modId: string): Promise { } export async function discoverMods(query: string): Promise { - const allDiscoverable: Mod[] = [ - { - id: "modmenu", - name: "Mod Menu", - version: "7.2.2", - author: "TerraformersMC", - description: "Adds a mod menu to view the list of installed mods.", - enabled: false, - supported_versions: ["1.20.1", "1.20.4", "1.21"], - }, - { - id: "starlight", - name: "Starlight", - version: "1.1.2", - author: "Spottedleaf", - description: "Rewrites the light engine to fix performance issues.", - enabled: false, - supported_versions: ["1.20.1"], - }, - { - id: "ferritecore", - name: "FerriteCore", - version: "6.0.1", - author: "malte0811", - description: "Memory usage optimizations.", - enabled: false, - supported_versions: ["1.20.1", "1.20.4", "1.21"], - }, - ]; + const url = new URL("https://api.modrinth.com/v2/search"); - if (!query) return allDiscoverable; + if (query) { + url.searchParams.set("query", query); + } - return allDiscoverable.filter((m) => - m.name.toLowerCase().includes(query.toLowerCase()), + // Optional but recommended filters + url.searchParams.set("facets", JSON.stringify([["project_type:mod"]])); + url.searchParams.set("limit", "20"); + + const res = await fetch(url.toString()); + + if (!res.ok) { + throw new Error("Failed to fetch mods from Modrinth"); + } + + const data = await res.json(); + + return data.hits.map( + (mod: any): Mod => ({ + id: mod.project_id, + name: mod.title, + version: "latest", + author: mod.author, + description: mod.description, + enabled: false, + icon: mod.icon_url, + supported_versions: mod.versions || [], + }), ); }