@bettercms-ai/sdk 1.42.0 → 1.43.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.
@@ -1,4 +1,4 @@
1
- import './read-client-TTj8g07j.js';
1
+ import './read-client-DHQb3lve.js';
2
2
  import { RichTextResolved, RichTextEmbeds } from '@bettercms-ai/types';
3
3
 
4
4
  /**
@@ -168,8 +168,8 @@ interface SearchHit {
168
168
  type: "page" | "entry";
169
169
  /** HTML-safe snippet with <mark> highlights around matched terms. */
170
170
  snippet: string;
171
- /** Site-relative URL, e.g. "/about". */
172
- url: string;
171
+ /** Site-relative URL, e.g. "/about". Absent on an entry no page of the site serves. */
172
+ url?: string;
173
173
  }
174
174
  interface SearchOptions {
175
175
  /** The project id (from the dashboard's Site Search settings). */
@@ -1,4 +1,4 @@
1
- import './read-client-TTj8g07j.cjs';
1
+ import './read-client-DHQb3lve.cjs';
2
2
  import { RichTextResolved, RichTextEmbeds } from '@bettercms-ai/types';
3
3
 
4
4
  /**
@@ -168,8 +168,8 @@ interface SearchHit {
168
168
  type: "page" | "entry";
169
169
  /** HTML-safe snippet with <mark> highlights around matched terms. */
170
170
  snippet: string;
171
- /** Site-relative URL, e.g. "/about". */
172
- url: string;
171
+ /** Site-relative URL, e.g. "/about". Absent on an entry no page of the site serves. */
172
+ url?: string;
173
173
  }
