UNPKG

drizzle-cube

Version:

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

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