Skip to content

Commit 90f125b

Browse files
refactor: Use /projects/ endpoint instead of making multiple requests to the Modrinth API.
1 parent 73451f9 commit 90f125b

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/pages/events/mod-garden/nature.astro

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import ProjectCard from "../../../components/ProjectCard.astro";
55
66
import natureLogo from "../../../icons/events/nature/logo-full.png";
77
import type { Project } from "../../../ts/ModGardenAPI";
8-
import { getEventProjects } from "../../../ts/ModGardenAPI";
8+
import { getEventProjects, getModrinthModDataForEvent } from "../../../ts/ModGardenAPI";
99
import { formatTime, isoTime } from "../../../ts/TimeHelper";
10-
import { getModrinthProject, type Mod } from "../../../ts/ModrinthHelper";
10+
import { type Mod } from "../../../ts/ModrinthHelper";
1111
1212
type ProjectAndMod = {
1313
project: Project;
1414
mod: Mod;
1515
}
1616
1717
async function getProjects() : Promise<ProjectAndMod[]> {
18+
const modrinthProjects = await getModrinthModDataForEvent("mod-garden-nature");
1819
const unsortedProjects = await getEventProjects("mod-garden-nature");
1920
const projects = new Array<ProjectAndMod>(unsortedProjects.length);
2021
for (let i = 0; i < unsortedProjects.length; ++i) {
2122
const project = unsortedProjects[i];
22-
const mod = await getModrinthProject(project.modrinth_id);
23+
const mod = modrinthProjects.find(mod => mod.id == project.modrinth_id);
2324
projects[i] = { project: project, mod: mod } as ProjectAndMod;
2425
}
2526
projects.sort((a, b) => a.mod.title.localeCompare(b.mod.title))

src/ts/ModGardenAPI.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getModrinthProject, type Mod } from "./ModrinthHelper.ts";
1+
import { getModrinthProject, getModrinthProjects, type Mod } from "./ModrinthHelper.ts";
22

33
export type MinecraftAccount = {
44
uuid: string;
@@ -174,8 +174,14 @@ export async function getUserAwards(user: string): Promise<Award[]> {
174174
.then((data) => data as Award[]);
175175
}
176176

177+
177178
export async function getModrinthModData(
178179
modrinth_id: string,
179180
): Promise<Mod | undefined> {
180181
return await getModrinthProject(modrinth_id);
181182
}
183+
184+
export async function getModrinthModDataForEvent(event: string): Promise<Mod[]> {
185+
const projects = await getEventProjects(event);
186+
return await getModrinthProjects(projects.map(project => project.modrinth_id));
187+
}

src/ts/ModrinthHelper.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import EleventyFetch from "@11ty/eleventy-fetch";
22
import { LIB_VERSION } from "./Version";
33
const MODRINTH_PROJECT_API = "https://api.modrinth.com/v2/project/";
4+
const MODRINTH_PROJECTS_API = "https://api.modrinth.com/v2/projects?ids=";
45
const MODRINTH_USER_API = "https://api.modrinth.com/v2/user/";
56
export interface GalleryImage {
67
url: string;
@@ -28,7 +29,7 @@ export interface File {
2829
}
2930

3031
export async function getModrinthProject(projectName: string): Promise<Mod> {
31-
let data = await EleventyFetch(`${MODRINTH_PROJECT_API}${projectName}`, {
32+
let data = await EleventyFetch(`${MODRINTH_PROJECT_API}[${projectName}]`, {
3233
duration: "1h",
3334
type: "json",
3435
fetchOptions: {
@@ -39,6 +40,20 @@ export async function getModrinthProject(projectName: string): Promise<Mod> {
3940
});
4041
return data as Mod;
4142
}
43+
44+
export async function getModrinthProjects(projectNames: string[]): Promise<Mod[]> {
45+
let data = await EleventyFetch(`${MODRINTH_PROJECTS_API}[${projectNames.map(projectName => "\"" + projectName + "\"").join(",")}]`, {
46+
duration: "1h",
47+
type: "json",
48+
fetchOptions: {
49+
headers: {
50+
"User-Agent": `ModGardenEvent/website/${LIB_VERSION} (modgarden.net)`,
51+
},
52+
},
53+
});
54+
return data as Mod[];
55+
}
56+
4257
export async function getModrinthUser(username: string): Promise<ModrinthUser> {
4358
let data = await EleventyFetch(`${MODRINTH_USER_API}${username}`, {
4459
duration: "1h",

0 commit comments

Comments
 (0)