UNPKG

drizzle-cube

Version:

Drizzle ORM-first semantic layer with Cube.js compatibility. Type-safe analytics and dashboards with SQL injection protection.

212 lines (211 loc) 6.04 kB
import { NextResponse as o } from "next/server"; import { c as l, f as C, a as H, b as p, h as v, S as A } from "../compiler-Dec55JjO.js"; function g(t) { const { cubes: e, drizzle: n, schema: s, engineType: y } = t; if (!e || e.length === 0) throw new Error("At least one cube must be provided in the cubes array"); const r = new A({ drizzle: n, schema: s, engineType: y }); return e.forEach((c) => { r.registerCube(c); }), r; } function j(t, e) { const n = t.headers.get("origin"), s = {}; return e.origin && (typeof e.origin == "string" ? s["Access-Control-Allow-Origin"] = e.origin : Array.isArray(e.origin) ? n && e.origin.includes(n) && (s["Access-Control-Allow-Origin"] = n) : typeof e.origin == "function" && n && e.origin(n) && (s["Access-Control-Allow-Origin"] = n)), e.methods && (s["Access-Control-Allow-Methods"] = e.methods.join(", ")), e.allowedHeaders && (s["Access-Control-Allow-Headers"] = e.allowedHeaders.join(", ")), e.credentials && (s["Access-Control-Allow-Credentials"] = "true"), s; } function P(t) { return async function(n) { const s = j(n, t); return new Response(null, { status: 200, headers: s }); }; } function b(t) { const { extractSecurityContext: e, cors: n } = t, s = g(t); return async function(r, c) { try { let a; if (r.method === "POST") { const d = await r.json(); a = d.query || d; } else if (r.method === "GET") { const d = r.nextUrl.searchParams.get("query"); if (!d) return o.json( l("Query parameter is required", 400), { status: 400 } ); try { a = JSON.parse(d); } catch { return o.json( l("Invalid JSON in query parameter", 400), { status: 400 } ); } } else return o.json( l("Method not allowed", 405), { status: 405 } ); const f = await e(r, c), i = s.validateQuery(a); if (!i.isValid) return o.json( l(`Query validation failed: ${i.errors.join(", ")}`, 400), { status: 400 } ); const u = await s.executeMultiCubeQuery(a, f), m = C(a, u, s); return o.json(m, { headers: n ? j(r, n) : {} }); } catch (a) { return console.error("Next.js load handler error:", a), o.json( l( a instanceof Error ? a.message : "Query execution failed", 500 ), { status: 500 } ); } }; } function E(t) { const { cors: e } = t, n = g(t); return async function(y, r) { try { const c = n.getMetadata(), a = H(c); return o.json(a, { headers: e ? j(y, e) : {} }); } catch (c) { return console.error("Next.js meta handler error:", c), o.json( l( c instanceof Error ? c.message : "Failed to fetch metadata", 500 ), { status: 500 } ); } }; } function N(t) { const { extractSecurityContext: e, cors: n } = t, s = g(t); return async function(r, c) { var a, f; try { let i; if (r.method === "POST") { const h = await r.json(); i = h.query || h; } else if (r.method === "GET") { const h = r.nextUrl.searchParams.get("query"); if (!h) return o.json( l("Query parameter is required", 400), { status: 400 } ); try { i = JSON.parse(h); } catch { return o.json( l("Invalid JSON in query parameter", 400), { status: 400 } ); } } else return o.json( l("Method not allowed", 405), { status: 405 } ); const u = await e(r, c), m = s.validateQuery(i); if (!m.isValid) return o.json( l(`Query validation failed: ${m.errors.join(", ")}`, 400), { status: 400 } ); const d = ((a = i.measures) == null ? void 0 : a[0]) || ((f = i.dimensions) == null ? void 0 : f[0]); if (!d) return o.json( l("No measures or dimensions specified", 400), { status: 400 } ); const w = d.split(".")[0], x = await s.generateSQL(w, i, u), S = p(i, x); return o.json(S, { headers: n ? j(r, n) : {} }); } catch (i) { return console.error("Next.js SQL handler error:", i), o.json( l( i instanceof Error ? i.message : "SQL generation failed", 500 ), { status: 500 } ); } }; } function Q(t) { const { extractSecurityContext: e, cors: n } = t, s = g(t); return async function(r, c) { try { let a; if (r.method === "POST") { const u = await r.json(); a = u.query || u; } else if (r.method === "GET") { const u = r.nextUrl.searchParams.get("query"); if (!u) return o.json( { error: "Query parameter is required", valid: !1 }, { status: 400 } ); try { a = JSON.parse(u); } catch { return o.json( { error: "Invalid JSON in query parameter", valid: !1 }, { status: 400 } ); } } else return o.json( { error: "Method not allowed", valid: !1 }, { status: 405 } ); const f = await e(r, c), i = await v(a, f, s); return o.json(i, { headers: n ? j(r, n) : {} }); } catch (a) { return console.error("Next.js dry-run handler error:", a), o.json( { error: a instanceof Error ? a.message : "Dry-run validation failed", valid: !1 }, { status: 400 } ); } }; } function q(t) { return { load: b(t), meta: E(t), sql: N(t), dryRun: Q(t) }; } export { q as createCubeHandlers, Q as createDryRunHandler, b as createLoadHandler, E as createMetaHandler, P as createOptionsHandler, N as createSqlHandler };