@lumibase/sdk 2.1.0 → 2.2.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/index.cjs +772 -20
- package/dist/index.d.cts +401 -6
- package/dist/index.d.ts +401 -6
- package/dist/index.js +762 -20
- package/package.json +5 -2
package/dist/index.cjs
CHANGED
|
@@ -24,8 +24,12 @@ __export(index_exports, {
|
|
|
24
24
|
AudienceClient: () => AudienceClient,
|
|
25
25
|
DECISION_ERROR_CODES: () => DECISION_ERROR_CODES,
|
|
26
26
|
DEFAULT_IMPORT_SOURCE: () => DEFAULT_IMPORT_SOURCE,
|
|
27
|
+
DEFAULT_ZOD_IMPORT: () => DEFAULT_ZOD_IMPORT,
|
|
28
|
+
FILES_INTERFACE: () => FILES_INTERFACE,
|
|
29
|
+
FILE_INTERFACE: () => FILE_INTERFACE,
|
|
27
30
|
LumiError: () => LumiError,
|
|
28
31
|
RealtimeClient: () => RealtimeClient,
|
|
32
|
+
TRANSLATABLE_INTERFACE: () => TRANSLATABLE_INTERFACE,
|
|
29
33
|
ackCdcSubscription: () => ackCdcSubscription,
|
|
30
34
|
checkCdcPipelineHealth: () => checkCdcPipelineHealth,
|
|
31
35
|
createAgentArtifact: () => createAgentArtifact,
|
|
@@ -46,8 +50,12 @@ __export(index_exports, {
|
|
|
46
50
|
evaluateAgentArtifact: () => evaluateAgentArtifact,
|
|
47
51
|
exportAccessManifest: () => exportAccessManifest,
|
|
48
52
|
extractSeo: () => extractSeo,
|
|
53
|
+
filterManifest: () => filterManifest,
|
|
49
54
|
generateAgentApp: () => generateAgentApp,
|
|
55
|
+
generateGraphQLSdl: () => generateGraphQLSdl,
|
|
56
|
+
generateOpenApi: () => generateOpenApi,
|
|
50
57
|
generateTypes: () => generateTypes,
|
|
58
|
+
generateZod: () => generateZod,
|
|
51
59
|
getEffectivePreset: () => getEffectivePreset,
|
|
52
60
|
getReview: () => getReview,
|
|
53
61
|
getReviewProfile: () => getReviewProfile,
|
|
@@ -76,7 +84,9 @@ __export(index_exports, {
|
|
|
76
84
|
readCdcSubscription: () => readCdcSubscription,
|
|
77
85
|
readItem: () => readItem,
|
|
78
86
|
readItems: () => readItems,
|
|
87
|
+
realtimeHolders: () => realtimeHolders,
|
|
79
88
|
replayCdcSubscription: () => replayCdcSubscription,
|
|
89
|
+
retainRealtime: () => retainRealtime,
|
|
80
90
|
retryAgentRun: () => retryAgentRun,
|
|
81
91
|
reviewContentHash: () => reviewContentHash,
|
|
82
92
|
reviewDraft: () => reviewDraft,
|
|
@@ -434,6 +444,29 @@ var AudienceClient = class {
|
|
|
434
444
|
}
|
|
435
445
|
};
|
|
436
446
|
|
|
447
|
+
// src/realtime/retain.ts
|
|
448
|
+
var holders = /* @__PURE__ */ new WeakMap();
|
|
449
|
+
function realtimeHolders(client) {
|
|
450
|
+
return holders.get(client) ?? 0;
|
|
451
|
+
}
|
|
452
|
+
function retainRealtime(client) {
|
|
453
|
+
const count = realtimeHolders(client);
|
|
454
|
+
holders.set(client, count + 1);
|
|
455
|
+
if (count === 0) client.connect();
|
|
456
|
+
let released = false;
|
|
457
|
+
return () => {
|
|
458
|
+
if (released) return;
|
|
459
|
+
released = true;
|
|
460
|
+
const next = realtimeHolders(client) - 1;
|
|
461
|
+
if (next <= 0) {
|
|
462
|
+
holders.delete(client);
|
|
463
|
+
client.disconnect();
|
|
464
|
+
} else {
|
|
465
|
+
holders.set(client, next);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
437
470
|
// src/realtime/index.ts
|
|
438
471
|
var RealtimeClient = class {
|
|
439
472
|
constructor(opts) {
|
|
@@ -636,6 +669,10 @@ var RealtimeClient = class {
|
|
|
636
669
|
};
|
|
637
670
|
|
|
638
671
|
// src/rest/legacy.ts
|
|
672
|
+
function ifMatchInit(options) {
|
|
673
|
+
if (options?.ifMatch === void 0) return {};
|
|
674
|
+
return { headers: { "if-match": `"${options.ifMatch}"` } };
|
|
675
|
+
}
|
|
639
676
|
function withQuery(path, params = {}) {
|
|
640
677
|
const qs = new URLSearchParams();
|
|
641
678
|
for (const [key, value] of Object.entries(params)) {
|
|
@@ -756,7 +793,19 @@ function legacyRest() {
|
|
|
756
793
|
setFormLayout: (name, layout) => client.rawRequest(
|
|
757
794
|
`/api/v1/collections/${name}/form-layout`,
|
|
758
795
|
{ method: "PUT", body: JSON.stringify({ layout }) }
|
|
759
|
-
)
|
|
796
|
+
),
|
|
797
|
+
/**
|
|
798
|
+
* Expression index advice for filtering on a JSON path (#729). `cast`
|
|
799
|
+
* defaults to the declared type of a top-level field; nested paths
|
|
800
|
+
* must name it. Read-only: the DDL is returned, never executed.
|
|
801
|
+
*/
|
|
802
|
+
indexAdvice: (name, path, cast) => {
|
|
803
|
+
const params = new URLSearchParams({ path });
|
|
804
|
+
if (cast) params.set("cast", cast);
|
|
805
|
+
return client.rawRequest(
|
|
806
|
+
`/api/v1/collections/${encodeURIComponent(name)}/index-advice?${params.toString()}`
|
|
807
|
+
);
|
|
808
|
+
}
|
|
760
809
|
};
|
|
761
810
|
function items(name) {
|
|
762
811
|
function buildQuery(params) {
|
|
@@ -769,6 +818,8 @@ function legacyRest() {
|
|
|
769
818
|
if (params.offset !== void 0)
|
|
770
819
|
qs.set("offset", String(params.offset));
|
|
771
820
|
if (params.status) qs.set("status", params.status);
|
|
821
|
+
if (params.publicationState) qs.set("publicationState", params.publicationState);
|
|
822
|
+
if (params.modifiedSincePublish) qs.set("modifiedSincePublish", "true");
|
|
772
823
|
const s = qs.toString();
|
|
773
824
|
return s ? `?${s}` : "";
|
|
774
825
|
}
|
|
@@ -787,12 +838,14 @@ function legacyRest() {
|
|
|
787
838
|
method: "POST",
|
|
788
839
|
body: JSON.stringify(input)
|
|
789
840
|
}),
|
|
790
|
-
patch: (id, input) => client.rawRequest(`${base}/${id}`, {
|
|
841
|
+
patch: (id, input, options) => client.rawRequest(`${base}/${id}`, {
|
|
791
842
|
method: "PATCH",
|
|
792
|
-
body: JSON.stringify(input)
|
|
843
|
+
body: JSON.stringify(input),
|
|
844
|
+
...ifMatchInit(options)
|
|
793
845
|
}),
|
|
794
|
-
replace: (id, input) => client.rawRequest(`${base}/${id}`, {
|
|
846
|
+
replace: (id, input, options) => client.rawRequest(`${base}/${id}`, {
|
|
795
847
|
method: "PUT",
|
|
848
|
+
...ifMatchInit(options),
|
|
796
849
|
body: JSON.stringify(input)
|
|
797
850
|
}),
|
|
798
851
|
delete: (id) => client.rawRequest(`${base}/${id}`, { method: "DELETE" }),
|
|
@@ -817,6 +870,30 @@ function legacyRest() {
|
|
|
817
870
|
method: "POST",
|
|
818
871
|
body: JSON.stringify({ op, items: payload })
|
|
819
872
|
}),
|
|
873
|
+
/**
|
|
874
|
+
* Save-as-copy (#689): a draft copy with unique fields suffixed
|
|
875
|
+
* (`-copy`, `-copy-2`, …). Goes through the normal create path.
|
|
876
|
+
*/
|
|
877
|
+
duplicate: (id, options = {}) => client.rawRequest(`${base}/${encodeURIComponent(id)}/duplicate`, {
|
|
878
|
+
method: "POST",
|
|
879
|
+
body: JSON.stringify(options)
|
|
880
|
+
}),
|
|
881
|
+
/**
|
|
882
|
+
* Manual ordering (#690) on a `meta.manualSort` collection: place `id`
|
|
883
|
+
* right after `after` (null = first) or right before `before`.
|
|
884
|
+
*/
|
|
885
|
+
reorder: (id, target) => client.rawRequest(`${base}/${encodeURIComponent(id)}/reorder`, {
|
|
886
|
+
method: "POST",
|
|
887
|
+
body: JSON.stringify(target)
|
|
888
|
+
}),
|
|
889
|
+
/**
|
|
890
|
+
* Patch without recording a revision (#693) — for autosave and
|
|
891
|
+
* machine writes. Agent-authored writes keep their revision anyway.
|
|
892
|
+
*/
|
|
893
|
+
patchWithoutRevision: (id, input) => client.rawRequest(`${base}/${id}?saveRevision=false`, {
|
|
894
|
+
method: "PATCH",
|
|
895
|
+
body: JSON.stringify(input)
|
|
896
|
+
}),
|
|
820
897
|
/**
|
|
821
898
|
* Hierarchical collections (#749) on a `meta.tree.enabled` collection.
|
|
822
899
|
* Reads apply the caller's row rules; the anchor must be readable.
|
|
@@ -1101,10 +1178,66 @@ function legacyRest() {
|
|
|
1101
1178
|
method: "PATCH",
|
|
1102
1179
|
body: JSON.stringify(patch)
|
|
1103
1180
|
}),
|
|
1104
|
-
delete: (id) => client.rawRequest(`/api/v1/files/folders/${id}`, { method: "DELETE" })
|
|
1181
|
+
delete: (id) => client.rawRequest(`/api/v1/files/folders/${id}`, { method: "DELETE" }),
|
|
1182
|
+
/** Nested folder tree with id paths (`/a/b/`) and name paths (#701). */
|
|
1183
|
+
tree: () => client.rawRequest("/api/v1/files/folders/tree")
|
|
1105
1184
|
};
|
|
1106
1185
|
const files = {
|
|
1107
|
-
|
|
1186
|
+
/**
|
|
1187
|
+
* List / filter the library. Without a query: the first 1000 files,
|
|
1188
|
+
* newest first. `meta` carries `{ total, limit, offset }`.
|
|
1189
|
+
*/
|
|
1190
|
+
list: (query) => {
|
|
1191
|
+
const q = new URLSearchParams();
|
|
1192
|
+
if (query?.folder !== void 0) q.set("folder", query.folder);
|
|
1193
|
+
if (query?.subfolders) q.set("subfolders", "true");
|
|
1194
|
+
if (query?.search) q.set("search", query.search);
|
|
1195
|
+
if (query?.mime?.length) q.set("mime", query.mime.join(","));
|
|
1196
|
+
if (query?.ids?.length) q.set("ids", query.ids.join(","));
|
|
1197
|
+
if (query?.limit !== void 0) q.set("limit", String(query.limit));
|
|
1198
|
+
if (query?.offset !== void 0) q.set("offset", String(query.offset));
|
|
1199
|
+
const qs = q.toString();
|
|
1200
|
+
return client.rawRequest(`/api/v1/files${qs ? `?${qs}` : ""}`);
|
|
1201
|
+
},
|
|
1202
|
+
get: (id) => client.rawRequest(`/api/v1/files/${encodeURIComponent(id)}`),
|
|
1203
|
+
/**
|
|
1204
|
+
* Replace the bytes behind a file in place (#703): same id, key, folder
|
|
1205
|
+
* and references; `version` bumps. Pass `expectedVersion` to refuse
|
|
1206
|
+
* (409) when someone replaced it since you loaded it.
|
|
1207
|
+
*/
|
|
1208
|
+
replaceContent: (id, body, opts) => {
|
|
1209
|
+
const q = opts.filename ? `?filename=${encodeURIComponent(opts.filename)}` : "";
|
|
1210
|
+
const headers = { "content-type": opts.mime };
|
|
1211
|
+
if (opts.expectedVersion !== void 0) headers["if-match"] = `"v${opts.expectedVersion}"`;
|
|
1212
|
+
return client.rawRequest(`/api/v1/files/${encodeURIComponent(id)}/content${q}`, {
|
|
1213
|
+
method: "PUT",
|
|
1214
|
+
body,
|
|
1215
|
+
headers
|
|
1216
|
+
});
|
|
1217
|
+
},
|
|
1218
|
+
/** URL that serves the current bytes; pass `version` for an immutable, cacheable URL. */
|
|
1219
|
+
contentUrl: (id, version) => `${client.url.replace(/\/$/, "")}/api/v1/files/${encodeURIComponent(id)}/content${version !== void 0 ? `?v=${version}` : ""}`,
|
|
1220
|
+
versions: (id) => client.rawRequest(`/api/v1/files/${encodeURIComponent(id)}/versions`),
|
|
1221
|
+
restoreVersion: (id, versionId) => client.rawRequest(
|
|
1222
|
+
`/api/v1/files/${encodeURIComponent(id)}/versions/${encodeURIComponent(versionId)}/restore`,
|
|
1223
|
+
{ method: "POST" }
|
|
1224
|
+
),
|
|
1225
|
+
/** Items and pages that reference the file (#706). */
|
|
1226
|
+
usage: (id) => client.rawRequest(`/api/v1/files/${encodeURIComponent(id)}/usage`),
|
|
1227
|
+
/** Files with the same content hash (lowercase hex SHA-256). */
|
|
1228
|
+
duplicates: (hash) => client.rawRequest(`/api/v1/files/duplicates?hash=${encodeURIComponent(hash)}`),
|
|
1229
|
+
/** Bulk move and/or merge editorial metadata (alt, title, caption, credit, tags). */
|
|
1230
|
+
bulk: (input) => client.rawRequest("/api/v1/files/bulk", {
|
|
1231
|
+
method: "POST",
|
|
1232
|
+
body: JSON.stringify(input)
|
|
1233
|
+
}),
|
|
1234
|
+
settings: {
|
|
1235
|
+
get: () => client.rawRequest("/api/v1/files/settings"),
|
|
1236
|
+
update: (patch) => client.rawRequest("/api/v1/files/settings", {
|
|
1237
|
+
method: "PUT",
|
|
1238
|
+
body: JSON.stringify(patch)
|
|
1239
|
+
})
|
|
1240
|
+
},
|
|
1108
1241
|
create: (input) => client.rawRequest("/api/v1/files", {
|
|
1109
1242
|
method: "POST",
|
|
1110
1243
|
body: JSON.stringify(input)
|
|
@@ -1113,7 +1246,12 @@ function legacyRest() {
|
|
|
1113
1246
|
method: "PATCH",
|
|
1114
1247
|
body: JSON.stringify(patch)
|
|
1115
1248
|
}),
|
|
1116
|
-
|
|
1249
|
+
/**
|
|
1250
|
+
* Safe delete (#706): a referenced file answers 409 `FILE_IN_USE` with the
|
|
1251
|
+
* references in `errors[0].details.usage`; repeat with `{ confirm: true }`
|
|
1252
|
+
* to delete anyway (unless the site's policy is `block`).
|
|
1253
|
+
*/
|
|
1254
|
+
delete: (id, opts) => client.rawRequest(`/api/v1/files/${id}${opts?.confirm ? "?confirm=true" : ""}`, { method: "DELETE" }),
|
|
1117
1255
|
getPresignedUrl: (filename) => client.rawRequest("/api/v1/files/presigned-url", {
|
|
1118
1256
|
method: "POST",
|
|
1119
1257
|
body: JSON.stringify({ filename })
|
|
@@ -1244,6 +1382,23 @@ function legacyRest() {
|
|
|
1244
1382
|
target: { type: "api_key", id }
|
|
1245
1383
|
})
|
|
1246
1384
|
};
|
|
1385
|
+
const preview = {
|
|
1386
|
+
mintToken: (input) => client.rawRequest("/api/v1/preview/token", {
|
|
1387
|
+
method: "POST",
|
|
1388
|
+
body: JSON.stringify(input)
|
|
1389
|
+
}),
|
|
1390
|
+
listTokens: () => client.rawRequest("/api/v1/preview/tokens"),
|
|
1391
|
+
revokeToken: (id) => client.rawRequest(`/api/v1/preview/tokens/${encodeURIComponent(id)}/revoke`, {
|
|
1392
|
+
method: "POST"
|
|
1393
|
+
}),
|
|
1394
|
+
/** Rotation: revoke every live preview token of the site (site admin). */
|
|
1395
|
+
revokeAll: () => client.rawRequest("/api/v1/preview/tokens/revoke-all", { method: "POST" }),
|
|
1396
|
+
getSettings: () => client.rawRequest("/api/v1/preview/settings"),
|
|
1397
|
+
updateSettings: (input) => client.rawRequest("/api/v1/preview/settings", {
|
|
1398
|
+
method: "PUT",
|
|
1399
|
+
body: JSON.stringify(input)
|
|
1400
|
+
})
|
|
1401
|
+
};
|
|
1247
1402
|
const shares = {
|
|
1248
1403
|
create: (input) => client.rawRequest("/api/v1/shares", {
|
|
1249
1404
|
method: "POST",
|
|
@@ -1361,6 +1516,7 @@ function legacyRest() {
|
|
|
1361
1516
|
access,
|
|
1362
1517
|
apiKeys,
|
|
1363
1518
|
shares,
|
|
1519
|
+
preview,
|
|
1364
1520
|
me,
|
|
1365
1521
|
permissions,
|
|
1366
1522
|
presets,
|
|
@@ -1656,7 +1812,7 @@ function readItem(collection, id, fields) {
|
|
|
1656
1812
|
const res = await client.rawRequest(
|
|
1657
1813
|
`/api/v1/items/${collection}/${id}${fields?.length ? `?fields=${fields.join(",")}` : ""}`
|
|
1658
1814
|
);
|
|
1659
|
-
return res;
|
|
1815
|
+
return res.data;
|
|
1660
1816
|
};
|
|
1661
1817
|
}
|
|
1662
1818
|
function exportAccessManifest() {
|
|
@@ -2038,6 +2194,583 @@ function graphql(endpoint = "/api/v1/graphql") {
|
|
|
2038
2194
|
};
|
|
2039
2195
|
}
|
|
2040
2196
|
|
|
2197
|
+
// src/typegen/shared.ts
|
|
2198
|
+
var TRANSLATABLE_INTERFACE = "translatable-text";
|
|
2199
|
+
var FILE_INTERFACE = "file";
|
|
2200
|
+
var FILES_INTERFACE = "files";
|
|
2201
|
+
function fieldShape(field) {
|
|
2202
|
+
if (field.kind === "m2o") return { kind: "m2o" };
|
|
2203
|
+
if (field.kind === "o2m" || field.kind === "m2m") {
|
|
2204
|
+
return { kind: "to-many", target: field.target || "unknown" };
|
|
2205
|
+
}
|
|
2206
|
+
if (field.kind === "m2a") return { kind: "m2a" };
|
|
2207
|
+
if (field.interface === TRANSLATABLE_INTERFACE) return { kind: "localized" };
|
|
2208
|
+
if (field.interface === FILES_INTERFACE || field.type === "files") return { kind: "files" };
|
|
2209
|
+
if (field.interface === FILE_INTERFACE) return { kind: "file" };
|
|
2210
|
+
if (field.enum && field.enum.length > 0) return { kind: "enum", values: field.enum };
|
|
2211
|
+
if (field.primaryKey) return { kind: "primary-key" };
|
|
2212
|
+
return { kind: "scalar" };
|
|
2213
|
+
}
|
|
2214
|
+
function isOptional(field) {
|
|
2215
|
+
return !field.required;
|
|
2216
|
+
}
|
|
2217
|
+
function isNullable(field) {
|
|
2218
|
+
return !field.required || field.nullable;
|
|
2219
|
+
}
|
|
2220
|
+
function hasIntegerPrimaryKey(collection) {
|
|
2221
|
+
return collection?.primaryKeyType === "integer" || collection?.primaryKeyType === "bigInteger";
|
|
2222
|
+
}
|
|
2223
|
+
function relationsOf(collection) {
|
|
2224
|
+
return collection.relations ?? [];
|
|
2225
|
+
}
|
|
2226
|
+
function capitalize(str) {
|
|
2227
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2228
|
+
}
|
|
2229
|
+
var IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
2230
|
+
function propertyKey(name) {
|
|
2231
|
+
return IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
2232
|
+
}
|
|
2233
|
+
function filterManifest(manifest, filters) {
|
|
2234
|
+
const { include, exclude } = filters;
|
|
2235
|
+
if (!include?.length && !exclude?.length) return manifest;
|
|
2236
|
+
return {
|
|
2237
|
+
...manifest,
|
|
2238
|
+
collections: manifest.collections.filter(
|
|
2239
|
+
(collection) => (!include?.length || include.includes(collection.name)) && !(exclude?.length && exclude.includes(collection.name))
|
|
2240
|
+
)
|
|
2241
|
+
};
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2244
|
+
// src/typegen/richtext.ts
|
|
2245
|
+
var RICHTEXT_TS_DECLARATIONS = `export type RichTextMark = 'bold' | 'italic' | 'underline' | 'strike' | 'code' | 'superscript' | 'subscript';
|
|
2246
|
+
|
|
2247
|
+
export interface RichTextRef {
|
|
2248
|
+
kind: 'item' | 'file';
|
|
2249
|
+
collection?: string;
|
|
2250
|
+
id: string;
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
export type RichTextInline =
|
|
2254
|
+
| { type: 'text'; text: string; marks?: RichTextMark[] }
|
|
2255
|
+
| { type: 'link'; href: string; content: RichTextInline[] }
|
|
2256
|
+
| { type: 'ref'; ref: RichTextRef; content: RichTextInline[] }
|
|
2257
|
+
| { type: 'embed'; ref: RichTextRef; alt?: string }
|
|
2258
|
+
| { type: 'break' }
|
|
2259
|
+
| { type: 'footnote'; label: string };
|
|
2260
|
+
|
|
2261
|
+
export interface RichTextTableRow {
|
|
2262
|
+
header?: boolean;
|
|
2263
|
+
cells: RichTextBlock[][];
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
export type RichTextBlock =
|
|
2267
|
+
| { type: 'paragraph'; content: RichTextInline[] }
|
|
2268
|
+
| { type: 'heading'; level: 1 | 2 | 3 | 4 | 5 | 6; content: RichTextInline[] }
|
|
2269
|
+
| { type: 'quote'; content: RichTextBlock[] }
|
|
2270
|
+
| { type: 'list'; ordered: boolean; start?: number; items: RichTextBlock[][] }
|
|
2271
|
+
| { type: 'code'; language?: string; text: string }
|
|
2272
|
+
| { type: 'rule' }
|
|
2273
|
+
| { type: 'embed'; ref: RichTextRef; alt?: string }
|
|
2274
|
+
| { type: 'table'; rows: RichTextTableRow[]; caption?: string; headerColumn?: boolean; align?: Array<'left' | 'center' | 'right' | null> }
|
|
2275
|
+
| { type: 'footnote'; label: string; content: RichTextBlock[] }
|
|
2276
|
+
| { type: 'custom'; name: string; data: Record<string, unknown> };
|
|
2277
|
+
|
|
2278
|
+
export interface RichTextDocument {
|
|
2279
|
+
type: 'doc';
|
|
2280
|
+
version: 1;
|
|
2281
|
+
content: RichTextBlock[];
|
|
2282
|
+
}`;
|
|
2283
|
+
var RICHTEXT_ZOD = "z.object({ type: z.literal('doc'), version: z.literal(1), content: z.array(z.record(z.string(), z.unknown())) })";
|
|
2284
|
+
|
|
2285
|
+
// src/typegen/zod.ts
|
|
2286
|
+
var DEFAULT_ZOD_IMPORT = "zod";
|
|
2287
|
+
function generateZod(manifest, options = {}) {
|
|
2288
|
+
const importSource = options.importSource ?? DEFAULT_ZOD_IMPORT;
|
|
2289
|
+
const byName = new Map(manifest.collections.map((collection) => [collection.name, collection]));
|
|
2290
|
+
const blocks = manifest.collections.map((collection) => {
|
|
2291
|
+
const name = capitalize(collection.name);
|
|
2292
|
+
const props = collection.fields.map((field) => ` ${propertyKey(field.name)}: ${fieldValidator(field, collection, byName)},`).join("\n");
|
|
2293
|
+
return [
|
|
2294
|
+
`export const ${name}Schema = z.object({`,
|
|
2295
|
+
props,
|
|
2296
|
+
"});",
|
|
2297
|
+
`export type ${name} = z.infer<typeof ${name}Schema>;`
|
|
2298
|
+
].filter((line) => line !== "").join("\n");
|
|
2299
|
+
});
|
|
2300
|
+
const map = [
|
|
2301
|
+
"export const LumibaseSchemas = {",
|
|
2302
|
+
...manifest.collections.map(
|
|
2303
|
+
(collection) => ` ${propertyKey(collection.name)}: ${capitalize(collection.name)}Schema,`
|
|
2304
|
+
),
|
|
2305
|
+
"} as const;"
|
|
2306
|
+
].join("\n");
|
|
2307
|
+
return [`import { z } from ${JSON.stringify(importSource)};`, ...blocks, map].join("\n\n");
|
|
2308
|
+
}
|
|
2309
|
+
function fieldValidator(field, collection, byName) {
|
|
2310
|
+
const shape = fieldShape(field);
|
|
2311
|
+
let schema;
|
|
2312
|
+
switch (shape.kind) {
|
|
2313
|
+
case "to-many": {
|
|
2314
|
+
const target = byName.get(shape.target);
|
|
2315
|
+
const id = target ? primaryKeyValidator(target) : "z.union([z.string(), z.number()])";
|
|
2316
|
+
return withOptional(field, `z.array(z.union([${id}, z.record(z.string(), z.unknown())]))`);
|
|
2317
|
+
}
|
|
2318
|
+
case "m2a":
|
|
2319
|
+
return withOptional(
|
|
2320
|
+
field,
|
|
2321
|
+
"z.array(z.object({ collection: z.string(), item: z.unknown() }))"
|
|
2322
|
+
);
|
|
2323
|
+
case "localized":
|
|
2324
|
+
schema = "z.record(z.string(), z.string())";
|
|
2325
|
+
break;
|
|
2326
|
+
case "file":
|
|
2327
|
+
schema = "z.string()";
|
|
2328
|
+
break;
|
|
2329
|
+
case "files":
|
|
2330
|
+
schema = "z.array(z.string())";
|
|
2331
|
+
break;
|
|
2332
|
+
case "enum":
|
|
2333
|
+
return wrap(field, `z.enum(${JSON.stringify(shape.values)})`);
|
|
2334
|
+
case "primary-key":
|
|
2335
|
+
return wrap(field, primaryKeyValidator(collection));
|
|
2336
|
+
case "m2o":
|
|
2337
|
+
return wrap(field, scalarValidator(field.type));
|
|
2338
|
+
case "scalar":
|
|
2339
|
+
schema = scalarValidator(field.type);
|
|
2340
|
+
break;
|
|
2341
|
+
}
|
|
2342
|
+
if (field.encrypted) schema = `z.union([${schema}, z.literal('***')])`;
|
|
2343
|
+
return wrap(field, schema);
|
|
2344
|
+
}
|
|
2345
|
+
function primaryKeyValidator(collection) {
|
|
2346
|
+
const type = collection.primaryKeyType ?? "nanoid";
|
|
2347
|
+
return type === "integer" || type === "bigInteger" ? "z.number().int()" : "z.string()";
|
|
2348
|
+
}
|
|
2349
|
+
function scalarValidator(type) {
|
|
2350
|
+
switch (type) {
|
|
2351
|
+
case "string":
|
|
2352
|
+
case "text":
|
|
2353
|
+
case "hash":
|
|
2354
|
+
case "csv":
|
|
2355
|
+
case "uuid":
|
|
2356
|
+
case "date":
|
|
2357
|
+
case "datetime":
|
|
2358
|
+
case "dateTime":
|
|
2359
|
+
case "time":
|
|
2360
|
+
case "timestamp":
|
|
2361
|
+
return "z.string()";
|
|
2362
|
+
case "integer":
|
|
2363
|
+
return "z.number().int()";
|
|
2364
|
+
case "bigInteger":
|
|
2365
|
+
case "decimal":
|
|
2366
|
+
case "float":
|
|
2367
|
+
return "z.number()";
|
|
2368
|
+
case "boolean":
|
|
2369
|
+
return "z.boolean()";
|
|
2370
|
+
case "geometry":
|
|
2371
|
+
return "z.record(z.string(), z.unknown())";
|
|
2372
|
+
case "richtext":
|
|
2373
|
+
return RICHTEXT_ZOD;
|
|
2374
|
+
default:
|
|
2375
|
+
return "z.unknown()";
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
function wrap(field, schema) {
|
|
2379
|
+
return withOptional(field, isNullable(field) ? `${schema}.nullable()` : schema);
|
|
2380
|
+
}
|
|
2381
|
+
function withOptional(field, schema) {
|
|
2382
|
+
return isOptional(field) ? `${schema}.optional()` : schema;
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
// src/typegen/graphql-sdl.ts
|
|
2386
|
+
function generateGraphQLSdl(manifest) {
|
|
2387
|
+
const collections = manifest.collections.filter((collection) => GRAPHQL_NAME.test(collection.name));
|
|
2388
|
+
const typeNames = new Map(collections.map((collection) => [collection.name, pascalCase(collection.name)]));
|
|
2389
|
+
const blocks = ["scalar JSON", "scalar DateTime"];
|
|
2390
|
+
for (const collection of collections) {
|
|
2391
|
+
blocks.push(objectType(typeNames.get(collection.name), itemFields(collection, typeNames)));
|
|
2392
|
+
}
|
|
2393
|
+
if (collections.some((c) => c.fields.some((f) => !f.system && f.type === "richtext" && !RESERVED.has(f.name) && GRAPHQL_NAME.test(f.name)))) {
|
|
2394
|
+
blocks.push(objectType("RichText", ["json: JSON", "html: String", "markdown: String", "text: String"]));
|
|
2395
|
+
}
|
|
2396
|
+
const query = ["_collections: [String!]!"];
|
|
2397
|
+
const mutation = [];
|
|
2398
|
+
const subscription = [];
|
|
2399
|
+
for (const collection of collections) {
|
|
2400
|
+
const name = collection.name;
|
|
2401
|
+
const type = typeNames.get(name);
|
|
2402
|
+
query.push(
|
|
2403
|
+
`${name}(filter: JSON, sort: [String!], limit: Int, offset: Int, status: String, publicationState: PublicationState, modifiedSincePublish: Boolean): [${type}!]!`,
|
|
2404
|
+
`${name}_by_id(id: ID!, publicationState: PublicationState): ${type}`
|
|
2405
|
+
);
|
|
2406
|
+
mutation.push(
|
|
2407
|
+
`create_${name}(data: JSON!, status: String, sort: Int): ${type}!`,
|
|
2408
|
+
`update_${name}(id: ID!, data: JSON, status: String, sort: Int): ${type}!`,
|
|
2409
|
+
`delete_${name}(id: ID!): Boolean!`
|
|
2410
|
+
);
|
|
2411
|
+
subscription.push(`${name}_events: ItemEvent!`);
|
|
2412
|
+
}
|
|
2413
|
+
blocks.push(objectType("Query", query));
|
|
2414
|
+
if (collections.length > 0) blocks.push("enum PublicationState {\n live\n preview\n all\n}");
|
|
2415
|
+
if (mutation.length > 0) blocks.push(objectType("Mutation", mutation));
|
|
2416
|
+
if (subscription.length > 0) {
|
|
2417
|
+
blocks.push(objectType("Subscription", subscription));
|
|
2418
|
+
blocks.push(
|
|
2419
|
+
objectType("ItemEvent", [
|
|
2420
|
+
"collection: String!",
|
|
2421
|
+
"action: String!",
|
|
2422
|
+
"itemId: ID!",
|
|
2423
|
+
"item: JSON"
|
|
2424
|
+
])
|
|
2425
|
+
);
|
|
2426
|
+
}
|
|
2427
|
+
return `${blocks.join("\n\n")}
|
|
2428
|
+
`;
|
|
2429
|
+
}
|
|
2430
|
+
var STRUCTURAL = [
|
|
2431
|
+
["id", "ID!"],
|
|
2432
|
+
["status", "String"],
|
|
2433
|
+
["sort", "Int"],
|
|
2434
|
+
["createdAt", "DateTime"],
|
|
2435
|
+
["updatedAt", "DateTime"],
|
|
2436
|
+
["userCreated", "String"],
|
|
2437
|
+
["userUpdated", "String"],
|
|
2438
|
+
["_data", "JSON"],
|
|
2439
|
+
// Publication state (#687), underscored so a content field keeps its name.
|
|
2440
|
+
["_firstPublishedAt", "DateTime"],
|
|
2441
|
+
["_publishedAt", "DateTime"],
|
|
2442
|
+
["_hasUnpublishedChanges", "Boolean"]
|
|
2443
|
+
];
|
|
2444
|
+
var RESERVED = /* @__PURE__ */ new Set([
|
|
2445
|
+
"id",
|
|
2446
|
+
"status",
|
|
2447
|
+
"sort",
|
|
2448
|
+
"createdAt",
|
|
2449
|
+
"updatedAt",
|
|
2450
|
+
"userCreated",
|
|
2451
|
+
"userUpdated",
|
|
2452
|
+
"created_at",
|
|
2453
|
+
"updated_at",
|
|
2454
|
+
"user_created",
|
|
2455
|
+
"user_updated",
|
|
2456
|
+
"_data",
|
|
2457
|
+
"_firstPublishedAt",
|
|
2458
|
+
"_publishedAt",
|
|
2459
|
+
"_hasUnpublishedChanges"
|
|
2460
|
+
]);
|
|
2461
|
+
var GRAPHQL_NAME = /^[_A-Za-z][_0-9A-Za-z]*$/;
|
|
2462
|
+
function itemFields(collection, typeNames) {
|
|
2463
|
+
const fields = new Map(STRUCTURAL);
|
|
2464
|
+
for (const field of collection.fields) {
|
|
2465
|
+
if (field.system || RESERVED.has(field.name) || !GRAPHQL_NAME.test(field.name)) continue;
|
|
2466
|
+
fields.set(field.name, scalarFor(field.type));
|
|
2467
|
+
}
|
|
2468
|
+
for (const relation of relationsOf(collection)) {
|
|
2469
|
+
const nested = relationField(collection, relation, typeNames);
|
|
2470
|
+
if (!nested || !GRAPHQL_NAME.test(nested.name) || fields.has(nested.name)) continue;
|
|
2471
|
+
fields.set(nested.name, nested.args ? `${nested.args}: ${nested.type}` : nested.type);
|
|
2472
|
+
}
|
|
2473
|
+
return [...fields].map(([name, type]) => type.startsWith("(") ? `${name}${type}` : `${name}: ${type}`);
|
|
2474
|
+
}
|
|
2475
|
+
function relationField(collection, relation, typeNames) {
|
|
2476
|
+
const many = relation.manyCollection ?? (relation.kind === "m2o" ? collection.name : relation.target);
|
|
2477
|
+
const one = relation.oneCollection ?? (relation.kind === "m2o" ? relation.target : collection.name);
|
|
2478
|
+
const stored = relation.relationType ?? (many === collection.name ? relation.kind : void 0);
|
|
2479
|
+
if (stored === "m2o" && many === collection.name) {
|
|
2480
|
+
const target = typeNames.get(one);
|
|
2481
|
+
return target ? { name: relation.alias ?? m2oAlias(relation, one), type: target } : null;
|
|
2482
|
+
}
|
|
2483
|
+
if (stored === "o2m" && one === collection.name) {
|
|
2484
|
+
const target = typeNames.get(many);
|
|
2485
|
+
return target ? { name: relation.alias ?? many, args: "(limit: Int, offset: Int)", type: `[${target}!]!` } : null;
|
|
2486
|
+
}
|
|
2487
|
+
return null;
|
|
2488
|
+
}
|
|
2489
|
+
function m2oAlias(relation, oneCollection) {
|
|
2490
|
+
const manyField = relation.manyField ?? relation.field;
|
|
2491
|
+
return manyField.endsWith("_id") ? manyField.slice(0, -3) : oneCollection;
|
|
2492
|
+
}
|
|
2493
|
+
function scalarFor(type) {
|
|
2494
|
+
switch (type) {
|
|
2495
|
+
case "boolean":
|
|
2496
|
+
return "Boolean";
|
|
2497
|
+
case "integer":
|
|
2498
|
+
return "Int";
|
|
2499
|
+
case "float":
|
|
2500
|
+
case "decimal":
|
|
2501
|
+
return "Float";
|
|
2502
|
+
case "bigInteger":
|
|
2503
|
+
return "String";
|
|
2504
|
+
// `datetime` is the canonical FieldType spelling; `dateTime` is legacy (#865).
|
|
2505
|
+
case "datetime":
|
|
2506
|
+
case "dateTime":
|
|
2507
|
+
case "date":
|
|
2508
|
+
case "time":
|
|
2509
|
+
case "timestamp":
|
|
2510
|
+
return "DateTime";
|
|
2511
|
+
case "richtext":
|
|
2512
|
+
return "RichText";
|
|
2513
|
+
case "json":
|
|
2514
|
+
case "csv":
|
|
2515
|
+
case "geometry":
|
|
2516
|
+
case "files":
|
|
2517
|
+
case "m2o":
|
|
2518
|
+
case "o2m":
|
|
2519
|
+
case "m2m":
|
|
2520
|
+
case "m2a":
|
|
2521
|
+
return "JSON";
|
|
2522
|
+
default:
|
|
2523
|
+
return "String";
|
|
2524
|
+
}
|
|
2525
|
+
}
|
|
2526
|
+
function pascalCase(name) {
|
|
2527
|
+
return name.split("_").filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
2528
|
+
}
|
|
2529
|
+
function objectType(name, fields) {
|
|
2530
|
+
return `type ${name} {
|
|
2531
|
+
${fields.map((field) => ` ${field}`).join("\n")}
|
|
2532
|
+
}`;
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
// src/typegen/openapi.ts
|
|
2536
|
+
function generateOpenApi(manifest, options = {}) {
|
|
2537
|
+
const byName = new Map(manifest.collections.map((collection) => [collection.name, collection]));
|
|
2538
|
+
const bases = /* @__PURE__ */ new Map();
|
|
2539
|
+
const taken = /* @__PURE__ */ new Set();
|
|
2540
|
+
for (const collection of manifest.collections) {
|
|
2541
|
+
const raw = collection.name.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
2542
|
+
let base = raw;
|
|
2543
|
+
for (let n = 2; taken.has(base); n += 1) base = `${raw}_${n}`;
|
|
2544
|
+
taken.add(base);
|
|
2545
|
+
bases.set(collection.name, base);
|
|
2546
|
+
}
|
|
2547
|
+
const ctx = {
|
|
2548
|
+
primaryKeySchema: (name) => hasIntegerPrimaryKey(byName.get(name)) ? { type: "integer" } : { type: "string" },
|
|
2549
|
+
projectionRef: (name) => {
|
|
2550
|
+
const base = bases.get(name);
|
|
2551
|
+
return base ? `#/components/schemas/${base}.Projection` : null;
|
|
2552
|
+
}
|
|
2553
|
+
};
|
|
2554
|
+
const schemas = {
|
|
2555
|
+
ErrorObject: {
|
|
2556
|
+
type: "object",
|
|
2557
|
+
properties: { code: { type: "string" }, message: { type: "string" } },
|
|
2558
|
+
required: ["code"]
|
|
2559
|
+
},
|
|
2560
|
+
ErrorResponse: {
|
|
2561
|
+
type: "object",
|
|
2562
|
+
properties: { errors: { type: "array", items: { $ref: "#/components/schemas/ErrorObject" } } },
|
|
2563
|
+
required: ["errors"]
|
|
2564
|
+
},
|
|
2565
|
+
PaginationMeta: {
|
|
2566
|
+
type: "object",
|
|
2567
|
+
properties: {
|
|
2568
|
+
total: { type: "integer", minimum: 0, description: "Omitted when the request passed `meta=none`." },
|
|
2569
|
+
limit: { type: "integer", minimum: 1, maximum: 200 },
|
|
2570
|
+
offset: { type: "integer", minimum: 0 }
|
|
2571
|
+
},
|
|
2572
|
+
required: ["limit", "offset"]
|
|
2573
|
+
}
|
|
2574
|
+
};
|
|
2575
|
+
for (const collection of manifest.collections) {
|
|
2576
|
+
addCollectionSchemas(schemas, collection, bases.get(collection.name), ctx);
|
|
2577
|
+
}
|
|
2578
|
+
return {
|
|
2579
|
+
openapi: "3.1.0",
|
|
2580
|
+
info: {
|
|
2581
|
+
title: options.title ?? "LumiBase content schemas",
|
|
2582
|
+
version: options.version ?? `manifest-v${manifest.version}`,
|
|
2583
|
+
description: "Component schemas generated by `lumibase types` from the typegen manifest. For paths and a per-caller view, use the live document at /api/v1/openapi.json."
|
|
2584
|
+
},
|
|
2585
|
+
components: { schemas }
|
|
2586
|
+
};
|
|
2587
|
+
}
|
|
2588
|
+
var timestamp = { type: "string", format: "date-time" };
|
|
2589
|
+
var nullableTimestamp = { type: ["string", "null"], format: "date-time" };
|
|
2590
|
+
var nullableString = { type: ["string", "null"] };
|
|
2591
|
+
var expandedObject = {
|
|
2592
|
+
type: "object",
|
|
2593
|
+
description: "Expanded record (requested with `fields=<name>.*`)."
|
|
2594
|
+
};
|
|
2595
|
+
var fileId = { type: "string", description: "File id (`/api/v1/files/{id}`)." };
|
|
2596
|
+
var FIELD_TYPE_JSON_SCHEMA = {
|
|
2597
|
+
string: { type: "string" },
|
|
2598
|
+
text: { type: "string" },
|
|
2599
|
+
hash: { type: "string" },
|
|
2600
|
+
csv: { type: "string" },
|
|
2601
|
+
integer: { type: "integer" },
|
|
2602
|
+
bigInteger: { type: "integer", format: "int64" },
|
|
2603
|
+
decimal: { type: "number" },
|
|
2604
|
+
float: { type: "number" },
|
|
2605
|
+
boolean: { type: "boolean" },
|
|
2606
|
+
json: {},
|
|
2607
|
+
uuid: { type: "string", format: "uuid" },
|
|
2608
|
+
date: { type: "string", format: "date" },
|
|
2609
|
+
datetime: { type: "string", format: "date-time" },
|
|
2610
|
+
dateTime: { type: "string", format: "date-time" },
|
|
2611
|
+
time: { type: "string", format: "time" },
|
|
2612
|
+
timestamp: { type: "string", format: "date-time" },
|
|
2613
|
+
geometry: { type: "object", additionalProperties: true, description: "GeoJSON geometry object." },
|
|
2614
|
+
richtext: {
|
|
2615
|
+
type: "object",
|
|
2616
|
+
required: ["type", "version", "content"],
|
|
2617
|
+
properties: {
|
|
2618
|
+
type: { type: "string", enum: ["doc"] },
|
|
2619
|
+
version: { type: "integer", enum: [1] },
|
|
2620
|
+
content: { type: "array", items: { type: "object", additionalProperties: true } }
|
|
2621
|
+
},
|
|
2622
|
+
description: "Portable rich-text document (see the RichTextDocument type)."
|
|
2623
|
+
}
|
|
2624
|
+
};
|
|
2625
|
+
function withNull(schema) {
|
|
2626
|
+
if (Object.keys(schema).length === 0) return schema;
|
|
2627
|
+
const type = schema["type"];
|
|
2628
|
+
if (typeof type === "string" && !("enum" in schema)) return { ...schema, type: [type, "null"] };
|
|
2629
|
+
if (typeof type === "string" && Array.isArray(schema["enum"])) {
|
|
2630
|
+
return { ...schema, type: [type, "null"], enum: [...schema["enum"], null] };
|
|
2631
|
+
}
|
|
2632
|
+
return { anyOf: [schema, { type: "null" }] };
|
|
2633
|
+
}
|
|
2634
|
+
function referenceOrId(target, ctx, shape) {
|
|
2635
|
+
const id = ctx.primaryKeySchema(target);
|
|
2636
|
+
if (shape !== "projection") return id;
|
|
2637
|
+
const ref = ctx.projectionRef(target);
|
|
2638
|
+
return ref ? { anyOf: [id, { $ref: ref }] } : { anyOf: [id, expandedObject] };
|
|
2639
|
+
}
|
|
2640
|
+
function fieldSchema(field, collection, ctx, shape) {
|
|
2641
|
+
const relation = relationsOf(collection).find((candidate) => candidate.field === field.name);
|
|
2642
|
+
const kind = fieldShape(field);
|
|
2643
|
+
let schema;
|
|
2644
|
+
if (field.kind === "m2o" && field.target) {
|
|
2645
|
+
schema = referenceOrId(field.target, ctx, shape);
|
|
2646
|
+
} else if (kind.kind === "to-many" || relation && (relation.kind === "o2m" || relation.kind === "m2m")) {
|
|
2647
|
+
const target = kind.kind === "to-many" ? kind.target : relation?.target;
|
|
2648
|
+
schema = { type: "array", items: referenceOrId(target, ctx, shape) };
|
|
2649
|
+
} else if (kind.kind === "m2a" || relation?.kind === "m2a") {
|
|
2650
|
+
schema = {
|
|
2651
|
+
type: "array",
|
|
2652
|
+
items: {
|
|
2653
|
+
type: "object",
|
|
2654
|
+
properties: { collection: { type: "string" }, item: {} },
|
|
2655
|
+
required: ["collection", "item"]
|
|
2656
|
+
}
|
|
2657
|
+
};
|
|
2658
|
+
} else if (kind.kind === "localized") {
|
|
2659
|
+
schema = {
|
|
2660
|
+
type: "object",
|
|
2661
|
+
description: 'Per-locale values keyed by locale code (e.g. `{ "en": "\u2026", "vi": "\u2026" }`).',
|
|
2662
|
+
additionalProperties: { type: "string" }
|
|
2663
|
+
};
|
|
2664
|
+
} else if (kind.kind === "file") {
|
|
2665
|
+
schema = shape === "projection" ? { anyOf: [fileId, expandedObject] } : fileId;
|
|
2666
|
+
} else if (kind.kind === "files") {
|
|
2667
|
+
schema = { type: "array", items: shape === "projection" ? { anyOf: [fileId, expandedObject] } : fileId };
|
|
2668
|
+
} else if (field.type === "alias") {
|
|
2669
|
+
return null;
|
|
2670
|
+
} else if (kind.kind === "enum") {
|
|
2671
|
+
schema = { type: "string", enum: [...new Set(kind.values)] };
|
|
2672
|
+
} else {
|
|
2673
|
+
schema = { ...FIELD_TYPE_JSON_SCHEMA[field.type] ?? {} };
|
|
2674
|
+
}
|
|
2675
|
+
if (field.encrypted && shape !== "input") {
|
|
2676
|
+
schema = { anyOf: [schema, { const: "***", description: "Masked: caller lacks `read_decrypted`." }] };
|
|
2677
|
+
}
|
|
2678
|
+
if (isNullable(field)) schema = withNull(schema);
|
|
2679
|
+
if (field.readonly && shape !== "input") schema = { ...schema, readOnly: true };
|
|
2680
|
+
return schema;
|
|
2681
|
+
}
|
|
2682
|
+
function addCollectionSchemas(schemas, collection, base, ctx) {
|
|
2683
|
+
const userFields = collection.fields.filter((field) => !field.system);
|
|
2684
|
+
const ref = (suffix) => ({ $ref: `#/components/schemas/${base}.${suffix}` });
|
|
2685
|
+
const fieldProps = {};
|
|
2686
|
+
const projectionProps = {
|
|
2687
|
+
id: { ...ctx.primaryKeySchema(collection.name), readOnly: true },
|
|
2688
|
+
status: { type: "string" },
|
|
2689
|
+
sort: { type: "integer" },
|
|
2690
|
+
user_created: nullableString,
|
|
2691
|
+
user_updated: nullableString,
|
|
2692
|
+
created_at: timestamp,
|
|
2693
|
+
updated_at: timestamp
|
|
2694
|
+
};
|
|
2695
|
+
const required = [];
|
|
2696
|
+
for (const field of userFields) {
|
|
2697
|
+
const item = fieldSchema(field, collection, ctx, "item");
|
|
2698
|
+
const projection = fieldSchema(field, collection, ctx, "projection");
|
|
2699
|
+
if (item) {
|
|
2700
|
+
fieldProps[field.name] = item;
|
|
2701
|
+
if (field.required) required.push(field.name);
|
|
2702
|
+
}
|
|
2703
|
+
if (projection) projectionProps[field.name] = projection;
|
|
2704
|
+
}
|
|
2705
|
+
schemas[`${base}.Fields`] = {
|
|
2706
|
+
type: "object",
|
|
2707
|
+
description: `User fields of \`${collection.name}\`.`,
|
|
2708
|
+
properties: fieldProps,
|
|
2709
|
+
...required.length > 0 ? { required } : {}
|
|
2710
|
+
};
|
|
2711
|
+
schemas[`${base}.Item`] = {
|
|
2712
|
+
type: "object",
|
|
2713
|
+
properties: {
|
|
2714
|
+
id: { ...ctx.primaryKeySchema(collection.name), readOnly: true },
|
|
2715
|
+
status: { type: "string", description: "`draft`, `published`, `archived`, or a custom workflow value." },
|
|
2716
|
+
sort: { type: "integer" },
|
|
2717
|
+
data: ref("Fields"),
|
|
2718
|
+
publishAt: nullableTimestamp,
|
|
2719
|
+
unpublishAt: nullableTimestamp,
|
|
2720
|
+
userCreated: nullableString,
|
|
2721
|
+
userUpdated: nullableString,
|
|
2722
|
+
createdAt: { ...timestamp, readOnly: true },
|
|
2723
|
+
updatedAt: { ...timestamp, readOnly: true }
|
|
2724
|
+
},
|
|
2725
|
+
required: ["id", "status", "data"]
|
|
2726
|
+
};
|
|
2727
|
+
schemas[`${base}.Projection`] = {
|
|
2728
|
+
type: "object",
|
|
2729
|
+
description: "Flat shape returned when the request passes `fields=`; only requested keys are present.",
|
|
2730
|
+
properties: projectionProps
|
|
2731
|
+
};
|
|
2732
|
+
const inputSchema = (withRequired) => {
|
|
2733
|
+
const props = {};
|
|
2734
|
+
const req = [];
|
|
2735
|
+
for (const field of userFields) {
|
|
2736
|
+
if (field.readonly || field.generated) continue;
|
|
2737
|
+
const schema = fieldSchema(field, collection, ctx, "input");
|
|
2738
|
+
if (!schema) continue;
|
|
2739
|
+
props[field.name] = schema;
|
|
2740
|
+
if (withRequired && field.required) req.push(field.name);
|
|
2741
|
+
}
|
|
2742
|
+
return {
|
|
2743
|
+
type: "object",
|
|
2744
|
+
properties: {
|
|
2745
|
+
data: { type: "object", properties: props, ...req.length > 0 ? { required: req } : {} },
|
|
2746
|
+
status: { type: "string" },
|
|
2747
|
+
sort: { type: "integer" },
|
|
2748
|
+
publishAt: nullableTimestamp,
|
|
2749
|
+
unpublishAt: nullableTimestamp
|
|
2750
|
+
},
|
|
2751
|
+
...withRequired ? { required: ["data"] } : {}
|
|
2752
|
+
};
|
|
2753
|
+
};
|
|
2754
|
+
schemas[`${base}.CreateInput`] = inputSchema(true);
|
|
2755
|
+
schemas[`${base}.UpdateInput`] = inputSchema(false);
|
|
2756
|
+
const itemOrProjection = { anyOf: [ref("Item"), ref("Projection")] };
|
|
2757
|
+
schemas[`${base}.Response`] = {
|
|
2758
|
+
type: "object",
|
|
2759
|
+
description: "Single-item envelope.",
|
|
2760
|
+
properties: { data: itemOrProjection },
|
|
2761
|
+
required: ["data"]
|
|
2762
|
+
};
|
|
2763
|
+
schemas[`${base}.ListResponse`] = {
|
|
2764
|
+
type: "object",
|
|
2765
|
+
description: "A page of items.",
|
|
2766
|
+
properties: {
|
|
2767
|
+
data: { type: "array", items: itemOrProjection },
|
|
2768
|
+
meta: { $ref: "#/components/schemas/PaginationMeta" }
|
|
2769
|
+
},
|
|
2770
|
+
required: ["data"]
|
|
2771
|
+
};
|
|
2772
|
+
}
|
|
2773
|
+
|
|
2041
2774
|
// src/typegen/index.ts
|
|
2042
2775
|
var DEFAULT_IMPORT_SOURCE = "@lumibase/sdk";
|
|
2043
2776
|
function generateTypes(manifest, options = {}) {
|
|
@@ -2059,11 +2792,12 @@ function generateSingleFile(manifest, branded, importSource) {
|
|
|
2059
2792
|
return [base, expanded].filter(Boolean).join("\n\n");
|
|
2060
2793
|
}).join("\n\n");
|
|
2061
2794
|
const blockTypes = generateBlockTypes(manifest, branded, known);
|
|
2795
|
+
const richText = manifest.collections.some((c) => c.fields.some((f) => f.type === "richtext")) ? RICHTEXT_TS_DECLARATIONS : "";
|
|
2062
2796
|
const collectionsMap = `export interface LumibaseCollections {
|
|
2063
|
-
${manifest.collections.map((c) => ` ${c.name}: ${
|
|
2797
|
+
${manifest.collections.map((c) => ` ${c.name}: ${capitalize2(c.name)};`).join("\n")}
|
|
2064
2798
|
}`;
|
|
2065
2799
|
const schemaType = "export type LumibaseSchema = LumibaseCollections;";
|
|
2066
|
-
const body = [collectionInterfaces, blockTypes, collectionsMap, schemaType].filter(Boolean).join("\n\n");
|
|
2800
|
+
const body = [richText, collectionInterfaces, blockTypes, collectionsMap, schemaType].filter(Boolean).join("\n\n");
|
|
2067
2801
|
return `${imports}
|
|
2068
2802
|
|
|
2069
2803
|
${body}`;
|
|
@@ -2078,27 +2812,27 @@ function generateCollectionInterface(coll, branded, known) {
|
|
|
2078
2812
|
const readonly = f.readonly || f.generated ? "readonly " : "";
|
|
2079
2813
|
return ` ${readonly}${f.name}${optional}: ${tsType};`;
|
|
2080
2814
|
}).join("\n");
|
|
2081
|
-
return `export interface ${
|
|
2815
|
+
return `export interface ${capitalize2(coll.name)} {
|
|
2082
2816
|
${fields}
|
|
2083
2817
|
}`;
|
|
2084
2818
|
}
|
|
2085
2819
|
function generateExpandedType(coll) {
|
|
2086
2820
|
const relations = coll.relations ?? [];
|
|
2087
2821
|
if (relations.length === 0) {
|
|
2088
|
-
return `export type ${
|
|
2822
|
+
return `export type ${capitalize2(coll.name)}Expanded = ${capitalize2(coll.name)};`;
|
|
2089
2823
|
}
|
|
2090
2824
|
const omittedKeys = relations.map((relation) => quoteProperty(relation.field)).join(" | ");
|
|
2091
2825
|
const fields = relations.map((relation) => ` ${relation.field}?: ${expandedRelationType(relation)};`).join("\n");
|
|
2092
|
-
return `export type ${
|
|
2826
|
+
return `export type ${capitalize2(coll.name)}Expanded = Omit<${capitalize2(coll.name)}, ${omittedKeys}> & {
|
|
2093
2827
|
${fields}
|
|
2094
2828
|
};`;
|
|
2095
2829
|
}
|
|
2096
2830
|
function expandedRelationType(relation) {
|
|
2097
2831
|
if (relation.kind === "m2o") {
|
|
2098
|
-
return `${
|
|
2832
|
+
return `${capitalize2(relation.target)} | ${capitalize2(relation.target)}Expanded | null`;
|
|
2099
2833
|
}
|
|
2100
2834
|
if (relation.kind === "o2m" || relation.kind === "m2m") {
|
|
2101
|
-
return `Array<${
|
|
2835
|
+
return `Array<${capitalize2(relation.target)} | ${capitalize2(relation.target)}Expanded>`;
|
|
2102
2836
|
}
|
|
2103
2837
|
return `Array<{ collection: string; item: unknown }>`;
|
|
2104
2838
|
}
|
|
@@ -2116,7 +2850,7 @@ function generateBlockTypes(manifest, branded, known) {
|
|
|
2116
2850
|
const optional = f.required ? "" : "?";
|
|
2117
2851
|
return ` ${f.name}${optional}: ${mapFieldTypeToTs(f, coll, branded, known)};`;
|
|
2118
2852
|
}).join("\n");
|
|
2119
|
-
const t =
|
|
2853
|
+
const t = capitalize2(name);
|
|
2120
2854
|
return `export interface ${t}BlockData {
|
|
2121
2855
|
${dataFields}
|
|
2122
2856
|
}
|
|
@@ -2130,7 +2864,7 @@ export interface ${t}Block {
|
|
|
2130
2864
|
}
|
|
2131
2865
|
function blocksUnionType(field, isKnown) {
|
|
2132
2866
|
const members = (field.allowedBlocks ?? []).map(
|
|
2133
|
-
(name) => isKnown(name) ? `${
|
|
2867
|
+
(name) => isKnown(name) ? `${capitalize2(name)}Block` : `{ id: string; type: ${JSON.stringify(name)}; data: Record<string, unknown> }`
|
|
2134
2868
|
);
|
|
2135
2869
|
if (members.length === 0) return "Array<{ id: string; type: string; data: Record<string, unknown> }>";
|
|
2136
2870
|
return `Array<${members.join(" | ")}>`;
|
|
@@ -2143,11 +2877,16 @@ function mapFieldTypeToTs(field, coll, branded, known = /* @__PURE__ */ new Set(
|
|
|
2143
2877
|
return nullable(field, mapScalarFieldType(field, branded));
|
|
2144
2878
|
}
|
|
2145
2879
|
if (field.kind === "o2m" || field.kind === "m2m") {
|
|
2146
|
-
return `${
|
|
2880
|
+
return `${capitalize2(field.target || "unknown")}[]`;
|
|
2147
2881
|
}
|
|
2148
2882
|
if (field.kind === "m2a") {
|
|
2149
2883
|
return `Array<{ collection: string; item: unknown }>`;
|
|
2150
2884
|
}
|
|
2885
|
+
const shape = fieldShape(field);
|
|
2886
|
+
if (shape.kind === "localized" || shape.kind === "file" || shape.kind === "files") {
|
|
2887
|
+
const wire = shape.kind === "localized" ? "Partial<Record<Locale, string>>" : shape.kind === "file" ? "string" : "string[]";
|
|
2888
|
+
return nullable(field, field.encrypted ? `${wire} | '***'` : wire);
|
|
2889
|
+
}
|
|
2151
2890
|
if (field.enum && field.enum.length > 0) {
|
|
2152
2891
|
return nullable(field, field.enum.map((v) => JSON.stringify(v)).join(" | "));
|
|
2153
2892
|
}
|
|
@@ -2171,15 +2910,18 @@ function mapScalarFieldType(field, branded) {
|
|
|
2171
2910
|
integer: "number",
|
|
2172
2911
|
bigInteger: "number",
|
|
2173
2912
|
decimal: "number",
|
|
2913
|
+
float: "number",
|
|
2174
2914
|
boolean: "boolean",
|
|
2175
2915
|
json: "unknown",
|
|
2176
2916
|
uuid: mapPrimaryKeyType("uuid", branded, field.branded),
|
|
2177
2917
|
date: "string",
|
|
2178
2918
|
datetime: "string",
|
|
2919
|
+
dateTime: "string",
|
|
2179
2920
|
time: "string",
|
|
2180
2921
|
timestamp: "string",
|
|
2181
2922
|
geometry: "GeoJSON.Geometry",
|
|
2182
|
-
geopoint: "{ lat: number; lng: number }"
|
|
2923
|
+
geopoint: "{ lat: number; lng: number }",
|
|
2924
|
+
richtext: "RichTextDocument"
|
|
2183
2925
|
};
|
|
2184
2926
|
return typeMap[field.type] || "unknown";
|
|
2185
2927
|
}
|
|
@@ -2194,7 +2936,7 @@ function mapPrimaryKeyType(primaryKeyType, branded, brand) {
|
|
|
2194
2936
|
if (branded) return `Brand<'${brand || "ID"}', string>`;
|
|
2195
2937
|
return "ID";
|
|
2196
2938
|
}
|
|
2197
|
-
function
|
|
2939
|
+
function capitalize2(str) {
|
|
2198
2940
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2199
2941
|
}
|
|
2200
2942
|
function quoteProperty(name) {
|
|
@@ -2228,8 +2970,12 @@ function jsonLdScript(item) {
|
|
|
2228
2970
|
AudienceClient,
|
|
2229
2971
|
DECISION_ERROR_CODES,
|
|
2230
2972
|
DEFAULT_IMPORT_SOURCE,
|
|
2973
|
+
DEFAULT_ZOD_IMPORT,
|
|
2974
|
+
FILES_INTERFACE,
|
|
2975
|
+
FILE_INTERFACE,
|
|
2231
2976
|
LumiError,
|
|
2232
2977
|
RealtimeClient,
|
|
2978
|
+
TRANSLATABLE_INTERFACE,
|
|
2233
2979
|
ackCdcSubscription,
|
|
2234
2980
|
checkCdcPipelineHealth,
|
|
2235
2981
|
createAgentArtifact,
|
|
@@ -2250,8 +2996,12 @@ function jsonLdScript(item) {
|
|
|
2250
2996
|
evaluateAgentArtifact,
|
|
2251
2997
|
exportAccessManifest,
|
|
2252
2998
|
extractSeo,
|
|
2999
|
+
filterManifest,
|
|
2253
3000
|
generateAgentApp,
|
|
3001
|
+
generateGraphQLSdl,
|
|
3002
|
+
generateOpenApi,
|
|
2254
3003
|
generateTypes,
|
|
3004
|
+
generateZod,
|
|
2255
3005
|
getEffectivePreset,
|
|
2256
3006
|
getReview,
|
|
2257
3007
|
getReviewProfile,
|
|
@@ -2280,7 +3030,9 @@ function jsonLdScript(item) {
|
|
|
2280
3030
|
readCdcSubscription,
|
|
2281
3031
|
readItem,
|
|
2282
3032
|
readItems,
|
|
3033
|
+
realtimeHolders,
|
|
2283
3034
|
replayCdcSubscription,
|
|
3035
|
+
retainRealtime,
|
|
2284
3036
|
retryAgentRun,
|
|
2285
3037
|
reviewContentHash,
|
|
2286
3038
|
reviewDraft,
|