Skip to content

Commit ddb2049

Browse files
committed
feat: utilize edge-type fetching information for workers
1 parent 6c79e6d commit ddb2049

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
README.md
1+
README.md

bun.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"typescript": "^5.9.3"
3131
},
3232
"devDependencies": {
33+
"@cloudflare/workers-types": "^4.20251011.0",
3334
"@types/node": "^24.8.1",
3435
"@typescript-eslint/eslint-plugin": "^8.46.1",
3536
"@typescript-eslint/parser": "^8.46.1",

src/utils/fill.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@ import type { Build, Project } from "@/utils/types";
33
const API_ENDPOINT = "https://fill.papermc.io/v3";
44
const BSTATS_URL = "https://bstats.org/api/v1/plugins/580/charts/players/data/?maxElements=1";
55

6-
export async function getProject(project: string): Promise<Project> {
7-
const res = await fetch(`${API_ENDPOINT}/projects/${project}`);
6+
const edgeFetch = (url: string, ttlSeconds = 300): Promise<Response> => {
7+
return fetch(url, {
8+
cf: {
9+
cacheTtl: ttlSeconds,
10+
cacheEverything: true,
11+
},
12+
});
13+
};
814

15+
export async function getProject(project: string): Promise<Project> {
16+
const res = await edgeFetch(`${API_ENDPOINT}/projects/${project}`, 600);
917
if (!res.ok) {
1018
throw new Error(`getProject(${project}) failed: ${res.status}`);
1119
}
12-
return res.json();
20+
return res.json() as Promise<Project>;
1321
}
1422

1523
export async function getVersionBuilds(project: string, version: string): Promise<Build[]> {
16-
const res = await fetch(`${API_ENDPOINT}/projects/${project}/versions/${version}/builds`);
17-
24+
const res = await edgeFetch(`${API_ENDPOINT}/projects/${project}/versions/${version}/builds`, 300);
1825
if (!res.ok) {
1926
throw new Error(`getVersionBuilds(${project}, ${version}) failed: ${res.status}`);
2027
}
21-
return res.json();
28+
return res.json() as Promise<Build[]>;
2229
}
2330

24-
export async function getBStats(): Promise<{
25-
servers: number;
26-
players: number;
27-
}> {
31+
export async function getBStats(): Promise<{ servers: number; players: number }> {
2832
try {
29-
const response = await fetch(BSTATS_URL);
30-
const data = await response.json();
31-
const players = data[0]?.[1] || 0;
33+
const res = await edgeFetch(BSTATS_URL, 300);
34+
const data = (await res.json()) as Array<[number, number]> | null | undefined;
35+
const players = data?.[0]?.[1] ?? 0;
3236
return { servers: Math.round(players / 20), players };
3337
} catch (error) {
3438
console.error("Failed to fetch bStats:", error);

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"paths": {
77
"*": ["./*"],
88
"@/*": ["./src/*"]
9-
}
9+
},
10+
"types": ["@cloudflare/workers-types"]
1011
}
1112
}

0 commit comments

Comments
 (0)