@bettercms-ai/sdk 1.42.1 → 1.43.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/README.md CHANGED
@@ -145,6 +145,25 @@ entries, and never stored. An envelope value carries what it resolved to (`value
145
145
  Body stored as a bare block array cannot, so the entry carries the merged map beside `data`
146
146
  (`entry.resolved`). `resolved(value, entry)` returns the value's own map, else the entry's.
147
147
 
148
+ ## Form file fields
149
+
150
+ A `file` field's value is the ids of files uploaded BEFORE the submit, one request per file
151
+ (needs 1.43.0 or later):
152
+
153
+ ```ts
154
+ import { submitForm, uploadFormFile, fileFieldLimits, fileFieldRefusal } from "@bettercms-ai/sdk/read";
155
+
156
+ const field = form.fields.find((f) => f.key === "cv")!;
157
+ const refusal = fileFieldRefusal(fileFieldLimits(field), file); // a courtesy: the site's plan may hold the field to less, and the server decides
158
+ if (refusal) throw new Error(refusal);
159
+ const { id } = await uploadFormFile({ formId: form.id, fieldKey: "cv", file });
160
+ await submitForm({ formId: form.id, data: { cv: [id] } });
161
+ ```
162
+
163
+ `uploadFormFile` sends the file's bytes with its name percent-encoded in `X-Bcms-Filename`; a refusal
164
+ throws with the server's sentence ("This file is larger than 10 MB."). An id belongs to the one
165
+ submission that uses it. Files nobody submits are removed after 24 hours.
166
+
148
167
  ## API
149
168
 
150
169
  ### `BetterCMS.site(options)`
package/dist/index.cjs CHANGED
@@ -64,9 +64,11 @@ __export(index_exports, {
64
64
  embeds: () => embeds,
65
65
  encodeStega: () => encodeStega,
66
66
  entrySeoInput: () => entrySeoInput,
67
+ fileFieldLimits: () => import_types.fileFieldLimits,
68
+ fileFieldRefusal: () => fileFieldRefusal,
67
69
  formInitialValues: () => formInitialValues,
68
70
  getApiKeyUsage: () => getApiKeyUsage,
69
- getBlockType: () => import_types.getBlockType,
71
+ getBlockType: () => import_types2.getBlockType,
70
72
  getEntry: () => getEntry,
71
73
  getForm: () => getForm2,
72
74
  getManagedLayout: () => getManagedLayout,
@@ -83,7 +85,7 @@ __export(index_exports, {
83
85
  imageValueUrl: () => import_image_url.imageValueUrl,
84
86
  inEditor: () => inEditor,
85
87
  inviteMember: () => inviteMember,
86
- isBlock: () => import_types.isBlock,
88
+ isBlock: () => import_types2.isBlock,
87
89
  isMultiValueField: () => isMultiValueField,
88
90
  isRichText: () => isRichText,
89
91
  lines: () => lines,
@@ -132,6 +134,7 @@ __export(index_exports, {
132
134
  updatePage: () => updatePage,
133
135
  updatePageModel: () => updatePageModel,
134
136
  updateWorkspace: () => updateWorkspace,
137
+ uploadFormFile: () => uploadFormFile,
135
138
  uploadMedia: () => uploadMedia
136
139
  });
137
140
  module.exports = __toCommonJS(index_exports);
@@ -732,7 +735,12 @@ var MIME_BY_EXT = {
732
735
  avif: "image/avif",
733
736
  mp4: "video/mp4",
734
737
  webm: "video/webm",
738
+ mov: "video/quicktime",
735
739
  pdf: "application/pdf",
740
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
741
+ // A Lottie animation; the server refuses any other JSON.
742
+ json: "application/json",
743
+ riv: "application/x-rive",
736
744
  // mp3/ogg were server-allowed all along; the SDK sent application/octet-stream for them,
737
745
  // which the allowlist then refused — so an agent could never upload audio.
738
746
  mp3: "audio/mpeg",
@@ -2057,6 +2065,7 @@ async function removeMember(client, workspaceId, memberId) {
2057
2065
  }
2058
2066
 
2059
2067
  // src/forms.ts
2068
+ var import_types = require("@bettercms-ai/types");
2060
2069
  function isMultiValueField(type) {
2061
2070
  return type === "checkboxes";
2062
2071
  }
@@ -2070,6 +2079,41 @@ function toggleOption(value, option, on) {
2070
2079
  return current.filter((v) => v !== option);
2071
2080
  }
2072
2081
  var DEFAULT_BASE_URL5 = "https://api.bettercms.ai";
2082
+ function fileFieldRefusal(limits, file) {
2083
+ const ext = (0, import_types.fileExtension)(file.name);
2084
+ if (!ext || !limits.accept.includes(ext)) {
2085
+ return `This type of file is not accepted here. Accepted: ${limits.accept.map((e) => `.${e}`).join(", ")}.`;
2086
+ }
2087
+ if (file.size === 0) return "This file is empty.";
2088
+ if (file.size > limits.maxSizeMb * 1024 * 1024) return `This file is larger than ${limits.maxSizeMb} MB.`;
2089
+ return null;
2090
+ }
2091
+ async function uploadFormFile(opts) {
2092
+ const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL5).replace(/\/+$/, "");
2093
+ const doFetch = opts.fetchImpl ?? globalThis.fetch;
2094
+ let res;
2095
+ try {
2096
+ res = await doFetch(
2097
+ `${baseUrl}/api/v1/forms/public/${encodeURIComponent(opts.formId)}/uploads?field=${encodeURIComponent(opts.fieldKey)}`,
2098
+ {
2099
+ method: "POST",
2100
+ headers: { "Content-Type": "application/octet-stream", "X-Bcms-Filename": encodeURIComponent(opts.file.name) },
2101
+ body: opts.file
2102
+ }
2103
+ );
2104
+ } catch (err) {
2105
+ throw new BetterCMSError(
2106
+ `Network error: ${err instanceof Error ? err.message : String(err)}`,
2107
+ 0,
2108
+ ErrorCodes.NETWORK_ERROR
2109
+ );
2110
+ }
2111
+ if (!res.ok) throw await BetterCMSError.from(res);
2112
+ const json = await res.json().catch(() => ({}));
2113
+ const data = json.data;
2114
+ if (!data?.id) throw new BetterCMSError("The file could not be saved. Please try again.", res.status, ErrorCodes.INTERNAL_ERROR);
2115
+ return { id: data.id, name: data.name ?? opts.file.name, size: data.size ?? opts.file.size };
2116
+ }
2073
2117
  function shouldShowField(field, values) {
2074
2118
  if (!field.showIf) return true;
2075
2119
  const trigger = values[field.showIf.field];
@@ -2080,6 +2124,10 @@ function formInitialValues(form, prefill = {}) {
2080
2124
  const values = {};
2081
2125
  for (const field of form.fields) {
2082
2126
  if (field.type === "step") continue;
2127
+ if (field.type === "file") {
2128
+ values[field.key] = [];
2129
+ continue;
2130
+ }
2083
2131
  if (field.type === "checkbox" || field.type === "consent") {
2084
2132
  values[field.key] = false;
2085
2133
  } else if (field.type === "checkboxes") {
@@ -2121,7 +2169,7 @@ async function submitForm(opts) {
2121
2169
  }
2122
2170
 
2123
2171
  // src/index.ts
2124
- var import_types = require("@bettercms-ai/types");
2172
+ var import_types2 = require("@bettercms-ai/types");
2125
2173
 
2126
2174
  // src/in-editor.ts
2127
2175
  var EDITOR_FRAME_NAME = "bcms-editor";
@@ -2168,6 +2216,8 @@ function inEditor(win = globalThis.window) {
2168
2216
  embeds,
2169
2217
  encodeStega,
2170
2218
  entrySeoInput,
2219
+ fileFieldLimits,
2220
+ fileFieldRefusal,
2171
2221
  formInitialValues,
2172
2222
  getApiKeyUsage,
2173
2223
  getBlockType,
@@ -2236,6 +2286,7 @@ function inEditor(win = globalThis.window) {
2236
2286
  updatePage,
2237
2287
  updatePageModel,
2238
2288
  updateWorkspace,
2289
+ uploadFormFile,
2239
2290
  uploadMedia
2240
2291
  });
2241
2292
  //# sourceMappingURL=index.cjs.map