Skip to content
File

Blob: src/worker/routes/ui/refsApi.ts

typescript35 lines
1import type { Ref } from "@/worker/git";
2import { classifyRef, formatRefOption } from "@/shared/git/ref-display";
3import { isRequestPrivate, loadHeadAndRefsCached, resolveUiRepoAccess } from "./helpers";
4import type { AppContext } from "../hono";
5 
6export async function handleRefsApi(c: AppContext<"/:owner/:repo/api/refs">) {
7 const env = c.env;
8 const owner = c.req.param("owner");
9 const repo = c.req.param("repo");
10 const access = await resolveUiRepoAccess(c, owner, repo, { responseShape: "json" });
11 if (access.kind === "response") return access.response;
12 const { route, cacheCtx } = access;
13 try {
14 const refsData = await loadHeadAndRefsCached(env, cacheCtx, route.doName);
15 const refs: Ref[] = refsData?.refs || [];
16 const branches = refs.filter((ref) => classifyRef(ref.name) === "branch").map(formatRefOption);
17 const tags = refs.filter((ref) => classifyRef(ref.name) === "tag").map(formatRefOption);
18 const isPrivate = isRequestPrivate(cacheCtx);
19 return new Response(JSON.stringify({ branches, tags }), {
20 headers: {
21 "Content-Type": "application/json",
22 "Cache-Control": isPrivate ? "no-store" : "public, max-age=60",
23 },
24 });
25 } catch (e) {
26 return new Response(
27 JSON.stringify({ branches: [], tags: [], error: e instanceof Error ? e.message : String(e) }),
28 {
29 status: 500,
30 headers: { "Content-Type": "application/json" },
31 }
32 );
33 }
34}