diff --git a/api/_github-release.js b/api/_github-release.js new file mode 100644 index 000000000..0b39d3bc2 --- /dev/null +++ b/api/_github-release.js @@ -0,0 +1,12 @@ +const RELEASES_URL = 'https://api.github.com/repos/koala73/worldmonitor/releases/latest'; + +export async function fetchLatestRelease(userAgent) { + const res = await fetch(RELEASES_URL, { + headers: { + 'Accept': 'application/vnd.github+json', + 'User-Agent': userAgent, + }, + }); + if (!res.ok) return null; + return res.json(); +} diff --git a/api/download.js b/api/download.js index 31dd17176..7c35ed0a4 100644 --- a/api/download.js +++ b/api/download.js @@ -1,7 +1,8 @@ +import { fetchLatestRelease } from './_github-release.js'; + // Non-sebuf: returns XML/HTML, stays as standalone Vercel function export const config = { runtime: 'edge' }; -const RELEASES_URL = 'https://api.github.com/repos/koala73/worldmonitor/releases/latest'; const RELEASES_PAGE = 'https://github.com/koala73/worldmonitor/releases/latest'; const PLATFORM_PATTERNS = { @@ -48,18 +49,10 @@ export default async function handler(req) { } try { - const res = await fetch(RELEASES_URL, { - headers: { - 'Accept': 'application/vnd.github+json', - 'User-Agent': 'WorldMonitor-Download-Redirect', - }, - }); - - if (!res.ok) { + const release = await fetchLatestRelease('WorldMonitor-Download-Redirect'); + if (!release) { return Response.redirect(RELEASES_PAGE, 302); } - - const release = await res.json(); const matcher = PLATFORM_PATTERNS[platform]; const assets = Array.isArray(release.assets) ? release.assets : []; const asset = variant diff --git a/api/version.js b/api/version.js index 2dec8b1f5..b16f40fa8 100644 --- a/api/version.js +++ b/api/version.js @@ -1,25 +1,17 @@ +import { fetchLatestRelease } from './_github-release.js'; + // Non-sebuf: returns XML/HTML, stays as standalone Vercel function export const config = { runtime: 'edge' }; -const RELEASES_URL = 'https://api.github.com/repos/koala73/worldmonitor/releases/latest'; - export default async function handler() { try { - const res = await fetch(RELEASES_URL, { - headers: { - 'Accept': 'application/vnd.github+json', - 'User-Agent': 'WorldMonitor-Version-Check', - }, - }); - - if (!res.ok) { + const release = await fetchLatestRelease('WorldMonitor-Version-Check'); + if (!release) { return new Response(JSON.stringify({ error: 'upstream' }), { status: 502, headers: { 'Content-Type': 'application/json' }, }); } - - const release = await res.json(); const tag = release.tag_name ?? ''; const version = tag.replace(/^v/, '');