This commit is contained in:
cirroskais 2024-03-03 21:22:51 -05:00
parent e0de38eb87
commit 4cd8f68e92
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
4 changed files with 92 additions and 67 deletions

View file

@ -1,3 +1,4 @@
export let cfModCache = new Map()
export let cfModIdCache = new Map()
export let cfModFilesCache = new Map()
export let cfModCache = new Map();
export let cfModIdCache = new Map();
export let cfModFilesCache = new Map();
export let cfModFileCache = new Map();

View file

@ -1,67 +1,81 @@
import { cfModCache, cfModFilesCache, cfModIdCache } from "./cache"
import { cfModCache, cfModFilesCache, cfModIdCache, cfModFileCache } from "./cache";
const BASE_URL = "https://api.curseforge.com"
const SEARCH_MODS = "/v1/mods/search"
const GET_FILES = "/v1/mods/{modId}/files"
const GET_MOD = "/v1/mods/{modId}"
const BASE_URL = "https://api.curseforge.com";
const SEARCH_MODS = "/v1/mods/search";
const GET_FILES = "/v1/mods/{modId}/files";
const GET_MOD = "/v1/mods/{modId}";
const GET_FILE = "/v1/mods/{modId}/files/{fileId}";
const GAME_ID = 432
const GAME_ID = 432;
export async function getMod(url, gameVersion = "1.18.2", modLoaderType = 1) {
url = url.split("/")
const slug = url[url.length - 1]
url = url.split("/");
const slug = url[url.length - 1];
if (cfModCache.get(slug)) return cfModCache.get(slug)
if (cfModCache.get(slug)) return cfModCache.get(slug);
const query = new URLSearchParams()
query.append("gameId", GAME_ID)
query.append("slug", slug)
query.append("pageSize", 1)
if (gameVersion) query.append("gameVersion", gameVersion)
if (modLoaderType) query.append("modLoaderType", modLoaderType)
const query = new URLSearchParams();
query.append("gameId", GAME_ID);
query.append("slug", slug);
query.append("pageSize", 1);
if (gameVersion) query.append("gameVersion", gameVersion);
if (modLoaderType) query.append("modLoaderType", modLoaderType);
const response = await fetch(BASE_URL + SEARCH_MODS + "?" + query.toString(), {
headers: {
"x-api-key": import.meta.env.CURSEFORGE_API,
},
}).catch((e) => console.log(e))
}).catch((e) => console.log(e));
const data = (await response.json()).data[0]
cfModCache.set(slug, data)
return data
const data = (await response.json()).data[0];
cfModCache.set(slug, data);
return data;
}
export async function getModFromId(modId) {
if (cfModIdCache.get(modId)) return cfModIdCache.get(modId)
if (cfModIdCache.get(modId)) return cfModIdCache.get(modId);
const response = await fetch(BASE_URL + GET_MOD.replace("{modId}", modId), {
headers: {
"x-api-key": import.meta.env.CURSEFORGE_API,
},
}).catch((e) => console.log(e))
}).catch((e) => console.log(e));
const data = await response.json()
cfModIdCache.set(modId, data)
const data = await response.json();
cfModIdCache.set(modId, data);
return data
return data;
}
export async function getModFiles(modId, gameVersion = "1.18.2", modLoaderType = 1) {
if (cfModFilesCache.get(modId)) return cfModFilesCache.get(modId)
if (cfModFilesCache.get(modId)) return cfModFilesCache.get(modId);
const query = new URLSearchParams()
if (gameVersion) query.append("gameVersion", gameVersion)
if (modLoaderType) query.append("modLoaderType", modLoaderType)
const query = new URLSearchParams();
if (gameVersion) query.append("gameVersion", gameVersion);
if (modLoaderType) query.append("modLoaderType", modLoaderType);
const response = await fetch(BASE_URL + GET_FILES.replace("{modId}", modId) + "?" + query.toString(), {
headers: {
"x-api-key": import.meta.env.CURSEFORGE_API,
},
}).catch((e) => console.log(e))
}).catch((e) => console.log(e));
const data = (await response.json()).data[0]
cfModFilesCache.set(modId, data)
return data
const data = (await response.json()).data[0];
cfModFilesCache.set(modId, data);
return data;
}
export async function downloadMod(id, fileId) {}
export async function downloadMod(modId, fileId) {
if (cfModFileCache.get(modId.toString() + fileId.toString()))
return cfModFileCache.get(modId.toString() + fileId.toString());
const response = await fetch(BASE_URL + GET_FILE.replace("{modId}", modId).replace("{fileId}", fileId), {
headers: {
"x-api-key": import.meta.env.CURSEFORGE_API,
},
}).catch((e) => console.log(e));
const data = (await response.json()).data;
cfModFileCache.set(modId.toString() + fileId.toString(), data);
return data;
}

8
src/routes/download.js Normal file
View file

@ -0,0 +1,8 @@
import { downloadMod } from "../lib/curseforge";
export default async function route(req) {
const querystring = new URL(req.url).searchParams;
const data = await downloadMod(querystring.get("modId"), querystring.get("fileId"));
return new Response(null, { status: 302, headers: { Location: data.downloadUrl } });
}

View file

@ -1,25 +1,27 @@
import getChanges from "./routes/getUpdates"
import getAllMods from "./routes/getAllMods"
import index from "./routes/index"
import getChanges from "./routes/getUpdates";
import getAllMods from "./routes/getAllMods";
import download from "./routes/download";
import index from "./routes/index";
const ROUTES = {
"/": index,
"/getallmods": getAllMods,
"/changes": getChanges,
}
"/download": download,
};
export default async function server() {
const http = Bun.serve({
fetch(req) {
const url = new URL(req.url)
const route = ROUTES[url.pathname]
if (route) return route(req)
throw new Error(404)
const url = new URL(req.url);
const route = ROUTES[url.pathname];
if (route) return route(req);
throw new Error(404);
},
error(error) {
return new Response(error)
return new Response(error);
},
})
});
console.log(`Listening on ${http.hostname}:${http.port}`)
console.log(`Listening on ${http.hostname}:${http.port}`);
}