@webpresso/forms
v0.1.1
Published
Webpresso drop-in form components: a provider-neutral contact-form handler for edge workers.
Readme
@webpresso/forms
Contact-form submission handling for Workers: field validation and sanitization, captcha gating, internal + confirmation email dispatch, and redirect responses with a status vocabulary. Provider-neutral root surface; the captcha vendor is confined to a single adapter subpath.
Install
vp add @webpresso/formsThe public package names and release policy are defined by .webpresso/package-surface.json in the framework repository.
Config shape
import type { ContactFormConfig } from '@webpresso/forms'
const config: ContactFormConfig = {
siteName: 'Example',
from: '[email protected]',
internalRecipients: ['[email protected]'],
fields: [
{ name: 'name', label: 'Name', type: 'text', required: true },
{ name: 'email', label: 'Email', type: 'email', required: true },
{ name: 'message', label: 'Message', type: 'textarea', required: true },
],
customerEmailField: 'email',
customerNameField: 'name',
redirectPath: '/contact',
subjects: { internal: 'New contact request', confirmation: 'We received your message' },
}Field types are text | email | textarea | tel; values are control-character-stripped, whitespace-collapsed, length-capped (maxLength, default 2000), and email-typed fields are lowercased. Required fields and the customer email field are validated before any email is sent.
Usage
Mount the handler in a Worker with an email binding and a captcha verifier:
import { createContactFormHandler, type EmailBinding } from '@webpresso/forms'
import { createTurnstileVerifier } from '@webpresso/forms/adapters/turnstile'
type Env = { TURNSTILE_SECRET: string; SEND_EMAIL: EmailBinding }
export default {
fetch: (request: Request, env: Env) => {
const url = new URL(request.url)
if (url.pathname === '/api/contact' && request.method === 'POST') {
const contact = createContactFormHandler(config, {
captcha: createTurnstileVerifier({ secret: env.TURNSTILE_SECRET }),
})
return contact(request, { email: env.SEND_EMAIL })
}
return new Response('Not found', { status: 404 })
},
}context.email is an EmailBinding (send(message)); when it is absent the handler redirects with the email status instead of sending. A custom CaptchaVerifier can replace the adapter at the neutral captcha option.
Redirect status vocabulary
The handler answers with a 303 redirect carrying a contact query parameter. A form's optional _redirect field is accepted only if its decoded value starts with /, does not start with //, and contains no backslashes or control characters (U+0000–U+001F or U+007F). Otherwise, or when the field is missing, the handler uses config.redirectPath. Wrong-method and unparsable-form requests also use config.redirectPath.
| Value | Meaning |
| --------- | ------------------------------------------------------------ |
| success | Validation and captcha passed; internal email send succeeded |
| invalid | Wrong method, unparsable form, or bad fields |
| captcha | Captcha failed or the verifier errored |
| email | No email binding, or the internal send failed |
After the internal send succeeds, the handler attempts the confirmation email on a best-effort basis. A confirmation send failure still yields success unless the optional logger itself throws. Supply a logger, such as logger: console, in the handler options to receive a contact_confirmation_failed warning with the error code and site name; without a logger, no warning is emitted.
Adapter-confined naming
The root surface stays provider-neutral: vendor names appear only where the adapter subpath is documented. @webpresso/forms/adapters/turnstile is the single subpath naming the captcha vendor, its siteverify endpoint, and its form field name — mirroring how the runtime documents its auth adapter imports. Import the adapter explicitly; nothing vendor-specific is re-exported from the root.
Provenance
Logic ported from the in-org public ozby/cloudflare-utils repository, pinned at commit dd4cb995c8b786f30b5b2993cd40b05e2a73bb56. The source repository carries no LICENSE file and the module is authored in-org by the rights holder, so this port is licensed Elastic-2.0 as part of the framework inventory.
License
Elastic-2.0
