import { TWY_API_URL } from 'astro:env/server'; import type { LatestBuildsResponse, PatchNote } from './types/api/builds'; import { ApiResult } from './types/api'; const BUILDS_V1 = "api/builds/v1"; export const api = { builds: { v1: { async getLatestVersions(): Promise> { try { const response = await fetch(`${TWY_API_URL}/${BUILDS_V1}/latest`, { headers: { "Content-Type": "application/json", }, cache: "no-store", }); if (!response.ok) { console.error("Failed to load latest versions.", { status: response.status, statusText: response.statusText, }); return ApiResult.failure("Failed to load latest versions."); } var obj = (await response.json()) as LatestBuildsResponse; return ApiResult.success(obj); } catch (error: unknown) { if (error instanceof Error) return ApiResult.failure(error.message); return ApiResult.failure("An unexpected error occured."); } }, async getPatchNotes(): Promise { try { const response = await fetch(`${TWY_API_URL}/${BUILDS_V1}/patchNotes`, { headers: { "Content-Type": "application/json", }, cache: "no-store", }); if (!response.ok) { console.error("Failed to load account.", { status: response.status, statusText: response.statusText, }); return []; } return (await response.json()) as PatchNote[]; } catch (error) { return []; } } } } }