From e70acc8981b1247d14f3fcb4779a6642d3487510 Mon Sep 17 00:00:00 2001 From: Simon Axelsson Date: Mon, 7 Sep 2026 18:07:01 +0200 Subject: [PATCH] feat: expose durable dataset version create and query routes --- .../datasets/[datasetId]/versions/route.ts | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 app/api/0pt/datasets/[datasetId]/versions/route.ts diff --git a/app/api/0pt/datasets/[datasetId]/versions/route.ts b/app/api/0pt/datasets/[datasetId]/versions/route.ts new file mode 100644 index 0000000..3e5b0f3 --- /dev/null +++ b/app/api/0pt/datasets/[datasetId]/versions/route.ts @@ -0,0 +1,75 @@ +import { NextRequest, NextResponse } from "next/server"; + +import { assertTenantMatchesVerified } from "@/src/http/tenant-guard"; +import { siaxErrorResponse } from "@/src/http/siax-error-response"; +import { SiaxError } from "@/src/platform/errors"; +import { datasetVersionService } from "@/src/services/datasets"; + +function required(body: Record, field: string): string { + const value = body[field]; + if (typeof value !== "string" || !value.trim()) { + throw new SiaxError("INVALID_REQUEST", "0pt.dataset", `${field} is required`); + } + return value.trim(); +} + +export async function POST( + request: NextRequest, + { params }: { params: Promise<{ datasetId: string }> }, +) { + try { + const { datasetId } = await params; + const raw = await request.json(); + if (!raw || typeof raw !== "object" || Array.isArray(raw)) { + throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "JSON object body is required"); + } + const body = raw as Record; + const tenantId = required(body, "tenant_id"); + const guard = assertTenantMatchesVerified(request, tenantId); + if (guard) return guard; + if (!Array.isArray(body.artifact_refs) || !body.artifact_refs.every((value) => typeof value === "string")) { + throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "artifact_refs must be a string array"); + } + + const recordCount = body.record_count === undefined ? undefined : Number(body.record_count); + const idempotencyKey = request.headers.get("idempotency-key")?.trim() + || (typeof body.idempotency_key === "string" ? body.idempotency_key.trim() : "") + || undefined; + + const version = await datasetVersionService.createVersion({ + dataset_id: datasetId, + tenant_id: tenantId, + project_id: required(body, "project_id"), + artifact_refs: body.artifact_refs, + schema_version: required(body, "schema_version"), + ...(recordCount === undefined ? {} : { record_count: recordCount }), + ...(typeof body.description === "string" && body.description.trim() + ? { description: body.description.trim() } + : {}), + created_by: request.headers.get("x-0pt-verified-sub") ?? "local-dev", + ...(typeof body.correlation_id === "string" && body.correlation_id.trim() + ? { correlation_id: body.correlation_id.trim() } + : {}), + ...(idempotencyKey ? { idempotency_key: idempotencyKey } : {}), + }); + return NextResponse.json(version, { status: 201 }); + } catch (error: unknown) { + return siaxErrorResponse(error); + } +} + +export async function GET( + request: NextRequest, + { params }: { params: Promise<{ datasetId: string }> }, +) { + try { + const { datasetId } = await params; + const tenantId = new URL(request.url).searchParams.get("tenant_id")?.trim(); + if (!tenantId) throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "tenant_id is required"); + const guard = assertTenantMatchesVerified(request, tenantId); + if (guard) return guard; + return NextResponse.json(await datasetVersionService.listVersions(datasetId, tenantId)); + } catch (error: unknown) { + return siaxErrorResponse(error); + } +}