/** * Example API route wiring for Firebase / Firestore. * * Install peers: `npm install firebase-admin repo-tracking` * Point the GitHub Action http mode at this endpoint. * * Security rules tip: * - Prefer writes only through this signed API (admin SDK bypasses rules) * - For public portfolio reads, either expose a GET that returns non-sensitive * fields, or allow public read on the collection with rules that never * store secrets in documents. */ import { initializeApp, applicationDefault, getApps } from "firebase-admin/app"; import { getFirestore } from "firebase-admin/firestore"; import { createTouchHandler } from "repo-tracking/server"; import { createFirestoreStore } from "repo-tracking/store/firestore"; if (!getApps().length) { initializeApp({ credential: applicationDefault() }); } const store = createFirestoreStore({ firestore: getFirestore(), collection: "repo-tracking", }); const handleTouch = createTouchHandler({ store, secret: process.env.REPO_TRACKING_SECRET || "", }); /** Next.js App Router style */ export async function POST(request: Request): Promise { const body = await request.text(); const result = await handleTouch({ method: request.method, body, headers: request.headers, }); return Response.json(result.body, { status: result.status }); } /** Optional public read for a single id */ export async function GET(request: Request): Promise { const id = new URL(request.url).searchParams.get("id"); if (!id) { return Response.json({ error: "id required" }, { status: 400 }); } const entry = await store.get(id); if (!entry) { return Response.json({ error: "not found" }, { status: 404 }); } return Response.json({ id, ...entry }, { headers: { "Cache-Control": "public, max-age=30, stale-while-revalidate=60", }, }); }