@@ -3,32 +3,36 @@ import type { Build, Project } from "@/utils/types";
33const API_ENDPOINT = "https://fill.papermc.io/v3" ;
44const 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
1523export 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 ) ;
0 commit comments