@bettercms-ai/sdk 1.42.1 → 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.
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);
@@ -2057,6 +2060,7 @@ async function removeMember(client, workspaceId, memberId) {
2057
2060
  }
2058
2061
 
2059
2062
  // src/forms.ts
2063
+ var import_types = require("@bettercms-ai/types");
2060
2064
  function isMultiValueField(type) {
2061
2065
  return type === "checkboxes";
2062
2066
  }
@@ -2070,6 +2074,41 @@ function toggleOption(value, option, on) {
2070
2074
  return current.filter((v) => v !== option);
2071
2075
  }
2072
2076
  var DEFAULT_BASE_URL5 = "https://api.bettercms.ai";
2077
+ function fileFieldRefusal(limits, file) {
2078
+ const ext = (0, import_types.fileExtension)(file.name);
2079
+ if (!ext || !limits.accept.includes(ext)) {
2080
+ return `This type of file is not accepted here. Accepted: ${limits.accept.map((e) => `.${e}`).join(", ")}.`;
2081
+ }
2082
+ if (file.size === 0) return "This file is empty.";
2083
+ if (file.size > limits.maxSizeMb * 1024 * 1024) return `This file is larger than ${limits.maxSizeMb} MB.`;
2084
+ return null;
2085
+ }
2086
+ async function uploadFormFile(opts) {
2087
+ const baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL5).replace(/\/+$/, "");
2088
+ const doFetch = opts.fetchImpl ?? globalThis.fetch;
2089
+ let res;
2090
+ try {
2091
+ res = await doFetch(
2092
+ `${baseUrl}/api/v1/forms/public/${encodeURIComponent(opts.formId)}/uploads?field=${encodeURIComponent(opts.fieldKey)}`,
2093
+ {
2094
+ method: "POST",
2095
+ headers: { "Content-Type": "application/octet-stream", "X-Bcms-Filename": encodeURIComponent(opts.file.name) },
2096
+ body: opts.file
2097
+ }
2098
+ );
2099
+ } catch (err) {
2100
+ throw new BetterCMSError(
2101
+ `Network error: ${err instanceof Error ? err.message : String(err)}`,
2102
+ 0,
2103
+ ErrorCodes.NETWORK_ERROR
2104
+ );
2105
+ }
2106
+ if (!res.ok) throw await BetterCMSError.from(res);
2107
+ const json = await res.json().catch(() => ({}));
2108
+ const data = json.data;
2109
+ if (!data?.id) throw new BetterCMSError("The file could not be saved. Please try again.", res.status, ErrorCodes.INTERNAL_ERROR);
2110
+ return { id: data.id, name: data.name ?? opts.file.name, size: data.size ?? opts.file.size };
2111
+ }
2073
2112
  function shouldShowField(field, values) {
2074
2113
  if (!field.showIf) return true;
2075
2114
  const trigger = values[field.showIf.field];
@@ -2080,6 +2119,10 @@ function formInitialValues(form, prefill = {}) {
2080
2119
  const values = {};
2081
2120
  for (const field of form.fields) {
2082
2121
  if (field.type === "step") continue;
2122
+ if (field.type === "file") {
2123
+ values[field.key] = [];
2124
+ continue;
2125
+ }
2083
2126
  if (field.type === "checkbox" || field.type === "consent") {
2084
2127
  values[field.key] = false;
2085
2128
  } else if (field.type === "checkboxes") {
@@ -2121,7 +2164,7 @@ async function submitForm(opts) {
2121
2164
  }
2122
2165
 
2123
2166
  // src/index.ts
2124
- var import_types = require("@bettercms-ai/types");
2167
+ var import_types2 = require("@bettercms-ai/types");
2125
2168
 
2126
2169
  // src/in-editor.ts
2127
2170
  var EDITOR_FRAME_NAME = "bcms-editor";
@@ -2168,6 +2211,8 @@ function inEditor(win = globalThis.window) {
2168
2211
  embeds,
2169
2212
  encodeStega,
2170
2213
  entrySeoInput,
2214
+ fileFieldLimits,
2215
+ fileFieldRefusal,
2171
2216
  formInitialValues,
2172
2217
  getApiKeyUsage,
2173
2218
  getBlockType,
@@ -2236,6 +2281,7 @@ function inEditor(win = globalThis.window) {
2236
2281
  updatePage,
2237
2282
  updatePageModel,
2238
2283
  updateWorkspace,
2284
+ uploadFormFile,
2239
2285
  uploadMedia
2240
2286
  });
2241
2287
  //# sourceMappingURL=index.cjs.map