drizzle-cube 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,211 @@
1
+ import { NextResponse as o } from "next/server";
2
+ import { c as l, f as S, a as b, b as H, h as p, S as v } from "../utils-C3A4JGFs.js";
3
+ function j(t) {
4
+ const { cubes: e, drizzle: a, schema: s, engineType: y } = t;
5
+ if (!e || e.length === 0)
6
+ throw new Error("At least one cube must be provided in the cubes array");
7
+ const r = new v({
8
+ drizzle: a,
9
+ schema: s,
10
+ engineType: y
11
+ });
12
+ return e.forEach((c) => {
13
+ r.registerCube(c);
14
+ }), console.log(`🚀 Drizzle Cube: Registered ${e.length} cube(s) with ${y || "auto-detected"} engine`), r;
15
+ }
16
+ function g(t, e) {
17
+ const a = t.headers.get("origin"), s = {};
18
+ return e.origin && (typeof e.origin == "string" ? s["Access-Control-Allow-Origin"] = e.origin : Array.isArray(e.origin) ? a && e.origin.includes(a) && (s["Access-Control-Allow-Origin"] = a) : typeof e.origin == "function" && a && e.origin(a) && (s["Access-Control-Allow-Origin"] = a)), 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;
19
+ }
20
+ function P(t) {
21
+ return async function(a) {
22
+ const s = g(a, t);
23
+ return new Response(null, {
24
+ status: 200,
25
+ headers: s
26
+ });
27
+ };
28
+ }
29
+ function A(t) {
30
+ const { extractSecurityContext: e, cors: a } = t, s = j(t);
31
+ return async function(r, c) {
32
+ try {
33
+ let n;
34
+ if (r.method === "POST") {
35
+ const d = await r.json();
36
+ n = d.query || d;
37
+ } else if (r.method === "GET") {
38
+ const d = r.nextUrl.searchParams.get("query");
39
+ if (!d)
40
+ return o.json(
41
+ l("Query parameter is required", 400),
42
+ { status: 400 }
43
+ );
44
+ try {
45
+ n = JSON.parse(d);
46
+ } catch {
47
+ return o.json(
48
+ l("Invalid JSON in query parameter", 400),
49
+ { status: 400 }
50
+ );
51
+ }
52
+ } else
53
+ return o.json(
54
+ l("Method not allowed", 405),
55
+ { status: 405 }
56
+ );
57
+ const f = await e(r, c), i = s.validateQuery(n);
58
+ if (!i.isValid)
59
+ return o.json(
60
+ l(`Query validation failed: ${i.errors.join(", ")}`, 400),
61
+ { status: 400 }
62
+ );
63
+ const u = await s.executeMultiCubeQuery(n, f), m = S(n, u, s);
64
+ return o.json(m, {
65
+ headers: a ? g(r, a) : {}
66
+ });
67
+ } catch (n) {
68
+ return console.error("Next.js load handler error:", n), o.json(
69
+ l(
70
+ n instanceof Error ? n.message : "Query execution failed",
71
+ 500
72
+ ),
73
+ { status: 500 }
74
+ );
75
+ }
76
+ };
77
+ }
78
+ function E(t) {
79
+ const { cors: e } = t, a = j(t);
80
+ return async function(y, r) {
81
+ try {
82
+ const c = a.getMetadata(), n = b(c);
83
+ return o.json(n, {
84
+ headers: e ? g(y, e) : {}
85
+ });
86
+ } catch (c) {
87
+ return console.error("Next.js meta handler error:", c), o.json(
88
+ l(
89
+ c instanceof Error ? c.message : "Failed to fetch metadata",
90
+ 500
91
+ ),
92
+ { status: 500 }
93
+ );
94
+ }
95
+ };
96
+ }
97
+ function N(t) {
98
+ const { extractSecurityContext: e, cors: a } = t, s = j(t);
99
+ return async function(r, c) {
100
+ var n, f;
101
+ try {
102
+ let i;
103
+ if (r.method === "POST") {
104
+ const h = await r.json();
105
+ i = h.query || h;
106
+ } else if (r.method === "GET") {
107
+ const h = r.nextUrl.searchParams.get("query");
108
+ if (!h)
109
+ return o.json(
110
+ l("Query parameter is required", 400),
111
+ { status: 400 }
112
+ );
113
+ try {
114
+ i = JSON.parse(h);
115
+ } catch {
116
+ return o.json(
117
+ l("Invalid JSON in query parameter", 400),
118
+ { status: 400 }
119
+ );
120
+ }
121
+ } else
122
+ return o.json(
123
+ l("Method not allowed", 405),
124
+ { status: 405 }
125
+ );
126
+ const u = await e(r, c), m = s.validateQuery(i);
127
+ if (!m.isValid)
128
+ return o.json(
129
+ l(`Query validation failed: ${m.errors.join(", ")}`, 400),
130
+ { status: 400 }
131
+ );
132
+ const d = ((n = i.measures) == null ? void 0 : n[0]) || ((f = i.dimensions) == null ? void 0 : f[0]);
133
+ if (!d)
134
+ return o.json(
135
+ l("No measures or dimensions specified", 400),
136
+ { status: 400 }
137
+ );
138
+ const w = d.split(".")[0], x = await s.generateSQL(w, i, u), C = H(i, x);
139
+ return o.json(C, {
140
+ headers: a ? g(r, a) : {}
141
+ });
142
+ } catch (i) {
143
+ return console.error("Next.js SQL handler error:", i), o.json(
144
+ l(
145
+ i instanceof Error ? i.message : "SQL generation failed",
146
+ 500
147
+ ),
148
+ { status: 500 }
149
+ );
150
+ }
151
+ };
152
+ }
153
+ function Q(t) {
154
+ const { extractSecurityContext: e, cors: a } = t, s = j(t);
155
+ return async function(r, c) {
156
+ try {
157
+ let n;
158
+ if (r.method === "POST") {
159
+ const u = await r.json();
160
+ n = u.query || u;
161
+ } else if (r.method === "GET") {
162
+ const u = r.nextUrl.searchParams.get("query");
163
+ if (!u)
164
+ return o.json(
165
+ { error: "Query parameter is required", valid: !1 },
166
+ { status: 400 }
167
+ );
168
+ try {
169
+ n = JSON.parse(u);
170
+ } catch {
171
+ return o.json(
172
+ { error: "Invalid JSON in query parameter", valid: !1 },
173
+ { status: 400 }
174
+ );
175
+ }
176
+ } else
177
+ return o.json(
178
+ { error: "Method not allowed", valid: !1 },
179
+ { status: 405 }
180
+ );
181
+ const f = await e(r, c), i = await p(n, f, s);
182
+ return o.json(i, {
183
+ headers: a ? g(r, a) : {}
184
+ });
185
+ } catch (n) {
186
+ return console.error("Next.js dry-run handler error:", n), o.json(
187
+ {
188
+ error: n instanceof Error ? n.message : "Dry-run validation failed",
189
+ valid: !1
190
+ },
191
+ { status: 400 }
192
+ );
193
+ }
194
+ };
195
+ }
196
+ function q(t) {
197
+ return {
198
+ load: A(t),
199
+ meta: E(t),
200
+ sql: N(t),
201
+ dryRun: Q(t)
202
+ };
203
+ }
204
+ export {
205
+ q as createCubeHandlers,
206
+ Q as createDryRunHandler,
207
+ A as createLoadHandler,
208
+ E as createMetaHandler,
209
+ P as createOptionsHandler,
210
+ N as createSqlHandler
211
+ };