@insane-forms/core
v0.1.1
Published
Browser client for Insane Forms: spam protection, file uploads and submission in one call.
Readme
@insane-forms/core
The browser client for Insane Forms. It sends a Submission to a Form endpoint, solves the Form's proof of work, checks for a Turnstile token and uploads Files straight to storage. It renders nothing. In a React app, use @insane-forms/react, which is built on this package.
Install
npm install @insane-forms/coreThe package is ESM only and uses browser APIs (fetch, FormData, File), so run it in the browser.
Send a form
import { createFormClient } from "@insane-forms/core";
const form = document.querySelector<HTMLFormElement>("#contact");
const status = document.querySelector<HTMLElement>("#status");
if (!form || !status) throw new Error("Missing form markup");
const client = createFormClient("https://us.insaneforms.com/f/<form-id>");
form.addEventListener(
"focusin",
() => {
client.prepare().catch(() => {
// submit() reports the failure.
});
},
{ once: true },
);
form.addEventListener("submit", async (event) => {
event.preventDefault();
status.textContent = "Sending";
try {
const result = await client.submit(form);
if (result.ok) window.location.assign(result.redirect);
else status.textContent = result.error;
} catch (error) {
status.textContent =
error instanceof Error ? error.message : "Could not send form";
}
});Create one client per Form, with the full endpoint URL the app shows for it. prepare() is optional: it loads the spam protection and starts the proof of work early, so the send is quick when the visitor presses the button.
submit takes a <form> element or FormData and resolves with { ok: true, id, redirect } when the Form accepts the Submission, or { ok: false, error } when the server turns it down. When the Submission cannot be sent at all, for example because the network is down, it rejects with an Error whose message is safe to show. The client never navigates on its own.
If the Site uses Cloudflare Turnstile, pass onTurnstileSiteKey to createFormClient and render the widget with that key inside the form. submit reads the widget's token from the form.
Documentation
Every option, the steps submit takes and each error message are in the @insane-forms/core docs.
License
MIT
