remix-utils
Version:
This package contains simple utility functions to use with [React Router](https://reactrouter.com/).
47 lines • 2.1 kB
JavaScript
import { Honeypot, SpamError } from "../honeypot.js";
export function unstable_createHoneypotMiddleware({ onSpam, ...options } = {}) {
let honeypot = new Honeypot(options);
return [
async function middleware({ request }, next) {
// If there's no body, we can skip the honeypot check (e.g. GET requests)
if (request.body === null)
return await next();
// If the request is not a form submission, we can skip the honeypot check
// (e.g. JSON or other types of requests)
if (!isFormData(request))
return await next();
try {
let formData = await request.clone().formData();
await honeypot.check(formData);
}
catch (error) {
// If the error is a SpamError, we can handle it here and return a
// custom response if provided.
// Otherwise, we throw the error to be handled by the next middleware
// or the error boundary.
// This allows us to customize the response for spam errors while still
// allowing other errors to propagate normally.
if (error instanceof SpamError && onSpam)
return onSpam(error);
throw error;
}
return await next();
},
function getInputProps() {
return honeypot.getInputProps();
},
];
// Check if the request is a form submission by checking the content-type
// This is needed because the Honeypot class only accepts FormData and we want
// to prevent unnecessarily parsing of the request body for other types of
// requests.
function isFormData(request) {
let contentType = request.headers.get("content-type") ?? "";
if (contentType.includes("multipart/form-data"))
return true;
if (contentType.includes("application/x-www-form-urlencoded"))
return true;
return false;
}
}
//# sourceMappingURL=honeypot.js.map