返回 CodeWhale
published-release.ts
根目录 / web / lib / published-release.ts
1 import { fetchBoundedText } from "./bounded-body";
2 import type { PublishedReleaseFact } from "./facts";
3
4 const REPOSITORIES = {
5 terminal: "codewhale-hq/CodeWhale",
6 computerUse: "codewhale-hq/codewhale-cu-plugin",
7 } as const;
8
9 export async function fetchLatestPublishedRelease(
10 token?: string,
11 channel: keyof typeof REPOSITORIES = "terminal",
12 ): Promise<PublishedReleaseFact | null> {
13 if (process.env.NEXT_PHASE === "phase-production-build") return null;
14 const repo = REPOSITORIES[channel];
15 try {
16 const response = await fetchBoundedText(
17 `https://api.github.com/repos/${repo}/releases/latest`,
18 {
19 headers: {
20 Accept: "application/vnd.github+json",
21 "User-Agent": "codewhale-web",
22 "X-GitHub-Api-Version": "2022-11-28",
23 ...(token ? { Authorization: `Bearer ${token}` } : {}),
24 },
25 next: { revalidate: 300 },
26 },
27 { timeoutMs: 5000, maxBytes: 128 * 1024 },
28 );
29 if (!response.ok) return null;
30 const release = JSON.parse(response.text) as Record<string, unknown> | null;
31 if (!release || release.draft !== false || release.prerelease !== false
32 || typeof release.tag_name !== "string" || !/^v\d+\.\d+\.\d+$/.test(release.tag_name)
33 || typeof release.html_url !== "string"
34 || typeof release.published_at !== "string"
35 || !Number.isFinite(Date.parse(release.published_at))) return null;
36 const url = `https://github.com/${repo}/releases/tag/${release.tag_name}`;
37 if (release.html_url.toLowerCase() !== url.toLowerCase()) return null;
38 return {
39 tag: release.tag_name,
40 version: release.tag_name.slice(1),
41 publishedAt: release.published_at,
42 url,
43 };
44 } catch {
45 return null;
46 }
47 }
48
48 lines TYPESCRIPT