| 90 | } |
| 91 | |
| 92 | async function tinybirdIngest({ host, token, datasource, ndjson }) { |
| 93 | const search = new URLSearchParams({ name: datasource, format: "ndjson" }); |
| 94 | const url = `${host.replace(/\/$/, "")}/v0/events?${search.toString()}`; |
| 95 | const response = await fetch(url, { |
| 96 | method: "POST", |
| 97 | headers: { |
| 98 | Authorization: `Bearer ${token}`, |
| 99 | "Content-Type": "application/x-ndjson", |
| 100 | Accept: "application/json", |
| 101 | }, |
| 102 | body: ndjson, |
| 103 | }); |
| 104 | const text = await response.text(); |
| 105 | if (!response.ok) { |
| 106 | let message = text; |
| 107 | try { |
| 108 | const payload = JSON.parse(text || "{}"); |
| 109 | message = payload?.error || payload?.message || text; |
| 110 | } catch {} |
| 111 | throw new Error(`Tinybird ingest failed (${response.status}): ${message}`); |
| 112 | } |
| 113 | return text ? JSON.parse(text) : {}; |
| 114 | } |
| 115 | |
| 116 | function parseDatabaseUrl(url) { |
| 117 | if (!url) throw new Error("DATABASE_URL not found"); |