Public
Code
HTTP
export const getVersion = async (request: Request) => {
const url = new URL(request.url);
const [username, repo] = url.pathname.split("/").filter(Boolean).map((s) => s.trim());
if (!username || !repo) {
throw new Error("Must provide /{username}/{repo}");
}
const targetUrl = `https://api.github.com/repos/${username}/${repo}/releases/latest`;
const headers = request.headers;
headers.set("accept", "application/vnd.github.v3+json");
const res = await fetch(targetUrl, { headers });
const release = await res.json();
if (!release || !release.tag_name) {
throw new Error(`No valid releases found for ${username}/${repo}`);
}
return Response.json({
username,
repo,
version: release.tag_name,
link: `https://github.com/${encodeURIComponent(username)}/${encodeURIComponent(repo)}`,
});
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
jpwilliams-getversion.web.val.run
Updated: November 13, 2023