formanitor 0.1.5 → 0.1.6

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
@@ -1745,6 +1745,8 @@ var FormStore = class {
1745
1745
  updateConfig(next) {
1746
1746
  let needsReeval = false;
1747
1747
  if (next.onUpload !== void 0) this.config.onUpload = next.onUpload;
1748
+ if (next.onFileUpload !== void 0)
1749
+ this.config.onFileUpload = next.onFileUpload;
1748
1750
  if (next.onEvent !== void 0) this.config.onEvent = next.onEvent;
1749
1751
  if (next.onDraftChange !== void 0) this.config.onDraftChange = next.onDraftChange;
1750
1752
  if (next.voice !== void 0) this.config.voice = next.voice;
@@ -1765,6 +1767,10 @@ var FormStore = class {
1765
1767
  getUploadHandler() {
1766
1768
  return this.config.onUpload;
1767
1769
  }
1770
+ /** Resolved handler for `file_upload` widgets: `onFileUpload` ?? `onUpload`. */
1771
+ getFileUploadHandler() {
1772
+ return this.config.onFileUpload ?? this.config.onUpload;
1773
+ }
1768
1774
  preSubmitHandlers = /* @__PURE__ */ new Map();
1769
1775
  registerPreSubmitHandler(fieldId, fn) {
1770
1776
  this.preSubmitHandlers.set(fieldId, fn);
@@ -4073,7 +4079,7 @@ var FormFileUploadWidget = ({
4073
4079
  const accept = def.accept ?? "*/*";
4074
4080
  const maxSize = def.maxSize ?? 10 * 1024 * 1024;
4075
4081
  const allowMultiple = def.multiple === true;
4076
- const uploadHandler = store.getUploadHandler();
4082
+ const uploadHandler = store.getFileUploadHandler();
4077
4083
  const showLabel = isLabelVisible(fieldDef);
4078
4084
  if (!uploadHandler) {
4079
4085
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: fieldWrapperClass(fieldDef, "space-y-2"), children: [
@@ -4082,7 +4088,7 @@ var FormFileUploadWidget = ({
4082
4088
  " ",
4083
4089
  /* @__PURE__ */ jsxRuntime.jsx(FieldRequiredIndicator, { fieldDef })
4084
4090
  ] }) : null,
4085
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "border-2 border-red-300 rounded-lg p-4 bg-red-50", children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm text-red-600", children: "Upload handler not configured. Please provide an onUpload function in FormProvider config." }) })
4091
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "border-2 border-red-300 rounded-lg p-4 bg-red-50", children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm text-red-600", children: "Upload handler not configured. Provide `config.onFileUpload` or `config.onUpload` on FormProvider." }) })
4086
4092
  ] });
4087
4093
  }
4088
4094
  const hasStoredValue = value !== null && value !== void 0 && value !== "" && !(Array.isArray(value) && value.length === 0);
@@ -16322,33 +16328,52 @@ var FormControls = ({
16322
16328
  };
16323
16329
 
16324
16330
  // src/utils/createUploadHandler.ts
16325
- function createUploadHandler(options) {
16331
+ function normalizeBaseUrl(apiBaseUrl) {
16332
+ return apiBaseUrl.replace(/\/+$/, "");
16333
+ }
16334
+ async function uploadViaPresignedEndpoint(options, presignPath, file, filename) {
16326
16335
  const { apiBaseUrl, headers = {} } = options;
16327
- return async (file, filename) => {
16328
- const response = await fetch(`${apiBaseUrl}/api/forms/media-upload/`, {
16329
- method: "POST",
16330
- headers: {
16331
- "Content-Type": "application/json",
16332
- ...headers
16333
- },
16334
- body: JSON.stringify({ filename })
16335
- });
16336
- if (!response.ok) {
16337
- throw new Error(`Failed to get presigned URL: ${response.statusText}`);
16338
- }
16339
- const presignedData = await response.json();
16340
- const uploadResponse = await fetch(presignedData.presigned_url, {
16341
- method: presignedData.upload_method,
16342
- headers: {
16343
- "Content-Type": presignedData.content_type
16344
- },
16345
- body: file
16346
- });
16347
- if (!uploadResponse.ok) {
16348
- throw new Error(`Failed to upload file: ${uploadResponse.statusText}`);
16349
- }
16350
- return presignedData.s3_key;
16351
- };
16336
+ const base = normalizeBaseUrl(apiBaseUrl);
16337
+ const path = presignPath.startsWith("/") ? presignPath : `/${presignPath}`;
16338
+ const response = await fetch(`${base}${path}`, {
16339
+ method: "POST",
16340
+ headers: {
16341
+ "Content-Type": "application/json",
16342
+ ...headers
16343
+ },
16344
+ body: JSON.stringify({ filename })
16345
+ });
16346
+ if (!response.ok) {
16347
+ throw new Error(`Failed to get presigned URL: ${response.statusText}`);
16348
+ }
16349
+ const presignedData = await response.json();
16350
+ const uploadResponse = await fetch(presignedData.presigned_url, {
16351
+ method: presignedData.upload_method,
16352
+ headers: {
16353
+ "Content-Type": presignedData.content_type
16354
+ },
16355
+ body: file
16356
+ });
16357
+ if (!uploadResponse.ok) {
16358
+ throw new Error(`Failed to upload file: ${uploadResponse.statusText}`);
16359
+ }
16360
+ return presignedData.s3_key;
16361
+ }
16362
+ function createUploadHandler(options) {
16363
+ return async (file, filename) => uploadViaPresignedEndpoint(
16364
+ options,
16365
+ "/api/forms/media-upload/",
16366
+ file,
16367
+ filename
16368
+ );
16369
+ }
16370
+ function createFileUploadHandler(options) {
16371
+ return async (file, filename) => uploadViaPresignedEndpoint(
16372
+ options,
16373
+ "/api/forms/media-presigned-get/",
16374
+ file,
16375
+ filename
16376
+ );
16352
16377
  }
16353
16378
 
16354
16379
  exports.AddInvestigationField = AddInvestigationField;
@@ -16377,6 +16402,7 @@ exports.SignatureUploadWidget = SignatureUploadWidget;
16377
16402
  exports.SmartForm = SmartForm;
16378
16403
  exports.SmartTextareaWidget = SmartTextareaWidget;
16379
16404
  exports.Upload = Upload3;
16405
+ exports.createFileUploadHandler = createFileUploadHandler;
16380
16406
  exports.createUploadHandler = createUploadHandler;
16381
16407
  exports.evaluateRules = evaluateRules;
16382
16408
  exports.fieldControlClass = fieldControlClass;