The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | const REPO = "pingdotgg/t3code"; |
| 2 | |
| 3 | export const RELEASES_URL = `https://github.com/${REPO}/releases`; |
| 4 | export const NIGHTLY_RELEASES_URL = `${RELEASES_URL}?q=nightly&expanded=true`; |
| 5 | |
| 6 | const 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. |
| 8 | const LIST_API_URL = `https://api.github.com/repos/${REPO}/releases?per_page=10`; |
| 9 | |
| 10 | export type ReleaseChannel = "stable" | "nightly"; |
| 11 | |
| 12 | export interface ReleaseAsset { |
| 13 | name: string; |
| 14 | browser_download_url: string; |
| 15 | } |
| 16 | |
| 17 | export interface Release { |
| 18 | tag_name: string; |
| 19 | html_url: string; |
| 20 | published_at: string; |
| 21 | assets: ReleaseAsset[]; |
| 22 | } |
| 23 | |
| 24 | function cacheKey(channel: ReleaseChannel) { |
| 25 | return `t3code-${channel}-release`; |
| 26 | } |
| 27 | |
| 28 | async 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 | |
| 34 | async function fetchStable(): Promise<Release> { |
| 35 | return fetchJson(LATEST_API_URL); |
| 36 | } |
| 37 | |
| 38 | async 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 | |
| 47 | export 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 | } |