Open History to see the last commit
1const REPO = "pingdotgg/t3code";
2
3export const RELEASES_URL = `https://github.com/${REPO}/releases`;
4export const NIGHTLY_RELEASES_URL = `${RELEASES_URL}?q=nightly&expanded=true`;
5
6const LATEST_API_URL = `https://api.github.com/repos/${REPO}/releases/latest`;
7// GitHub's latest endpoint excludes prereleases; the newest nightly comes from the ordered release list.
8const LIST_API_URL = `https://api.github.com/repos/${REPO}/releases?per_page=10`;
9
10export type ReleaseChannel = "stable" | "nightly";
11
12export interface ReleaseAsset {
13 name: string;
14 browser_download_url: string;
15}
16
17export interface Release {
18 tag_name: string;
19 html_url: string;
20 published_at: string;
21 assets: ReleaseAsset[];
22}
23
24function cacheKey(channel: ReleaseChannel) {
25 return `t3code-${channel}-release`;
26}
27
28async function fetchJson<T>(url: string): Promise<T> {
29 const response = await fetch(url);
30 if (!response.ok) throw new Error(`GitHub API responded ${response.status}`);
31 return response.json();
32}
33
34async function fetchStable(): Promise<Release> {
35 return fetchJson(LATEST_API_URL);
36}
37
38async function fetchNightly(): Promise<Release> {
39 const list = await fetchJson<Release[]>(LIST_API_URL);
40 const nightly = Array.isArray(list)
41 ? list.find((release) => release.tag_name?.includes("-nightly."))
42 : undefined;
43 if (!nightly) throw new Error("No nightly release in the latest page");
44 return nightly;
45}
46
47export async function fetchLatestRelease(channel: ReleaseChannel = "stable"): Promise<Release> {
48 const key = cacheKey(channel);
49 const cached = sessionStorage.getItem(key);
50 if (cached) return JSON.parse(cached);
51
52 const data = channel === "nightly" ? await fetchNightly() : await fetchStable();
53
54 if (data?.assets) {
55 sessionStorage.setItem(key, JSON.stringify(data));
56 }
57
58 return data;
59}