From 6c986b16b6cae8bc4c18f0b95e5030b36e3a0387 Mon Sep 17 00:00:00 2001 From: Agochar Singh Bhatia Date: Wed, 18 Nov 2026 16:04:54 -0500 Subject: [PATCH] fix: serialize nested map values during ingestion --- src/api/api.test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/api/api.ts | 4 +++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/api/api.test.ts b/src/api/api.test.ts index 44d45cc..7b5fb6b 100644 --- a/src/api/api.test.ts +++ b/src/api/api.test.ts @@ -270,6 +270,50 @@ describe("TinybirdApi", () => { ).rejects.toThrow("Date values are not supported in ingest payloads"); }); + it.each([ + { + name: "bigint entries", + payload: new Map([["total", 9007199254740993n], ["pending", 0n]]), + expected: { total: "9007199254740993", pending: "0" }, + }, + { + name: "nested maps", + payload: new Map([["current", new Map([["count", 42]])]]), + expected: { current: { count: 42 } }, + }, + ])("serializes $name in ingested maps", async ({ payload, expected }) => { + let event: unknown; + server.use( + http.post(`${BASE_URL}/v0/events`, async ({ request }) => { + event = JSON.parse(await request.text()); + return HttpResponse.json({ successful_rows: 1, quarantined_rows: 0 }); + }) + ); + + const api = createTinybirdApi({ baseUrl: BASE_URL, token: "p.default-token" }); + await api.ingest("events", { payload }); + + expect(event).toEqual({ payload: expected }); + }); + + it("rejects Date values inside ingest maps before making a request", async () => { + let requests = 0; + server.use( + http.post(`${BASE_URL}/v0/events`, () => { + requests += 1; + return HttpResponse.json({ successful_rows: 1, quarantined_rows: 0 }); + }) + ); + + const api = createTinybirdApi({ baseUrl: BASE_URL, token: "p.default-token" }); + await expect( + api.ingest("events", { + payload: new Map([["when", new Date("2024-01-01T00:00:00.000Z")]]), + }) + ).rejects.toThrow("Date values are not supported in ingest payloads"); + expect(requests).toBe(0); + }); + it("does not retry ingest on 503 when retry is disabled", async () => { let attempts = 0; diff --git a/src/api/api.ts b/src/api/api.ts index 53b9230..78fdb25 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -790,7 +790,9 @@ export class TinybirdApi { } if (value instanceof Map) { - return Object.fromEntries(value); + return Object.fromEntries( + Array.from(value, ([key, entry]) => [key, this.serializeValue(entry)]) + ); } if (typeof value === "bigint") {