Skip to content

Commit bbcb3a7

Browse files
authored
fix: handle BigInt serialization in REST API v1 detail endpoints (#53)
GET /api/v1/pipelines/:id and /api/v1/nodes/:id return 500 because NodePipelineStatus contains BigInt fields (eventsIn, eventsOut, etc.) that JSON.stringify cannot serialize. Adds a jsonResponse() helper using a custom replacer to convert BigInts to numbers.
1 parent 1d1853e commit bbcb3a7

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/app/api/v1/_lib/api-handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import {
66
type ServiceAccountContext,
77
} from "@/server/middleware/api-auth";
88

9+
/** BigInt-safe NextResponse.json() — converts BigInts to numbers before serialization. */
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
export function jsonResponse(data: any, init?: { status?: number }) {
12+
const body = JSON.stringify(data, (_key, value) =>
13+
typeof value === "bigint" ? Number(value) : value,
14+
);
15+
return new NextResponse(body, {
16+
status: init?.status ?? 200,
17+
headers: { "Content-Type": "application/json" },
18+
});
19+
}
20+
921
const TRPC_TO_HTTP: Record<string, number> = {
1022
NOT_FOUND: 404,
1123
BAD_REQUEST: 400,

src/app/api/v1/nodes/[id]/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextResponse } from "next/server";
22
import { prisma } from "@/lib/prisma";
3-
import { apiRoute } from "../../_lib/api-handler";
3+
import { apiRoute, jsonResponse } from "../../_lib/api-handler";
44

55
export const GET = apiRoute("nodes.read", async (_req, ctx, params) => {
66
const id = params?.id;
@@ -41,5 +41,5 @@ export const GET = apiRoute("nodes.read", async (_req, ctx, params) => {
4141
return NextResponse.json({ error: "Node not found" }, { status: 404 });
4242
}
4343

44-
return NextResponse.json({ node });
44+
return jsonResponse({ node });
4545
});

src/app/api/v1/pipelines/[id]/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextResponse } from "next/server";
22
import { prisma } from "@/lib/prisma";
3-
import { apiRoute } from "../../_lib/api-handler";
3+
import { apiRoute, jsonResponse } from "../../_lib/api-handler";
44

55
export const GET = apiRoute("pipelines.read", async (_req, ctx, params) => {
66
const id = params?.id;
@@ -58,5 +58,5 @@ export const GET = apiRoute("pipelines.read", async (_req, ctx, params) => {
5858
);
5959
}
6060

61-
return NextResponse.json({ pipeline });
61+
return jsonResponse({ pipeline });
6262
});

0 commit comments

Comments
 (0)