@bettercms-ai/sdk 1.17.0 → 1.19.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 CHANGED
@@ -154,13 +154,24 @@ var BetterCMSError = class _BetterCMSError extends Error {
154
154
  * hint which field or rule failed (FLO-474). Mirrors the forms client's `fieldErrors`.
155
155
  */
156
156
  fieldErrors;
157
- constructor(message, status, code, bodyCode, fieldErrors) {
157
+ /**
158
+ * The parsed response body, when there was one.
159
+ *
160
+ * Several refusals carry STRUCTURED detail that neither `message` nor `fieldErrors` can hold,
161
+ * and which is the whole point of the refusal: a component publish 409s with the full
162
+ * `readiness` tuple (what evidence is missing, whether a human still has to approve it) and
163
+ * 422s with the `issues` naming the Layouts it would break. Collapsing those to a sentence
164
+ * leaves a caller able to report that something failed and not what to do about it.
165
+ */
166
+ body;
167
+ constructor(message, status, code, bodyCode, fieldErrors, body) {
158
168
  super(message);
159
169
  this.name = "BetterCMSError";
160
170
  this.status = status;
161
171
  this.code = code;
162
172
  this.bodyCode = bodyCode;
163
173
  this.fieldErrors = fieldErrors;
174
+ this.body = body;
164
175
  if (Error.captureStackTrace) {
165
176
  Error.captureStackTrace(this, _BetterCMSError);
166
177
  }
@@ -187,6 +198,7 @@ var BetterCMSError = class _BetterCMSError extends Error {
187
198
  let bodyCode;
188
199
  let fieldErrors;
189
200
  let code = statusToCode(res.status);
201
+ let raw;
190
202
  try {
191
203
  const body = await res.json();
192
204
  if (body?.errors && typeof body.errors === "object") {
@@ -202,9 +214,10 @@ var BetterCMSError = class _BetterCMSError extends Error {
202
214
  if (field) message = `${field}: ${detail}`;
203
215
  }
204
216
  if (body?.code) bodyCode = body.code;
217
+ raw = body;
205
218
  } catch {
206
219
  }
207
- return new _BetterCMSError(message, res.status, code, bodyCode, fieldErrors);
220
+ return new _BetterCMSError(message, res.status, code, bodyCode, fieldErrors, raw);
208
221
  }
209
222
  };
210
223
 
@@ -825,9 +838,10 @@ async function updateForm(client, id, input) {
825
838
  }
826
839
 
827
840
  // src/methods/management/components.ts
828
- async function listComponents(client) {
841
+ async function listComponents(client, options) {
842
+ const query = options?.select ? `?select=${options.select}` : "";
829
843
  const res = await client.fetchJSON(
830
- client.url("/management/components"),
844
+ client.url(`/management/components${query}`),
831
845
  { method: "GET" }
832
846
  );
833
847
  return res.data;
@@ -1068,9 +1082,8 @@ var BetterCMSManagementClient = class extends BetterCMSDeliveryClient {
1068
1082
  updateForm(id, input) {
1069
1083
  return updateForm(this, id, input);
1070
1084
  }
1071
- // ── Components: discover + author reusable symbols ──
1072
- listComponents() {
1073
- return listComponents(this);
1085
+ listComponents(options) {
1086
+ return options?.select ? listComponents(this, { select: options.select }) : listComponents(this);
1074
1087
  }
1075
1088
  getComponent(id) {
1076
1089
  return getComponent(this, id);
@@ -1249,6 +1262,16 @@ function unwrap(json) {
1249
1262
  }
1250
1263
  return json;
1251
1264
  }
1265
+ function tokenSlug(token) {
1266
+ try {
1267
+ const part = token.split(".")[1];
1268
+ if (!part) return null;
1269
+ const json = atob(part.replace(/-/g, "+").replace(/_/g, "/"));
1270
+ return JSON.parse(json).entrySlug ?? null;
1271
+ } catch {
1272
+ return null;
1273
+ }
1274
+ }
1252
1275
  function createRequestCache() {
1253
1276
  const inflight = /* @__PURE__ */ new Map();
1254
1277
  const settled = /* @__PURE__ */ new Map();
@@ -1349,6 +1372,23 @@ function createClient(options) {
1349
1372
  return unwrap(await fetchJSON(url));
1350
1373
  },
1351
1374
  async getPage(slug) {
1375
+ if (draft && previewToken && tokenSlug(previewToken) === slug) {
1376
+ const url = `${apiUrl}/api/v1/preview/${encodeURIComponent(slug)}${qs({
1377
+ token: previewToken,
1378
+ stega: stega ? "1" : void 0
1379
+ })}`;
1380
+ const d = unwrap(await fetchJSON(url));
1381
+ return {
1382
+ slug: d.slug,
1383
+ title: d.title,
1384
+ metaTitle: d.metaTitle ?? null,
1385
+ metaDescription: d.metaDescription ?? null,
1386
+ metaJson: d.metaJson ?? null,
1387
+ blocks: d.blockJson ?? [],
1388
+ updatedAt: d.updatedAt ?? null,
1389
+ publishedAt: d.publishedAt ?? null
1390
+ };
1391
+ }
1352
1392
  let page = 1;
1353
1393
  for (; ; ) {
1354
1394
  const list = await this.listPages({ page, perPage: 100 });
@@ -1365,6 +1405,13 @@ function createClient(options) {
1365
1405
  })}`;
1366
1406
  return unwrap(await fetchJSON(url));
1367
1407
  },
1408
+ async listComponents() {
1409
+ const url = `${base}/components${qs({
1410
+ perspective: draft ? "draft" : void 0,
1411
+ token: draft ? previewToken : void 0
1412
+ })}`;
1413
+ return unwrap(await fetchJSON(url));
1414
+ },
1368
1415
  async getLayout() {
1369
1416
  const url = `${base}/layout${draft ? "?perspective=draft" : ""}`;
1370
1417
  try {