174
174
  interface SearchOptions {
175
175
  /** The project id (from the dashboard's Site Search settings). */
@@ -81,7 +81,7 @@ declare class BetterCMSError extends Error {
81
81
  * every published site while the builder happily offers it. `radio`/`checkboxes`
82
82
  * were exactly that gap.
83
83
  */
84
- type DeliveryFormFieldType = "text" | "email" | "phone" | "url" | "number" | "date" | "textarea" | "select" | "radio" | "checkboxes" | "checkbox" | "consent" | "hidden" | "step";
84
+ type DeliveryFormFieldType = "text" | "email" | "phone" | "url" | "number" | "date" | "textarea" | "select" | "radio" | "checkboxes" | "checkbox" | "consent" | "hidden" | "file" | "step";
85
85
  interface DeliveryFormField {
86
86
  key: string;
87
87
  label: string;
@@ -180,6 +180,12 @@ interface DeliveryFormFieldValidation {
180
180
  max?: number;
181
181
  phoneFormat?: "any" | "e164";
182
182
  pattern?: string;
183
+ /** `file` — accepted extensions, lowercase, no dot. */
184
+ accept?: string[];
185
+ /** `file` — files per field. */
186
+ maxFiles?: number;
187
+ /** `file` — per-file cap in MB. */
188
+ maxSizeMb?: number;
183
189
  }
184
190
  /** The pick-many type, whose value is an array rather than a scalar. */
185
191
  declare function isMultiValueField(type: DeliveryFormFieldType): boolean;
@@ -211,6 +217,44 @@ type FormValues = Record<string, FormValue>;
211
217
  declare function asStringArray(value: FormValue | undefined): string[];
212
218
  /** Add/remove `option` from a `checkboxes` value, preserving the field's option order. */
213
219
  declare function toggleOption(value: FormValue | undefined, option: string, on: boolean): string[];
220
+
221
+ /** Why a chosen file is refused before upload, or null: a sentence the visitor can read. */
222
+ declare function fileFieldRefusal(limits: {
223
+ accept: string[];
224
+ maxSizeMb: number;
225
+ }, file: {
226
+ name: string;
227
+ size: number;
228
+ }): string | null;
229
+ interface UploadFormFileOptions {
230
+ formId: string;
231
+ /** The `file` field's key. */
232
+ fieldKey: string;
233
+ /** A browser `File` (anything with a name and a body). */
234
+ file: Blob & {
235
+ name: string;
236
+ };
237
+ /** API origin. Defaults to https://api.bettercms.ai. */
238
+ baseUrl?: string;
239
+ /** Injectable fetch for tests / non-browser runtimes. */
240
+ fetchImpl?: typeof fetch;
241
+ }
242
+ interface UploadedFormFile {
243
+ /** What the submission names this file by: put it in the field's value. */
244
+ id: string;
245
+ name: string;
246
+ size: number;
247
+ }
248
+ /**
249
+ * Upload one file of a form's `file` field, ahead of the submit. Resolves with the id the
250
+ * submission names it by (`data[fieldKey] = [id, …]`), or throws a BetterCMSError whose
251
+ * `bodyCode` is the server's (FILE_TOO_LARGE, UPLOAD_TYPE_NOT_ALLOWED, UPLOAD_BUSY,
252
+ * STORAGE_FULL, …) and whose message is a sentence for the visitor. No retry.
253
+ *
254
+ * The body is the file's bytes and nothing else; its name travels percent-encoded in a header,
255
+ * so it never lands in an access log.
256
+ */
257
+ declare function uploadFormFile(opts: UploadFormFileOptions): Promise<UploadedFormFile>;
214
258
  /**
215
259
  * Conditional visibility. A `showIf` field is shown only when its trigger field's
216
260
  * current value equals the target; non-conditional fields are always shown. The
@@ -367,4 +411,4 @@ interface BetterCMSReadClient {
367
411
  }
368
412
  declare function createClient(options: CreateClientOptions): BetterCMSReadClient;
369
413
 
370
- export { type BetterCMSReadClient as B, type CreateClientOptions as C, type DeliveryFormField as D, ErrorCodes as E, type FormSubmitError as F, type GetEntryOptions as G, type ListEntriesOptions as L, type SubmitFormOptions as S, BetterCMSError as a, type DeliveryForm as b, type DeliveryFormFieldType as c, type FormValue as d, type FormValues as e, type ListPagesOptions as f, type SubmitFormResult as g, asStringArray as h, createClient as i, formInitialValues as j, isMultiValueField as k, submitForm as l, shouldShowField as s, toggleOption as t };
414
+ export { type BetterCMSReadClient as B, type CreateClientOptions as C, type DeliveryFormField as D, ErrorCodes as E, type FormSubmitError as F, type GetEntryOptions as G, type ListEntriesOptions as L, type SubmitFormOptions as S, type UploadFormFileOptions as U, BetterCMSError as a, type DeliveryForm as b, type DeliveryFormFieldType as c, type FormValue as d, type FormValues as e, type ListPagesOptions as f, type SubmitFormResult as g, type UploadedFormFile as h, asStringArray as i, createClient as j, fileFieldRefusal as k, formInitialValues as l, isMultiValueField as m, submitForm as n, shouldShowField as s, toggleOption as t, uploadFormFile as u };
@@ -81,7 +81,7 @@ declare class BetterCMSError extends Error {
81
81
  * every published site while the builder happily offers it. `radio`/`checkboxes`
82
82
  * were exactly that gap.
83
83
  */
84
- type DeliveryFormFieldType = "text" | "email" | "phone" | "url" | "number" | "date" | "textarea" | "select" | "radio" | "checkboxes" | "checkbox" | "consent" | "hidden" | "step";
84
+ type DeliveryFormFieldType = "text" | "email" | "phone" | "url" | "number" | "date" | "textarea" | "select" | "radio" | "checkboxes" | "checkbox" | "consent" | "hidden" | "file" | "step";
85
85
  interface DeliveryFormField {
86
86
  key: string;
87
87
  label: string;
@@ -180,6 +180,12 @@ interface DeliveryFormFieldValidation {
180
180
  max?: number;
181
181
  phoneFormat?: "any" | "e164";
182
182
  pattern?: string;
183
+ /** `file` — accepted extensions, lowercase, no dot. */
184
+ accept?: string[];
185
+ /** `file` — files per field. */
186
+ maxFiles?: number;
187
+ /** `file` — per-file cap in MB. */
188
+ maxSizeMb?: number;
183
189
  }
184
190
  /** The pick-many type, whose value is an array rather than a scalar. */
185
191
  declare function isMultiValueField(type: DeliveryFormFieldType): boolean;
@@ -211,6 +217,44 @@ type FormValues = Record<string, FormValue>;
211
217
  declare function asStringArray(value: FormValue | undefined): string[];
212
218
  /** Add/remove `option` from a `checkboxes` value, preserving the field's option order. */
213
219
  declare function toggleOption(value: FormValue | undefined, option: string, on: boolean): string[];
220
+
221
+ /** Why a chosen file is refused before upload, or null: a sentence the visitor can read. */
222
+ declare function fileFieldRefusal(limits: {
223
+ accept: string[];
224
+ maxSizeMb: number;
225
+ }, file: {
226
+ name: string;
227
+ size: number;
228
+ }): string | null;
229
+ interface UploadFormFileOptions {
230
+ formId: string;
231
+ /** The `file` field's key. */
232
+ fieldKey: string;
233
+ /** A browser `File` (anything with a name and a body). */
234
+ file: Blob & {
235
+ name: string;
236
+ };
237
+ /** API origin. Defaults to https://api.bettercms.ai. */
238
+ baseUrl?: string;
239
+ /** Injectable fetch for tests / non-browser runtimes. */
240
+ fetchImpl?: typeof fetch;
241
+ }
242
+ interface UploadedFormFile {
243
+ /** What the submission names this file by: put it in the field's value. */
244
+ id: string;
245
+ name: string;
246
+ size: number;
247
+ }
248
+ /**
249
+ * Upload one file of a form's `file` field, ahead of the submit. Resolves with the id the
250
+ * submission names it by (`data[fieldKey] = [id, …]`), or throws a BetterCMSError whose
251
+ * `bodyCode` is the server's (FILE_TOO_LARGE, UPLOAD_TYPE_NOT_ALLOWED, UPLOAD_BUSY,
252
+ * STORAGE_FULL, …) and whose message is a sentence for the visitor. No retry.
253
+ *
254
+ * The body is the file's bytes and nothing else; its name travels percent-encoded in a header,
255
+ * so it never lands in an access log.
256
+ */
257
+ declare function uploadFormFile(opts: UploadFormFileOptions): Promise<UploadedFormFile>;
214
258
  /**
215
259
  * Conditional visibility. A `showIf` field is shown only when its trigger field's
216
260
  * current value equals the target; non-conditional fields are always shown. The
@@ -367,4 +411,4 @@ interface BetterCMSReadClient {
367
411
  }
368
412
  declare function createClient(options: CreateClientOptions): BetterCMSReadClient;
369
413
 
370
- export { type BetterCMSReadClient as B, type CreateClientOptions as C, type DeliveryFormField as D, ErrorCodes as E, type FormSubmitError as F, type GetEntryOptions as G, type ListEntriesOptions as L, type SubmitFormOptions as S, BetterCMSError as a, type DeliveryForm as b, type DeliveryFormFieldType as c, type FormValue as d, type FormValues as e, type ListPagesOptions as f, type SubmitFormResult as g, asStringArray as h, createClient as i, formInitialValues as j, isMultiValueField as k, submitForm as l, shouldShowField as s, toggleOption as t };
414
+ export { type BetterCMSReadClient as B, type CreateClientOptions as C, type DeliveryFormField as D, ErrorCodes as E, type FormSubmitError as F, type GetEntryOptions as G, type ListEntriesOptions as L, type SubmitFormOptions as S, type UploadFormFileOptions as U, BetterCMSError as a, type DeliveryForm as b, type DeliveryFormFieldType as c, type FormValue as d, type FormValues as e, type ListPagesOptions as f, type SubmitFormResult as g, type UploadedFormFile as h, asStringArray as i, createClient as j, fileFieldRefusal as k, formInitialValues as l, isMultiValueField as m, submitForm as n, shouldShowField as s, toggleOption as t, uploadFormFile as u };
package/dist/read.cjs CHANGED
@@ -22,6 +22,8 @@ var read_exports = {};
22
22
  __export(read_exports, {
23
23
  asStringArray: () => asStringArray,
24
24
  createClient: () => createClient,
25
+ fileFieldLimits: () => import_types.fileFieldLimits,
26
+ fileFieldRefusal: () => fileFieldRefusal,
25
27
  formInitialValues: () => formInitialValues,
26
28
  isMultiValueField: () => isMultiValueField,
27
29
  isRichText: () => isRichText,
@@ -36,7 +38,8 @@ __export(read_exports, {
36
38
  search: () => search,
37
39
  shouldShowField: () => shouldShowField,
38
40
  submitForm: () => submitForm,
39
- toggleOption: () => toggleOption
41
+ toggleOption: () => toggleOption,
42
+ uploadFormFile: () => uploadFormFile
40
43
  });
41
44
  module.exports = __toCommonJS(read_exports);
42
45
 
@@ -383,6 +386,7 @@ function createClient(options) {
383
386
  }
384
387
 
385
388
  // src/forms.ts
389
+ var import_types = require("@bettercms-ai/types");
386
390
  function isMultiValueField(type) {
387
391
  return type === "checkboxes";
388
392
  }
@@ -396,6 +400,41 @@ function toggleOption(value, option, on) {
396
400
  return current.filter((v) => v !== option);
397
401
  }
398
402
  var DEFAULT_BASE_URL = "https://api.bettercms.ai";
403
+ function fileFieldRefusal(limits, file) {
404
+ const ext = (0, import_types.fileExtension)(file.name);
405
+ if (!ext || !limits.accept.includes(ext)) {
406
+ return `This type of file is not accepted here. Accepted: ${limits.accept.map((e) => `.${e}`).join(", ")}.`;
407
+ }
408
+ if (file.size === 0) return "This file is empty.";
409
+ if (file.size > limits.maxSizeMb * 1024 * 1024) return `This file is larger than ${limits.maxSizeMb} MB.`;
410
+ return null;
411
+ }
412
+ async function uploadFormFile(opts) {
413
+ const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
414
+ const doFetch = opts.fetchImpl ?? globalThis.fetch;
415
+ let res;
416
+ try {
417
+ res = await doFetch(
418
+ `${baseUrl}/api/v1/forms/public/${encodeURIComponent(opts.formId)}/uploads?field=${encodeURIComponent(opts.fieldKey)}`,
419
+ {
420
+ method: "POST",
421
+ headers: { "Content-Type": "application/octet-stream", "X-Bcms-Filename": encodeURIComponent(opts.file.name) },
422
+ body: opts.file
423
+ }
424
+ );
425
+ } catch (err) {
426
+ throw new BetterCMSError(
427
+ `Network error: ${err instanceof Error ? err.message : String(err)}`,
428
+ 0,
429
+ ErrorCodes.NETWORK_ERROR
430
+ );
431
+ }
432
+ if (!res.ok) throw await BetterCMSError.from(res);
433
+ const json = await res.json().catch(() => ({}));
434
+ const data = json.data;
435
+ if (!data?.id) throw new BetterCMSError("The file could not be saved. Please try again.", res.status, ErrorCodes.INTERNAL_ERROR);
436
+ return { id: data.id, name: data.name ?? opts.file.name, size: data.size ?? opts.file.size };
437
+ }
399
438
  function shouldShowField(field, values) {
400
439
  if (!field.showIf) return true;
401
440
  const trigger = values[field.showIf.field];
@@ -406,6 +445,10 @@ function formInitialValues(form, prefill = {}) {
406
445
  const values = {};
407
446
  for (const field of form.fields) {
408
447
  if (field.type === "step") continue;
448
+ if (field.type === "file") {
449
+ values[field.key] = [];
450
+ continue;
451
+ }
409
452
  if (field.type === "checkbox" || field.type === "consent") {
410
453
  values[field.key] = false;
411
454
  } else if (field.type === "checkboxes") {
@@ -547,6 +590,8 @@ function resetPublishedContentCache() {
547
590
  0 && (module.exports = {
548
591
  asStringArray,
549
592
  createClient,
593
+ fileFieldLimits,
594
+ fileFieldRefusal,
550
595
  formInitialValues,
551
596
  isMultiValueField,
552
597
  isRichText,
@@ -561,6 +606,7 @@ function resetPublishedContentCache() {
561
606
  search,
562
607
  shouldShowField,
563
608
  submitForm,
564
- toggleOption
609
+ toggleOption,
610
+ uploadFormFile
565
611
  });
566
612
  //# sourceMappingURL=read.cjs.map