All files / src/app/api/catalog/jsonld route.ts

85.71% Statements 12/14
75% Branches 6/8
100% Functions 1/1
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50        1x                     4x 4x   4x 4x     4x 4x       3x 1x         2x           2x       1x            
import { NextRequest, NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { NEO4J_PROXY_URL, loadTestHeaders } from "@/lib/proxy";
 
export const dynamic = "force-dynamic";
 
/**
 * GET /api/catalog/jsonld?id=<datasetId>
 *
 * A dataset's description in HealthDCAT-AP Release 8 JSON-LD (#700), or the
 * whole catalogue without `id`. The proxy writes it (services/neo4j-proxy/src/
 * healthdcat-ap.ts) and CI validates that output with the official SHACL, so
 * the UI passes it through rather than writing its own.
 */
export async function GET(req: NextRequest): Promise<NextResponse> {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  const id = req.nextUrl.searchParams.get("id");
  const url = id
    ? `${NEO4J_PROXY_URL}/catalog/datasets/${encodeURIComponent(id)}`
    : `${NEO4J_PROXY_URL}/catalog/datasets`;
  try {
    const res = await fetch(url, {
      headers: { Accept: "application/ld+json", ...loadTestHeaders(req) },
      cache: "no-store",
    });
    if (res.status === 404) {
      return NextResponse.json(
        { error: `Dataset ${id} not found` },
        { status: 404 },
      );
    }
    Iif (!res.ok) {
      return NextResponse.json(
        { error: `Catalogue unavailable (${res.status})` },
        { status: 502 },
      );
    }
    return NextResponse.json(await res.json(), {
      headers: { "Content-Type": "application/ld+json" },
    });
  } catch (err) {
    return NextResponse.json(
      { error: "Catalogue unavailable", detail: String(err) },
      { status: 502 },
    );
  }
}