@lumibase/sdk 2.0.0 → 2.1.0
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 +197 -17
- package/dist/index.d.cts +244 -5
- package/dist/index.d.ts +244 -5
- package/dist/index.js +197 -17
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -88,16 +88,35 @@ function createLumiClient(opts) {
|
|
|
88
88
|
}
|
|
89
89
|
return refreshInFlight;
|
|
90
90
|
}
|
|
91
|
-
|
|
91
|
+
function buildHeaders(init) {
|
|
92
92
|
const headers = new Headers(init.headers);
|
|
93
93
|
for (const [key, value] of Object.entries(opts.headers ?? {})) {
|
|
94
94
|
headers.set(key, value);
|
|
95
95
|
}
|
|
96
96
|
headers.set("authorization", `Bearer ${currentToken}`);
|
|
97
97
|
headers.set("x-lumi-site", opts.siteId);
|
|
98
|
-
|
|
98
|
+
const isForm = typeof FormData !== "undefined" && init.body instanceof FormData;
|
|
99
|
+
if (!headers.has("content-type") && init.body && !isForm) {
|
|
99
100
|
headers.set("content-type", "application/json");
|
|
100
101
|
}
|
|
102
|
+
return headers;
|
|
103
|
+
}
|
|
104
|
+
async function rawFetch(path, init = {}, retried = false) {
|
|
105
|
+
const res = await fetcher(`${base}${path}`, { ...init, headers: buildHeaders(init) });
|
|
106
|
+
if (res.ok) return res;
|
|
107
|
+
if (res.status === 401 && !retried && currentRefreshToken && await attemptRefresh()) {
|
|
108
|
+
return rawFetch(path, init, true);
|
|
109
|
+
}
|
|
110
|
+
if (res.status === 401 && opts.onUnauthorized) {
|
|
111
|
+
try {
|
|
112
|
+
opts.onUnauthorized();
|
|
113
|
+
} catch {
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
throw new LumiError(res.status, toErrorBody(res.status, parseResponseBody(await res.text())), res.headers);
|
|
117
|
+
}
|
|
118
|
+
async function rawRequest(path, init = {}, retried = false) {
|
|
119
|
+
const headers = buildHeaders(init);
|
|
101
120
|
const res = await fetcher(`${base}${path}`, { ...init, headers });
|
|
102
121
|
const text = await res.text();
|
|
103
122
|
const body = parseResponseBody(text);
|
|
@@ -125,6 +144,7 @@ function createLumiClient(opts) {
|
|
|
125
144
|
siteId: opts.siteId,
|
|
126
145
|
fetcher,
|
|
127
146
|
rawRequest,
|
|
147
|
+
rawFetch: (path, init) => rawFetch(path, init),
|
|
128
148
|
async request(command) {
|
|
129
149
|
return command(this);
|
|
130
150
|
},
|
|
@@ -625,7 +645,19 @@ function legacyRest() {
|
|
|
625
645
|
deleteField: fields.delete,
|
|
626
646
|
listRelations: relations.list,
|
|
627
647
|
createRelation: relations.create,
|
|
628
|
-
deleteRelation: relations.delete
|
|
648
|
+
deleteRelation: relations.delete,
|
|
649
|
+
/**
|
|
650
|
+
* Collection form layout (#750): tabs / groups / dividers + conditions.
|
|
651
|
+
* Typed loosely so the SDK stays decoupled from contracts; parse with
|
|
652
|
+
* `parseFormLayout` from `@lumibase/contracts/form-layout`.
|
|
653
|
+
*/
|
|
654
|
+
getFormLayout: (name) => client.rawRequest(
|
|
655
|
+
`/api/v1/collections/${name}/form-layout`
|
|
656
|
+
),
|
|
657
|
+
setFormLayout: (name, layout) => client.rawRequest(
|
|
658
|
+
`/api/v1/collections/${name}/form-layout`,
|
|
659
|
+
{ method: "PUT", body: JSON.stringify({ layout }) }
|
|
660
|
+
)
|
|
629
661
|
};
|
|
630
662
|
function items(name) {
|
|
631
663
|
function buildQuery(params) {
|
|
@@ -665,10 +697,55 @@ function legacyRest() {
|
|
|
665
697
|
body: JSON.stringify(input)
|
|
666
698
|
}),
|
|
667
699
|
delete: (id) => client.rawRequest(`${base}/${id}`, { method: "DELETE" }),
|
|
700
|
+
/**
|
|
701
|
+
* Stream the collection as CSV or JSON (#727). Resolves with the
|
|
702
|
+
* unread `Response`: pipe `res.body` to a file, or `await res.text()`.
|
|
703
|
+
* Uses the caller's read permission; filters use the list syntax.
|
|
704
|
+
*/
|
|
705
|
+
export: (params = {}) => {
|
|
706
|
+
const qs = new URLSearchParams();
|
|
707
|
+
if (params.format) qs.set("format", params.format);
|
|
708
|
+
if (params.fields?.length) qs.set("fields", params.fields.join(","));
|
|
709
|
+
if (params.locales?.length) qs.set("locales", params.locales.join(","));
|
|
710
|
+
if (params.relations) qs.set("relations", params.relations);
|
|
711
|
+
if (params.filter) qs.set("filter", JSON.stringify(params.filter));
|
|
712
|
+
if (params.sort?.length) qs.set("sort", params.sort.join(","));
|
|
713
|
+
if (params.status) qs.set("status", params.status);
|
|
714
|
+
const s = qs.toString();
|
|
715
|
+
return client.rawFetch(`${base}/export${s ? `?${s}` : ""}`);
|
|
716
|
+
},
|
|
668
717
|
bulk: (op, payload) => client.rawRequest(`${base}/bulk`, {
|
|
669
718
|
method: "POST",
|
|
670
719
|
body: JSON.stringify({ op, items: payload })
|
|
671
720
|
}),
|
|
721
|
+
/**
|
|
722
|
+
* Hierarchical collections (#749) on a `meta.tree.enabled` collection.
|
|
723
|
+
* Reads apply the caller's row rules; the anchor must be readable.
|
|
724
|
+
*/
|
|
725
|
+
tree: {
|
|
726
|
+
children: (parent, params) => {
|
|
727
|
+
const q = buildQuery(params);
|
|
728
|
+
const sep = q ? "&" : "?";
|
|
729
|
+
return client.rawRequest(
|
|
730
|
+
`${base}${q}${sep}tree=children&parent=${encodeURIComponent(parent ?? "null")}`
|
|
731
|
+
);
|
|
732
|
+
},
|
|
733
|
+
subtree: (parent, params) => {
|
|
734
|
+
const q = buildQuery(params);
|
|
735
|
+
const sep = q ? "&" : "?";
|
|
736
|
+
return client.rawRequest(
|
|
737
|
+
`${base}${q}${sep}tree=subtree&parent=${encodeURIComponent(parent ?? "null")}`
|
|
738
|
+
);
|
|
739
|
+
},
|
|
740
|
+
ancestors: (node) => client.rawRequest(
|
|
741
|
+
`${base}?tree=ancestors&node=${encodeURIComponent(node)}`
|
|
742
|
+
),
|
|
743
|
+
/** Move an item and its subtree; `position` is its 0-based index among the new siblings. */
|
|
744
|
+
move: (id, input) => client.rawRequest(`${base}/${id}/move`, {
|
|
745
|
+
method: "POST",
|
|
746
|
+
body: JSON.stringify(input)
|
|
747
|
+
})
|
|
748
|
+
},
|
|
672
749
|
listRevisions: (id) => client.rawRequest(`${base}/${id}/revisions`),
|
|
673
750
|
revertRevision: (id, revisionId) => client.rawRequest(`${base}/${id}/revert/${revisionId}`, {
|
|
674
751
|
method: "POST"
|
|
@@ -1096,10 +1173,44 @@ function legacyRest() {
|
|
|
1096
1173
|
body: JSON.stringify(patch)
|
|
1097
1174
|
}),
|
|
1098
1175
|
delete: (id) => client.rawRequest(`/api/v1/deployments/targets/${id}`, { method: "DELETE" }),
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1176
|
+
/**
|
|
1177
|
+
* Resolves with the deployment (201), or — when the target belongs to
|
|
1178
|
+
* an approval-gated environment (#743) — with a `pending_approval`
|
|
1179
|
+
* outcome (202).
|
|
1180
|
+
*/
|
|
1181
|
+
deploy: (id, input = {}) => client.rawRequest(
|
|
1182
|
+
`/api/v1/deployments/targets/${id}/deploy`,
|
|
1183
|
+
{
|
|
1184
|
+
method: "POST",
|
|
1185
|
+
body: JSON.stringify(input)
|
|
1186
|
+
}
|
|
1187
|
+
)
|
|
1188
|
+
},
|
|
1189
|
+
// Environments, promotion and deploy approvals (#743).
|
|
1190
|
+
access: () => client.rawRequest("/api/v1/deployments/access"),
|
|
1191
|
+
environments: {
|
|
1192
|
+
list: () => client.rawRequest("/api/v1/deployments/environments"),
|
|
1193
|
+
update: (environments) => client.rawRequest("/api/v1/deployments/environments", {
|
|
1194
|
+
method: "PUT",
|
|
1195
|
+
body: JSON.stringify({ environments })
|
|
1196
|
+
}),
|
|
1197
|
+
deploy: (key, input = {}) => client.rawRequest(
|
|
1198
|
+
`/api/v1/deployments/environments/${encodeURIComponent(key)}/deploy`,
|
|
1199
|
+
{ method: "POST", body: JSON.stringify(input) }
|
|
1200
|
+
),
|
|
1201
|
+
promote: (key, input = {}) => client.rawRequest(
|
|
1202
|
+
`/api/v1/deployments/environments/${encodeURIComponent(key)}/promote`,
|
|
1203
|
+
{ method: "POST", body: JSON.stringify(input) }
|
|
1204
|
+
)
|
|
1205
|
+
},
|
|
1206
|
+
approvals: {
|
|
1207
|
+
list: (status = "pending") => client.rawRequest(
|
|
1208
|
+
`/api/v1/deployments/approvals?status=${encodeURIComponent(status)}`
|
|
1209
|
+
),
|
|
1210
|
+
decide: (id, input) => client.rawRequest(
|
|
1211
|
+
`/api/v1/deployments/approvals/${encodeURIComponent(id)}/decide`,
|
|
1212
|
+
{ method: "POST", body: JSON.stringify(input) }
|
|
1213
|
+
)
|
|
1103
1214
|
},
|
|
1104
1215
|
list: (params) => {
|
|
1105
1216
|
const qs = new URLSearchParams();
|
|
@@ -1112,9 +1223,40 @@ function legacyRest() {
|
|
|
1112
1223
|
logs: (id) => client.rawRequest(`/api/v1/deployments/${id}/logs`),
|
|
1113
1224
|
refresh: (id) => client.rawRequest(`/api/v1/deployments/${id}/refresh`, { method: "POST" })
|
|
1114
1225
|
};
|
|
1226
|
+
const imports = {
|
|
1227
|
+
/**
|
|
1228
|
+
* Upload a CSV or JSON export file and queue it. Resolves with the job
|
|
1229
|
+
* (`202`); poll `get(id)` until `status` is terminal.
|
|
1230
|
+
*/
|
|
1231
|
+
create: (collection, file, opts = {}) => {
|
|
1232
|
+
const qs = new URLSearchParams({ collection });
|
|
1233
|
+
if (opts.format) qs.set("format", opts.format);
|
|
1234
|
+
if (opts.mode) qs.set("mode", opts.mode);
|
|
1235
|
+
const form = new FormData();
|
|
1236
|
+
const blob = typeof file === "string" ? new Blob([file], { type: "application/octet-stream" }) : file;
|
|
1237
|
+
form.append("file", blob, opts.filename ?? `${collection}.${opts.format ?? "csv"}`);
|
|
1238
|
+
return client.rawRequest(`/api/v1/imports?${qs.toString()}`, { method: "POST", body: form });
|
|
1239
|
+
},
|
|
1240
|
+
list: (params = {}) => {
|
|
1241
|
+
const qs = new URLSearchParams();
|
|
1242
|
+
if (params.collection) qs.set("collection", params.collection);
|
|
1243
|
+
if (params.status) qs.set("status", params.status);
|
|
1244
|
+
if (params.limit !== void 0) qs.set("limit", String(params.limit));
|
|
1245
|
+
const s = qs.toString();
|
|
1246
|
+
return client.rawRequest(`/api/v1/imports${s ? `?${s}` : ""}`);
|
|
1247
|
+
},
|
|
1248
|
+
get: (id) => client.rawRequest(`/api/v1/imports/${encodeURIComponent(id)}`),
|
|
1249
|
+
errors: (id) => client.rawRequest(
|
|
1250
|
+
`/api/v1/imports/${encodeURIComponent(id)}/errors`
|
|
1251
|
+
),
|
|
1252
|
+
/** The error report as a CSV download (`row,id,message`). */
|
|
1253
|
+
errorsCsv: (id) => client.rawFetch(`/api/v1/imports/${encodeURIComponent(id)}/errors?format=csv`),
|
|
1254
|
+
cancel: (id) => client.rawRequest(`/api/v1/imports/${encodeURIComponent(id)}/cancel`, { method: "POST" })
|
|
1255
|
+
};
|
|
1115
1256
|
return {
|
|
1116
1257
|
schema,
|
|
1117
1258
|
items,
|
|
1259
|
+
imports,
|
|
1118
1260
|
roles,
|
|
1119
1261
|
policies,
|
|
1120
1262
|
access,
|
|
@@ -1807,33 +1949,32 @@ function generateTypes(manifest, options = {}) {
|
|
|
1807
1949
|
return generatePerCollection(manifest, branded, importSource);
|
|
1808
1950
|
}
|
|
1809
1951
|
function generateSingleFile(manifest, branded, importSource) {
|
|
1952
|
+
const known = new Set(manifest.collections.map((c) => c.name));
|
|
1810
1953
|
const imports = [
|
|
1811
1954
|
`import type { ID, Locale } from '${importSource}';`,
|
|
1812
1955
|
branded ? `import type { Brand } from '${importSource}';` : ""
|
|
1813
1956
|
].filter(Boolean).join("\n");
|
|
1814
1957
|
const collectionInterfaces = manifest.collections.map((coll) => {
|
|
1815
|
-
const base = generateCollectionInterface(coll, branded);
|
|
1958
|
+
const base = generateCollectionInterface(coll, branded, known);
|
|
1816
1959
|
const expanded = generateExpandedType(coll);
|
|
1817
1960
|
return [base, expanded].filter(Boolean).join("\n\n");
|
|
1818
1961
|
}).join("\n\n");
|
|
1962
|
+
const blockTypes = generateBlockTypes(manifest, branded, known);
|
|
1819
1963
|
const collectionsMap = `export interface LumibaseCollections {
|
|
1820
1964
|
${manifest.collections.map((c) => ` ${c.name}: ${capitalize(c.name)};`).join("\n")}
|
|
1821
1965
|
}`;
|
|
1822
1966
|
const schemaType = "export type LumibaseSchema = LumibaseCollections;";
|
|
1967
|
+
const body = [collectionInterfaces, blockTypes, collectionsMap, schemaType].filter(Boolean).join("\n\n");
|
|
1823
1968
|
return `${imports}
|
|
1824
1969
|
|
|
1825
|
-
${
|
|
1826
|
-
|
|
1827
|
-
${collectionsMap}
|
|
1828
|
-
|
|
1829
|
-
${schemaType}`;
|
|
1970
|
+
${body}`;
|
|
1830
1971
|
}
|
|
1831
1972
|
function generatePerCollection(manifest, branded, importSource) {
|
|
1832
1973
|
return generateSingleFile(manifest, branded, importSource);
|
|
1833
1974
|
}
|
|
1834
|
-
function generateCollectionInterface(coll, branded) {
|
|
1975
|
+
function generateCollectionInterface(coll, branded, known) {
|
|
1835
1976
|
const fields = coll.fields.map((f) => {
|
|
1836
|
-
const tsType = mapFieldTypeToTs(f, coll, branded);
|
|
1977
|
+
const tsType = mapFieldTypeToTs(f, coll, branded, known);
|
|
1837
1978
|
const optional = f.required ? "" : "?";
|
|
1838
1979
|
const readonly = f.readonly || f.generated ? "readonly " : "";
|
|
1839
1980
|
return ` ${readonly}${f.name}${optional}: ${tsType};`;
|
|
@@ -1862,7 +2003,43 @@ function expandedRelationType(relation) {
|
|
|
1862
2003
|
}
|
|
1863
2004
|
return `Array<{ collection: string; item: unknown }>`;
|
|
1864
2005
|
}
|
|
1865
|
-
function
|
|
2006
|
+
function generateBlockTypes(manifest, branded, known) {
|
|
2007
|
+
const byName = new Map(manifest.collections.map((c) => [c.name, c]));
|
|
2008
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
2009
|
+
for (const coll of manifest.collections) {
|
|
2010
|
+
for (const f of coll.fields) {
|
|
2011
|
+
if (f.type === "blocks") for (const t of f.allowedBlocks ?? []) referenced.add(t);
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
return [...referenced].filter((name) => byName.has(name)).sort().map((name) => {
|
|
2015
|
+
const coll = byName.get(name);
|
|
2016
|
+
const dataFields = coll.fields.filter((f) => !f.system && !f.primaryKey).map((f) => {
|
|
2017
|
+
const optional = f.required ? "" : "?";
|
|
2018
|
+
return ` ${f.name}${optional}: ${mapFieldTypeToTs(f, coll, branded, known)};`;
|
|
2019
|
+
}).join("\n");
|
|
2020
|
+
const t = capitalize(name);
|
|
2021
|
+
return `export interface ${t}BlockData {
|
|
2022
|
+
${dataFields}
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
export interface ${t}Block {
|
|
2026
|
+
id: string;
|
|
2027
|
+
type: ${JSON.stringify(name)};
|
|
2028
|
+
data: ${t}BlockData;
|
|
2029
|
+
}`;
|
|
2030
|
+
}).join("\n\n");
|
|
2031
|
+
}
|
|
2032
|
+
function blocksUnionType(field, isKnown) {
|
|
2033
|
+
const members = (field.allowedBlocks ?? []).map(
|
|
2034
|
+
(name) => isKnown(name) ? `${capitalize(name)}Block` : `{ id: string; type: ${JSON.stringify(name)}; data: Record<string, unknown> }`
|
|
2035
|
+
);
|
|
2036
|
+
if (members.length === 0) return "Array<{ id: string; type: string; data: Record<string, unknown> }>";
|
|
2037
|
+
return `Array<${members.join(" | ")}>`;
|
|
2038
|
+
}
|
|
2039
|
+
function mapFieldTypeToTs(field, coll, branded, known = /* @__PURE__ */ new Set()) {
|
|
2040
|
+
if (field.type === "blocks") {
|
|
2041
|
+
return nullable(field, blocksUnionType(field, (name) => known.has(name)));
|
|
2042
|
+
}
|
|
1866
2043
|
if (field.kind === "m2o") {
|
|
1867
2044
|
return nullable(field, mapScalarFieldType(field, branded));
|
|
1868
2045
|
}
|
|
@@ -1884,7 +2061,9 @@ function mapFieldTypeToTs(field, coll, branded) {
|
|
|
1884
2061
|
const tsType = mapScalarFieldType(field, branded);
|
|
1885
2062
|
return nullable(field, field.encrypted ? `${tsType} | '***'` : tsType);
|
|
1886
2063
|
}
|
|
2064
|
+
var ZONED_DATETIME_TS = "{ instant: string; tz: string; offset: string }";
|
|
1887
2065
|
function mapScalarFieldType(field, branded) {
|
|
2066
|
+
if (field.timezone) return ZONED_DATETIME_TS;
|
|
1888
2067
|
const typeMap = {
|
|
1889
2068
|
string: "string",
|
|
1890
2069
|
text: "string",
|
|
@@ -1900,7 +2079,8 @@ function mapScalarFieldType(field, branded) {
|
|
|
1900
2079
|
datetime: "string",
|
|
1901
2080
|
time: "string",
|
|
1902
2081
|
timestamp: "string",
|
|
1903
|
-
geometry: "GeoJSON.Geometry"
|
|
2082
|
+
geometry: "GeoJSON.Geometry",
|
|
2083
|
+
geopoint: "{ lat: number; lng: number }"
|
|
1904
2084
|
};
|
|
1905
2085
|
return typeMap[field.type] || "unknown";
|
|
1906
2086
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumibase/sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "LumiBase JS SDK (REST + WebSocket) + typegen core. Isomorphic ESM.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"tsup": "^9.0.0",
|
|
26
26
|
"typescript": "^5.6.2",
|
|
27
27
|
"vitest": "^5.1.0",
|
|
28
|
-
"@lumibase/contracts": "2.
|
|
28
|
+
"@lumibase/contracts": "2.1.0"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
|
31
31
|
"dist"
|