@constructive-io/sheets 0.3.0 → 0.4.1
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.
- package/dist/advanced.cjs +449 -109
- package/dist/advanced.cjs.map +1 -1
- package/dist/advanced.js +3 -3
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.js +2 -2
- package/dist/{chunk-FNVHSOQQ.js → chunk-3BNOZQIJ.js} +2 -2
- package/dist/{chunk-ODUBS2NL.js → chunk-BWJVRYJT.js} +3 -3
- package/dist/{chunk-TU7ATZ4X.js → chunk-BWKB52JB.js} +73 -34
- package/dist/chunk-BWKB52JB.js.map +1 -0
- package/dist/{chunk-4PGPVMZB.js → chunk-FG2LRZP5.js} +1 -1
- package/dist/chunk-FG2LRZP5.js.map +1 -0
- package/dist/chunk-SDKHWE7G.js +469 -0
- package/dist/chunk-SDKHWE7G.js.map +1 -0
- package/dist/index.cjs +630 -254
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -3
- package/dist/index.d.ts +40 -3
- package/dist/index.js +66 -34
- package/dist/index.js.map +1 -1
- package/dist/{sheets-context-Da55TTR9.d.ts → sheets-context-9u9r7Bs0.d.ts} +23 -0
- package/dist/{sheets-context-CojEFlaY.d.cts → sheets-context-BjGIHocT.d.cts} +23 -0
- package/dist/testing.cjs +391 -89
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +3 -3
- package/package.json +3 -3
- package/dist/chunk-4PGPVMZB.js.map +0 -1
- package/dist/chunk-H4JJTMP5.js +0 -160
- package/dist/chunk-H4JJTMP5.js.map +0 -1
- package/dist/chunk-TU7ATZ4X.js.map +0 -1
- /package/dist/{chunk-FNVHSOQQ.js.map → chunk-3BNOZQIJ.js.map} +0 -0
- /package/dist/{chunk-ODUBS2NL.js.map → chunk-BWJVRYJT.js.map} +0 -0
package/dist/testing.cjs
CHANGED
|
@@ -29,9 +29,9 @@ module.exports = __toCommonJS(testing_exports);
|
|
|
29
29
|
|
|
30
30
|
// src/testing/mock-sheets-provider.tsx
|
|
31
31
|
var import_react4 = require("react");
|
|
32
|
-
var
|
|
32
|
+
var import_graphql3 = require("graphql");
|
|
33
33
|
var import_react_query2 = require("@tanstack/react-query");
|
|
34
|
-
var
|
|
34
|
+
var import_data6 = require("@constructive-io/data");
|
|
35
35
|
|
|
36
36
|
// src/context/sheets-provider.tsx
|
|
37
37
|
var import_react3 = require("react");
|
|
@@ -42,17 +42,343 @@ var import_react = require("react");
|
|
|
42
42
|
var SheetsContext = (0, import_react.createContext)(null);
|
|
43
43
|
|
|
44
44
|
// src/context/sheets-execute.ts
|
|
45
|
+
var import_graphql2 = require("graphql");
|
|
46
|
+
var import_data2 = require("@constructive-io/data");
|
|
47
|
+
|
|
48
|
+
// src/context/sheets-upload-presigned.ts
|
|
45
49
|
var import_graphql = require("graphql");
|
|
46
50
|
var import_data = require("@constructive-io/data");
|
|
47
|
-
|
|
51
|
+
async function sha256HexFromBuffer(buffer) {
|
|
52
|
+
if (!globalThis.crypto?.subtle) {
|
|
53
|
+
throw import_data.createError.badRequest("Web Crypto (crypto.subtle) is unavailable; cannot hash file for upload.");
|
|
54
|
+
}
|
|
55
|
+
const digest = await crypto.subtle.digest("SHA-256", buffer);
|
|
56
|
+
const bytes = new Uint8Array(digest);
|
|
57
|
+
let hex = "";
|
|
58
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
59
|
+
hex += bytes[i].toString(16).padStart(2, "0");
|
|
60
|
+
}
|
|
61
|
+
return hex;
|
|
62
|
+
}
|
|
63
|
+
var capabilityCache = /* @__PURE__ */ new Map();
|
|
64
|
+
function discoverUploadCapability(endpoint, execute) {
|
|
65
|
+
const cached = capabilityCache.get(endpoint);
|
|
66
|
+
if (cached) return cached;
|
|
67
|
+
const promise = (async () => {
|
|
68
|
+
const data = await execute((0, import_graphql.getIntrospectionQuery)());
|
|
69
|
+
const schema = data?.__schema;
|
|
70
|
+
if (!schema) {
|
|
71
|
+
throw import_data.createError.badRequest("Uploads are not enabled for this endpoint.");
|
|
72
|
+
}
|
|
73
|
+
return resolveUploadCapability(schema);
|
|
74
|
+
})();
|
|
75
|
+
promise.catch(() => capabilityCache.delete(endpoint));
|
|
76
|
+
capabilityCache.set(endpoint, promise);
|
|
77
|
+
return promise;
|
|
78
|
+
}
|
|
79
|
+
function namedTypeName(ref) {
|
|
80
|
+
let cur = ref;
|
|
81
|
+
while (cur && (cur.kind === "NON_NULL" || cur.kind === "LIST")) {
|
|
82
|
+
cur = cur.ofType ?? null;
|
|
83
|
+
}
|
|
84
|
+
return cur?.name ?? null;
|
|
85
|
+
}
|
|
86
|
+
function isListTypeRef(ref) {
|
|
87
|
+
let cur = ref;
|
|
88
|
+
while (cur && cur.kind === "NON_NULL") {
|
|
89
|
+
cur = cur.ofType ?? null;
|
|
90
|
+
}
|
|
91
|
+
return cur?.kind === "LIST";
|
|
92
|
+
}
|
|
93
|
+
var PK_FIELD_NAME = "id";
|
|
94
|
+
function resolveUploadCapability(schema) {
|
|
95
|
+
const typeByName = /* @__PURE__ */ new Map();
|
|
96
|
+
for (const t of schema.types) {
|
|
97
|
+
if (t.name) typeByName.set(t.name, t);
|
|
98
|
+
}
|
|
99
|
+
const objectHasFields = (typeName, names) => {
|
|
100
|
+
if (!typeName) return false;
|
|
101
|
+
const t = typeByName.get(typeName);
|
|
102
|
+
if (!t?.fields) return false;
|
|
103
|
+
const present = new Set(t.fields.map((f) => f.name));
|
|
104
|
+
return names.every((n) => present.has(n));
|
|
105
|
+
};
|
|
106
|
+
const inputHasFields = (typeName, names) => {
|
|
107
|
+
if (!typeName) return false;
|
|
108
|
+
const t = typeByName.get(typeName);
|
|
109
|
+
if (!t?.inputFields) return false;
|
|
110
|
+
const present = new Set(t.inputFields.map((f) => f.name));
|
|
111
|
+
return names.every((n) => present.has(n));
|
|
112
|
+
};
|
|
113
|
+
const PAYLOAD_FIELDS = ["uploadUrl", "fileId", "deduplicated"];
|
|
114
|
+
const INPUT_FIELDS = ["contentHash", "bucketKey", "contentType", "size"];
|
|
115
|
+
const mutationType = schema.mutationType ? typeByName.get(schema.mutationType.name) : null;
|
|
116
|
+
const queryType = schema.queryType ? typeByName.get(schema.queryType.name) : null;
|
|
117
|
+
let upload = null;
|
|
118
|
+
for (const field of mutationType?.fields ?? []) {
|
|
119
|
+
const inputArg = field.args.find((a) => a.name === "input");
|
|
120
|
+
if (!inputArg) continue;
|
|
121
|
+
const inputTypeName = namedTypeName(inputArg.type);
|
|
122
|
+
const payloadTypeName = namedTypeName(field.type);
|
|
123
|
+
if (inputHasFields(inputTypeName, INPUT_FIELDS) && objectHasFields(payloadTypeName, PAYLOAD_FIELDS)) {
|
|
124
|
+
upload = { uploadFieldName: field.name, inputTypeName };
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!upload) {
|
|
129
|
+
throw import_data.createError.badRequest("Uploads are not enabled for this endpoint.");
|
|
130
|
+
}
|
|
131
|
+
const downloadById = resolveDownloadByIdField(schema, typeByName, queryType, upload.uploadFieldName);
|
|
132
|
+
return {
|
|
133
|
+
...upload,
|
|
134
|
+
...downloadById
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function resolveDownloadByIdField(schema, typeByName, queryType, uploadFieldName) {
|
|
138
|
+
const fileTypesWithDownloadUrl = /* @__PURE__ */ new Set();
|
|
139
|
+
for (const t of schema.types) {
|
|
140
|
+
if (t.kind === "OBJECT" && t.name && t.fields?.some((f) => f.name === "downloadUrl")) {
|
|
141
|
+
fileTypesWithDownloadUrl.add(t.name);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
for (const field of queryType?.fields ?? []) {
|
|
145
|
+
const returnTypeName = namedTypeName(field.type);
|
|
146
|
+
if (!returnTypeName || !fileTypesWithDownloadUrl.has(returnTypeName)) continue;
|
|
147
|
+
if (field.args.length === 1 && field.args[0].name === PK_FIELD_NAME) {
|
|
148
|
+
return {
|
|
149
|
+
fileByIdField: field.name,
|
|
150
|
+
fileByIdArgName: "id",
|
|
151
|
+
fileTypeName: returnTypeName,
|
|
152
|
+
idScalarName: namedTypeName(field.args[0].type) ?? void 0
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const connection = resolveConnectionDownloadField(typeByName, queryType, fileTypesWithDownloadUrl, uploadFieldName);
|
|
157
|
+
if (connection) return connection;
|
|
158
|
+
const nodeField = queryType?.fields?.find(
|
|
159
|
+
(f) => f.name === "node" && f.args.some((a) => a.name === "nodeId")
|
|
160
|
+
);
|
|
161
|
+
if (nodeField && fileTypesWithDownloadUrl.size > 0) {
|
|
162
|
+
const preferred = uploadFieldName.replace(/^upload/, "");
|
|
163
|
+
const fileTypeName = fileTypesWithDownloadUrl.has(preferred) ? preferred : [...fileTypesWithDownloadUrl][0];
|
|
164
|
+
return { fileByIdField: "node", fileByIdArgName: "nodeId", fileTypeName };
|
|
165
|
+
}
|
|
166
|
+
throw import_data.createError.badRequest("Uploads are not enabled for this endpoint.");
|
|
167
|
+
}
|
|
168
|
+
function resolveConnectionDownloadField(typeByName, queryType, fileTypesWithDownloadUrl, uploadFieldName) {
|
|
169
|
+
const EQUALITY_OPERATORS = ["equalTo", "eq"];
|
|
170
|
+
const candidates = [];
|
|
171
|
+
for (const field of queryType?.fields ?? []) {
|
|
172
|
+
const connTypeName = namedTypeName(field.type);
|
|
173
|
+
if (!connTypeName) continue;
|
|
174
|
+
const connType = typeByName.get(connTypeName);
|
|
175
|
+
if (connType?.kind !== "OBJECT" || !connType.fields) continue;
|
|
176
|
+
const nodesField = connType.fields.find((f) => {
|
|
177
|
+
if (!isListTypeRef(f.type)) return false;
|
|
178
|
+
const elem = namedTypeName(f.type);
|
|
179
|
+
return elem != null && fileTypesWithDownloadUrl.has(elem);
|
|
180
|
+
});
|
|
181
|
+
if (!nodesField) continue;
|
|
182
|
+
const fileTypeName = namedTypeName(nodesField.type);
|
|
183
|
+
for (const arg of field.args) {
|
|
184
|
+
const filterTypeName = namedTypeName(arg.type);
|
|
185
|
+
const filterType = filterTypeName ? typeByName.get(filterTypeName) : null;
|
|
186
|
+
if (filterType?.kind !== "INPUT_OBJECT" || !filterType.inputFields) continue;
|
|
187
|
+
const idField = filterType.inputFields.find((f) => f.name === PK_FIELD_NAME);
|
|
188
|
+
if (!idField) continue;
|
|
189
|
+
const idFieldTypeName = namedTypeName(idField.type);
|
|
190
|
+
const idFieldType = idFieldTypeName ? typeByName.get(idFieldTypeName) : null;
|
|
191
|
+
if (idFieldType?.kind === "INPUT_OBJECT" && idFieldType.inputFields) {
|
|
192
|
+
const opField = EQUALITY_OPERATORS.map(
|
|
193
|
+
(op) => idFieldType.inputFields.find((f) => f.name === op)
|
|
194
|
+
).find((f) => f != null);
|
|
195
|
+
if (opField) {
|
|
196
|
+
candidates.push({
|
|
197
|
+
fileByIdField: field.name,
|
|
198
|
+
fileByIdArgName: "connection",
|
|
199
|
+
fileTypeName,
|
|
200
|
+
idScalarName: namedTypeName(opField.type) ?? void 0,
|
|
201
|
+
connectionFilterArgName: arg.name,
|
|
202
|
+
connectionIdField: idField.name,
|
|
203
|
+
connectionIdOperator: opField.name,
|
|
204
|
+
connectionNodesField: nodesField.name
|
|
205
|
+
});
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (idFieldType?.kind === "SCALAR" || !idFieldType && idFieldTypeName) {
|
|
211
|
+
candidates.push({
|
|
212
|
+
fileByIdField: field.name,
|
|
213
|
+
fileByIdArgName: "connection",
|
|
214
|
+
fileTypeName,
|
|
215
|
+
idScalarName: idFieldTypeName ?? void 0,
|
|
216
|
+
connectionFilterArgName: arg.name,
|
|
217
|
+
connectionIdField: idField.name,
|
|
218
|
+
connectionNodesField: nodesField.name
|
|
219
|
+
});
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (candidates.length === 0) return null;
|
|
225
|
+
const preferred = uploadFieldName.replace(/^upload/, "");
|
|
226
|
+
return candidates.find((c) => c.fileTypeName === preferred) ?? candidates[0];
|
|
227
|
+
}
|
|
228
|
+
async function callUploadOp(execute, cap, input) {
|
|
229
|
+
const mutation = `
|
|
230
|
+
mutation SheetsUpload($input: ${cap.inputTypeName}!) {
|
|
231
|
+
${cap.uploadFieldName}(input: $input) {
|
|
232
|
+
uploadUrl
|
|
233
|
+
fileId
|
|
234
|
+
key
|
|
235
|
+
deduplicated
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
`.trim();
|
|
239
|
+
const data = await execute(mutation, {
|
|
240
|
+
input: {
|
|
241
|
+
bucketKey: input.bucketKey,
|
|
242
|
+
contentHash: input.contentHash,
|
|
243
|
+
contentType: input.contentType,
|
|
244
|
+
size: input.size,
|
|
245
|
+
filename: input.filename
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
const payload = data?.[cap.uploadFieldName];
|
|
249
|
+
if (!payload) {
|
|
250
|
+
throw import_data.createError.badRequest("Upload failed: no payload returned from upload mutation.");
|
|
251
|
+
}
|
|
252
|
+
return payload;
|
|
253
|
+
}
|
|
254
|
+
async function presignedPut(url, buffer, contentType, onProgress) {
|
|
255
|
+
const total = buffer.byteLength;
|
|
256
|
+
if (onProgress && typeof XMLHttpRequest !== "undefined") {
|
|
257
|
+
await putWithXHR(url, buffer, contentType, total, onProgress);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
let response;
|
|
261
|
+
try {
|
|
262
|
+
response = await fetch(url, {
|
|
263
|
+
method: "PUT",
|
|
264
|
+
headers: { "Content-Type": contentType },
|
|
265
|
+
body: buffer
|
|
266
|
+
});
|
|
267
|
+
} catch (error) {
|
|
268
|
+
throw import_data.createError.network(error instanceof Error ? error : void 0);
|
|
269
|
+
}
|
|
270
|
+
if (!response.ok) {
|
|
271
|
+
const body = await response.text().catch(() => "");
|
|
272
|
+
throw import_data.createError.badRequest(`Upload failed: ${response.status} ${response.statusText}${body ? ` \u2014 ${body}` : ""}`);
|
|
273
|
+
}
|
|
274
|
+
onProgress?.({ loaded: total, total, percentage: 100 });
|
|
275
|
+
}
|
|
276
|
+
function putWithXHR(url, buffer, contentType, total, onProgress) {
|
|
277
|
+
return new Promise((resolve, reject) => {
|
|
278
|
+
const xhr = new XMLHttpRequest();
|
|
279
|
+
xhr.open("PUT", url);
|
|
280
|
+
xhr.setRequestHeader("Content-Type", contentType);
|
|
281
|
+
xhr.upload.addEventListener("progress", (event) => {
|
|
282
|
+
if (event.lengthComputable) {
|
|
283
|
+
onProgress({
|
|
284
|
+
loaded: event.loaded,
|
|
285
|
+
total: event.total,
|
|
286
|
+
percentage: Math.round(event.loaded / event.total * 100)
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
xhr.addEventListener("load", () => {
|
|
291
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
292
|
+
onProgress({ loaded: total, total, percentage: 100 });
|
|
293
|
+
resolve();
|
|
294
|
+
} else {
|
|
295
|
+
reject(import_data.createError.badRequest(`Upload failed: ${xhr.status} ${xhr.statusText}${xhr.responseText ? ` \u2014 ${xhr.responseText}` : ""}`));
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
xhr.addEventListener("error", () => reject(import_data.createError.network()));
|
|
299
|
+
xhr.send(buffer);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
function toGlobalId(typeName, id) {
|
|
303
|
+
if (typeof btoa !== "function") {
|
|
304
|
+
throw import_data.createError.badRequest("Cannot encode the file node id: btoa is unavailable in this environment.");
|
|
305
|
+
}
|
|
306
|
+
return btoa(JSON.stringify([typeName, id]));
|
|
307
|
+
}
|
|
308
|
+
async function fetchDownloadUrl(execute, cap, fileId) {
|
|
309
|
+
if (cap.fileByIdArgName === "nodeId") {
|
|
310
|
+
const nodeId = toGlobalId(cap.fileTypeName, fileId);
|
|
311
|
+
const query2 = `
|
|
312
|
+
query SheetsDownloadUrl($nodeId: ID!) {
|
|
313
|
+
${cap.fileByIdField}(nodeId: $nodeId) {
|
|
314
|
+
... on ${cap.fileTypeName} {
|
|
315
|
+
downloadUrl
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
`.trim();
|
|
320
|
+
const data2 = await execute(query2, { nodeId });
|
|
321
|
+
const node = data2?.[cap.fileByIdField];
|
|
322
|
+
const url2 = node?.downloadUrl;
|
|
323
|
+
if (!url2) {
|
|
324
|
+
throw import_data.createError.badRequest("Upload succeeded but no downloadUrl was returned for the file.");
|
|
325
|
+
}
|
|
326
|
+
return url2;
|
|
327
|
+
}
|
|
328
|
+
if (cap.fileByIdArgName === "connection") {
|
|
329
|
+
const idScalar2 = cap.idScalarName ?? "ID";
|
|
330
|
+
const filterArg = cap.connectionFilterArgName;
|
|
331
|
+
const idField = cap.connectionIdField;
|
|
332
|
+
const nodesField = cap.connectionNodesField;
|
|
333
|
+
const filterLiteral = cap.connectionIdOperator ? `{ ${idField}: { ${cap.connectionIdOperator}: $id } }` : `{ ${idField}: $id }`;
|
|
334
|
+
const query2 = `
|
|
335
|
+
query SheetsDownloadUrl($id: ${idScalar2}!) {
|
|
336
|
+
${cap.fileByIdField}(${filterArg}: ${filterLiteral}, first: 1) {
|
|
337
|
+
${nodesField} {
|
|
338
|
+
downloadUrl
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
`.trim();
|
|
343
|
+
const data2 = await execute(
|
|
344
|
+
query2,
|
|
345
|
+
{ id: fileId }
|
|
346
|
+
);
|
|
347
|
+
const conn = data2?.[cap.fileByIdField];
|
|
348
|
+
const url2 = conn?.[nodesField]?.[0]?.downloadUrl;
|
|
349
|
+
if (!url2) {
|
|
350
|
+
throw import_data.createError.badRequest("Upload succeeded but no downloadUrl was returned for the file.");
|
|
351
|
+
}
|
|
352
|
+
return url2;
|
|
353
|
+
}
|
|
354
|
+
const idScalar = cap.idScalarName ?? "ID";
|
|
355
|
+
const query = `
|
|
356
|
+
query SheetsDownloadUrl($id: ${idScalar}!) {
|
|
357
|
+
${cap.fileByIdField}(id: $id) {
|
|
358
|
+
id
|
|
359
|
+
downloadUrl
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
`.trim();
|
|
363
|
+
const data = await execute(query, { id: fileId });
|
|
364
|
+
const file = data?.[cap.fileByIdField];
|
|
365
|
+
const url = file?.downloadUrl;
|
|
366
|
+
if (!url) {
|
|
367
|
+
throw import_data.createError.badRequest("Upload succeeded but no downloadUrl was returned for the file.");
|
|
368
|
+
}
|
|
369
|
+
return url;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/context/sheets-execute.ts
|
|
373
|
+
var import_data3 = require("@constructive-io/data");
|
|
48
374
|
function documentToString(document) {
|
|
49
375
|
if (typeof document === "string") return document;
|
|
50
376
|
if (document instanceof String) return document.toString();
|
|
51
377
|
if (document && typeof document === "object" && "kind" in document) {
|
|
52
|
-
return (0,
|
|
378
|
+
return (0, import_graphql2.print)(document);
|
|
53
379
|
}
|
|
54
380
|
if (document === null || document === void 0) {
|
|
55
|
-
throw
|
|
381
|
+
throw import_data2.createError.badRequest("Invalid GraphQL document: null or undefined");
|
|
56
382
|
}
|
|
57
383
|
return String(document);
|
|
58
384
|
}
|
|
@@ -67,18 +393,18 @@ function parseGraphQLResponse(response, onAuthError) {
|
|
|
67
393
|
const message = error.message || "GraphQL query failed";
|
|
68
394
|
if (code === "UNAUTHENTICATED" || hasAuthMessage(message)) {
|
|
69
395
|
onAuthError?.();
|
|
70
|
-
return
|
|
396
|
+
return import_data2.createError.unauthorized("Authentication required. Please log in again.");
|
|
71
397
|
}
|
|
72
398
|
if (code === "FORBIDDEN") {
|
|
73
|
-
return
|
|
399
|
+
return import_data2.createError.forbidden("You do not have permission to perform this action.");
|
|
74
400
|
}
|
|
75
|
-
return
|
|
401
|
+
return import_data2.createError.graphql(message, code);
|
|
76
402
|
}
|
|
77
403
|
function createSheetsExecute(config, getToken) {
|
|
78
404
|
return async (document, variables) => {
|
|
79
405
|
const url = config.endpoint;
|
|
80
406
|
if (!url) {
|
|
81
|
-
throw
|
|
407
|
+
throw import_data2.createError.badRequest("No GraphQL endpoint configured.");
|
|
82
408
|
}
|
|
83
409
|
const token = getToken();
|
|
84
410
|
const headers = {
|
|
@@ -99,29 +425,29 @@ function createSheetsExecute(config, getToken) {
|
|
|
99
425
|
})
|
|
100
426
|
});
|
|
101
427
|
} catch (error) {
|
|
102
|
-
throw
|
|
428
|
+
throw import_data2.createError.network(error instanceof Error ? error : void 0);
|
|
103
429
|
}
|
|
104
430
|
if (!response.ok) {
|
|
105
431
|
const { status, statusText } = response;
|
|
106
432
|
if (status === 401) {
|
|
107
433
|
config.onAuthError?.();
|
|
108
|
-
throw
|
|
434
|
+
throw import_data2.createError.unauthorized("Authentication required. Please log in again.");
|
|
109
435
|
}
|
|
110
436
|
if (status === 403) {
|
|
111
|
-
throw
|
|
437
|
+
throw import_data2.createError.forbidden("You do not have permission to perform this action.");
|
|
112
438
|
}
|
|
113
439
|
if (status === 404) {
|
|
114
|
-
throw
|
|
440
|
+
throw import_data2.createError.notFound("The requested endpoint does not exist.");
|
|
115
441
|
}
|
|
116
442
|
try {
|
|
117
443
|
const body = await response.json();
|
|
118
444
|
if (body.errors?.length) {
|
|
119
|
-
throw
|
|
445
|
+
throw import_data2.createError.graphql(body.errors[0].message || `Request failed: ${status}`, body.errors[0].extensions?.code);
|
|
120
446
|
}
|
|
121
447
|
} catch (e) {
|
|
122
|
-
if (e instanceof
|
|
448
|
+
if (e instanceof import_data2.DataError) throw e;
|
|
123
449
|
}
|
|
124
|
-
throw
|
|
450
|
+
throw import_data2.createError.badRequest(`Request failed: ${status} ${statusText}`);
|
|
125
451
|
}
|
|
126
452
|
const result = await response.json();
|
|
127
453
|
const graphqlError = parseGraphQLResponse(result, config.onAuthError);
|
|
@@ -132,64 +458,40 @@ function createSheetsExecute(config, getToken) {
|
|
|
132
458
|
};
|
|
133
459
|
}
|
|
134
460
|
function createSheetsUpload(config, getToken) {
|
|
135
|
-
|
|
461
|
+
const execute = config.execute ?? createSheetsExecute(config, getToken);
|
|
462
|
+
return async (file, _path, options) => {
|
|
136
463
|
if (!config.endpoint) {
|
|
137
|
-
throw
|
|
464
|
+
throw import_data2.createError.badRequest("No GraphQL endpoint configured.");
|
|
138
465
|
}
|
|
139
|
-
const
|
|
140
|
-
const
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
});
|
|
155
|
-
} catch (error) {
|
|
156
|
-
throw import_data.createError.network(error instanceof Error ? error : void 0);
|
|
157
|
-
}
|
|
158
|
-
if (!response.ok) {
|
|
159
|
-
if (response.status === 401) {
|
|
160
|
-
config.onAuthError?.();
|
|
161
|
-
throw import_data.createError.unauthorized("Authentication required. Please log in again.");
|
|
162
|
-
}
|
|
163
|
-
if (response.status === 403) {
|
|
164
|
-
throw import_data.createError.forbidden("You do not have permission to perform this action.");
|
|
165
|
-
}
|
|
166
|
-
try {
|
|
167
|
-
const body = await response.json();
|
|
168
|
-
const graphqlError = parseGraphQLResponse(body, config.onAuthError);
|
|
169
|
-
if (graphqlError) {
|
|
170
|
-
throw graphqlError;
|
|
171
|
-
}
|
|
172
|
-
if (body.errors?.length) {
|
|
173
|
-
const firstError = body.errors[0];
|
|
174
|
-
throw import_data.createError.graphql(firstError.message || "Upload failed", firstError.extensions?.code);
|
|
175
|
-
}
|
|
176
|
-
} catch (error) {
|
|
177
|
-
if (error instanceof import_data.DataError) throw error;
|
|
178
|
-
}
|
|
179
|
-
if (response.status === 400) throw import_data.createError.badRequest("No file provided for upload.");
|
|
180
|
-
if (response.status === 413) throw import_data.createError.badRequest("File is too large. Maximum size is 10 MB.");
|
|
181
|
-
throw import_data.createError.badRequest(`Upload failed: ${response.status} ${response.statusText}`);
|
|
182
|
-
}
|
|
183
|
-
const result = await response.json();
|
|
184
|
-
if (result.errors?.length) {
|
|
185
|
-
const graphqlError = parseGraphQLResponse(result, config.onAuthError);
|
|
186
|
-
if (graphqlError) {
|
|
187
|
-
throw graphqlError;
|
|
466
|
+
const bucketKey = config.upload?.bucketKey ?? "public";
|
|
467
|
+
const contentType = file.type || "application/octet-stream";
|
|
468
|
+
const buffer = await file.arrayBuffer();
|
|
469
|
+
const contentHash = await sha256HexFromBuffer(buffer);
|
|
470
|
+
const cap = await discoverUploadCapability(config.endpoint, execute);
|
|
471
|
+
const payload = await callUploadOp(execute, cap, {
|
|
472
|
+
bucketKey,
|
|
473
|
+
contentHash,
|
|
474
|
+
contentType,
|
|
475
|
+
size: file.size,
|
|
476
|
+
filename: file.name
|
|
477
|
+
});
|
|
478
|
+
if (!payload.deduplicated) {
|
|
479
|
+
if (!payload.uploadUrl) {
|
|
480
|
+
throw import_data2.createError.badRequest("Upload failed: server returned no uploadUrl for a non-deduplicated file.");
|
|
188
481
|
}
|
|
189
|
-
|
|
190
|
-
|
|
482
|
+
await presignedPut(payload.uploadUrl, buffer, contentType, options?.onProgress);
|
|
483
|
+
} else {
|
|
484
|
+
options?.onProgress?.({ loaded: file.size, total: file.size, percentage: 100 });
|
|
191
485
|
}
|
|
192
|
-
|
|
486
|
+
const url = await fetchDownloadUrl(execute, cap, payload.fileId);
|
|
487
|
+
return {
|
|
488
|
+
url,
|
|
489
|
+
filename: file.name,
|
|
490
|
+
mime: file.type,
|
|
491
|
+
size: file.size,
|
|
492
|
+
key: payload.key,
|
|
493
|
+
fileId: payload.fileId
|
|
494
|
+
};
|
|
193
495
|
};
|
|
194
496
|
}
|
|
195
497
|
|
|
@@ -504,11 +806,11 @@ function shallowEqualArrays(a, b) {
|
|
|
504
806
|
|
|
505
807
|
// src/store/relation-info-slice.ts
|
|
506
808
|
var import_inflekt2 = require("inflekt");
|
|
507
|
-
var
|
|
809
|
+
var import_data5 = require("@constructive-io/data");
|
|
508
810
|
|
|
509
811
|
// src/utils/relation-utils.ts
|
|
510
812
|
var import_inflekt = require("inflekt");
|
|
511
|
-
var
|
|
813
|
+
var import_data4 = require("@constructive-io/data");
|
|
512
814
|
function isJunctionTablePure(junctionTableName, leftFkField, rightFkField, metaTables) {
|
|
513
815
|
if (!leftFkField || !rightFkField) return true;
|
|
514
816
|
const junctionMeta = metaTables.find((t) => t?.name === junctionTableName);
|
|
@@ -516,7 +818,7 @@ function isJunctionTablePure(junctionTableName, leftFkField, rightFkField, metaT
|
|
|
516
818
|
const exemptFields = /* @__PURE__ */ new Set(["id", leftFkField, rightFkField]);
|
|
517
819
|
for (const field of junctionMeta.fields) {
|
|
518
820
|
if (!field?.name) continue;
|
|
519
|
-
const camelName = (0,
|
|
821
|
+
const camelName = (0, import_data4.pgFieldToCamelCase)(field.name);
|
|
520
822
|
if (exemptFields.has(camelName)) continue;
|
|
521
823
|
if (field.isNotNull && !field.hasDefault) return false;
|
|
522
824
|
}
|
|
@@ -575,7 +877,7 @@ function createRelationInfoSlice(set, get) {
|
|
|
575
877
|
}
|
|
576
878
|
function toCleanTables(meta) {
|
|
577
879
|
const tables = meta?._meta?.tables ?? [];
|
|
578
|
-
return tables.filter((table) => table != null).map((table) => (0,
|
|
880
|
+
return tables.filter((table) => table != null).map((table) => (0, import_data5.cleanTable)(table));
|
|
579
881
|
}
|
|
580
882
|
function firstFieldName(fields) {
|
|
581
883
|
return fields[0]?.name;
|
|
@@ -597,7 +899,7 @@ function findForeignKeyInRelatedTable(currentTableName, relatedTableName, allTab
|
|
|
597
899
|
const refName = fk.refTable?.name;
|
|
598
900
|
if (refName && refName.toLowerCase() === currentTableName.toLowerCase()) {
|
|
599
901
|
const fieldName = fk.fields?.[0]?.name;
|
|
600
|
-
if (fieldName) return (0,
|
|
902
|
+
if (fieldName) return (0, import_data5.pgFieldToCamelCase)(fieldName);
|
|
601
903
|
}
|
|
602
904
|
}
|
|
603
905
|
}
|
|
@@ -873,7 +1175,7 @@ function documentToString2(document) {
|
|
|
873
1175
|
if (typeof document === "string") return document;
|
|
874
1176
|
if (document instanceof String) return document.toString();
|
|
875
1177
|
if (document && typeof document === "object" && "kind" in document) {
|
|
876
|
-
return (0,
|
|
1178
|
+
return (0, import_graphql3.print)(document);
|
|
877
1179
|
}
|
|
878
1180
|
return String(document ?? "");
|
|
879
1181
|
}
|
|
@@ -897,16 +1199,16 @@ function buildMetaTable(table) {
|
|
|
897
1199
|
name: table.name,
|
|
898
1200
|
schemaName: "public",
|
|
899
1201
|
query: {
|
|
900
|
-
all: (0,
|
|
901
|
-
create: (0,
|
|
902
|
-
delete: (0,
|
|
903
|
-
one: (0,
|
|
904
|
-
update: (0,
|
|
1202
|
+
all: (0, import_data6.toCamelCasePlural)(table.name),
|
|
1203
|
+
create: (0, import_data6.toCreateMutationName)(table.name),
|
|
1204
|
+
delete: (0, import_data6.toDeleteMutationName)(table.name),
|
|
1205
|
+
one: (0, import_data6.toCamelCaseSingular)(table.name),
|
|
1206
|
+
update: (0, import_data6.toUpdateMutationName)(table.name)
|
|
905
1207
|
},
|
|
906
1208
|
fields: table.fields.map(buildMetaField),
|
|
907
1209
|
inflection: {
|
|
908
|
-
allRows: (0,
|
|
909
|
-
connection: (0,
|
|
1210
|
+
allRows: (0, import_data6.toCamelCasePlural)(table.name),
|
|
1211
|
+
connection: (0, import_data6.toCamelCasePlural)(table.name),
|
|
910
1212
|
tableType: table.name
|
|
911
1213
|
},
|
|
912
1214
|
indexes: [],
|
|
@@ -925,7 +1227,7 @@ function buildMetaResponse(tables) {
|
|
|
925
1227
|
};
|
|
926
1228
|
}
|
|
927
1229
|
function buildListResponse(table) {
|
|
928
|
-
const field = (0,
|
|
1230
|
+
const field = (0, import_data6.toCamelCasePlural)(table.name);
|
|
929
1231
|
return {
|
|
930
1232
|
[field]: {
|
|
931
1233
|
nodes: table.rows,
|
|
@@ -985,13 +1287,13 @@ function findMutationTable(query, tables) {
|
|
|
985
1287
|
let best = null;
|
|
986
1288
|
for (const table of tables) {
|
|
987
1289
|
const candidates = [
|
|
988
|
-
{ op: "create", mutationName: (0,
|
|
989
|
-
{ op: "update", mutationName: (0,
|
|
990
|
-
{ op: "delete", mutationName: (0,
|
|
1290
|
+
{ op: "create", mutationName: (0, import_data6.toCreateMutationName)(table.name) },
|
|
1291
|
+
{ op: "update", mutationName: (0, import_data6.toUpdateMutationName)(table.name) },
|
|
1292
|
+
{ op: "delete", mutationName: (0, import_data6.toDeleteMutationName)(table.name) }
|
|
991
1293
|
];
|
|
992
1294
|
for (const c of candidates) {
|
|
993
1295
|
if (query.includes(c.mutationName) && (!best || c.mutationName.length > best.mutationName.length)) {
|
|
994
|
-
best = { table, op: c.op, mutationName: c.mutationName, singular: (0,
|
|
1296
|
+
best = { table, op: c.op, mutationName: c.mutationName, singular: (0, import_data6.toCamelCaseSingular)(table.name) };
|
|
995
1297
|
}
|
|
996
1298
|
}
|
|
997
1299
|
}
|
|
@@ -1001,7 +1303,7 @@ function findListTable(query, tables) {
|
|
|
1001
1303
|
let best = null;
|
|
1002
1304
|
let bestLen = 0;
|
|
1003
1305
|
for (const table of tables) {
|
|
1004
|
-
const plural = (0,
|
|
1306
|
+
const plural = (0, import_data6.toCamelCasePlural)(table.name);
|
|
1005
1307
|
if (query.includes(plural) && plural.length > bestLen) {
|
|
1006
1308
|
best = table;
|
|
1007
1309
|
bestLen = plural.length;
|