feat: expose durable dataset version create and query routes
This commit is contained in:
@@ -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<string, unknown>, 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<string, unknown>;
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user