- Add debounced state updates for title and content (500ms delay) - Immediate UI updates with delayed history saving - Prevent one-letter-per-undo issue - Add cleanup for debounce timers on unmount
73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
// src/utils/body.ts
|
|
import { HonoRequest } from "../request.js";
|
|
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
const { all = false, dot = false } = options;
|
|
const headers = request instanceof HonoRequest ? request.raw.headers : request.headers;
|
|
const contentType = headers.get("Content-Type");
|
|
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) {
|
|
return parseFormData(request, { all, dot });
|
|
}
|
|
return {};
|
|
};
|
|
async function parseFormData(request, options) {
|
|
const formData = await request.formData();
|
|
if (formData) {
|
|
return convertFormDataToBodyData(formData, options);
|
|
}
|
|
return {};
|
|
}
|
|
function convertFormDataToBodyData(formData, options) {
|
|
const form = /* @__PURE__ */ Object.create(null);
|
|
formData.forEach((value, key) => {
|
|
const shouldParseAllValues = options.all || key.endsWith("[]");
|
|
if (!shouldParseAllValues) {
|
|
form[key] = value;
|
|
} else {
|
|
handleParsingAllValues(form, key, value);
|
|
}
|
|
});
|
|
if (options.dot) {
|
|
Object.entries(form).forEach(([key, value]) => {
|
|
const shouldParseDotValues = key.includes(".");
|
|
if (shouldParseDotValues) {
|
|
handleParsingNestedValues(form, key, value);
|
|
delete form[key];
|
|
}
|
|
});
|
|
}
|
|
return form;
|
|
}
|
|
var handleParsingAllValues = (form, key, value) => {
|
|
if (form[key] !== void 0) {
|
|
if (Array.isArray(form[key])) {
|
|
;
|
|
form[key].push(value);
|
|
} else {
|
|
form[key] = [form[key], value];
|
|
}
|
|
} else {
|
|
if (!key.endsWith("[]")) {
|
|
form[key] = value;
|
|
} else {
|
|
form[key] = [value];
|
|
}
|
|
}
|
|
};
|
|
var handleParsingNestedValues = (form, key, value) => {
|
|
let nestedForm = form;
|
|
const keys = key.split(".");
|
|
keys.forEach((key2, index) => {
|
|
if (index === keys.length - 1) {
|
|
nestedForm[key2] = value;
|
|
} else {
|
|
if (!nestedForm[key2] || typeof nestedForm[key2] !== "object" || Array.isArray(nestedForm[key2]) || nestedForm[key2] instanceof File) {
|
|
nestedForm[key2] = /* @__PURE__ */ Object.create(null);
|
|
}
|
|
nestedForm = nestedForm[key2];
|
|
}
|
|
});
|
|
};
|
|
export {
|
|
parseBody
|
|
};
|