import 'server-only' import type { BasePayload } from 'payload' import type { ClientIpOptions } from '../shared/rateLimit.js' import type { SubmitFormArgs, SubmitFormResult } from './submitForm.js' import { getNotificationTexts } from '../notifications/getNotificationTexts.js' import { resolveFormMessage } from '../notifications/resolveFormMessage.js' import { clientIp } from '../shared/rateLimit.js' import { HONEYPOT_FIELD, isHoneypotTripped } from './honeypot.js' import { submitForm } from './submitForm.js' type CreateSubmitActionArgs = { /** Optional: IP options (default IPAL_IP_HEADER env / clientIp defaults). */ clientIp?: ClientIpOptions getCachedPayload: () => Promise /** * `headers` from 'next/headers', INJECTED (the plugin never imports next/*, * same pattern as NextResponse in createProxy). Called inside the action only. */ headers: () => Headers | Promise /** Honeypot field name. Default HONEYPOT_FIELD. */ honeypotField?: string } & Pick export type SubmitActionResult = { field?: string /** Text for the visitor, from Notifications in this locale. */ message: string /** Why it failed — for the renderer to mark a field (`field`) or reset Turnstile. */ reason?: Exclude['reason'] success: boolean } /** * The one server action every panel form posts to — honeypot → client IP → * submitForm (rate limit, Turnstile, schema validation, GDPR consent, storage) * → the visitor message from Notifications in the page's locale. * * // src/app/(frontend)/[locale]/actions.ts * 'use server' * import { headers } from 'next/headers' * import { createSubmitAction } from '@intecion/ipal-kit/server' * import { getCachedPayload } from '@/lib/content' * export const sendForm = createSubmitAction({ getCachedPayload, headers }) * * Signature matches the ipal-form FormRenderer: `sendForm(formId, locale, values, * turnstileToken?)` — bind formId and locale in the block (`sendForm.bind(null, id, locale)`). * A tripped honeypot gets a fake success and stores nothing. */ export function createSubmitAction({ clientIp: ipOptions, consentFieldName, getCachedPayload, headers, honeypotField = HONEYPOT_FIELD, maxPerMinute, turnstileOnConfigError, }: CreateSubmitActionArgs) { return async function sendForm( formId: string, locale: string, values: Record, turnstileToken?: string, ): Promise { const payload = await getCachedPayload() const texts = (await getNotificationTexts({ locale, payload })).form const data = values && typeof values === 'object' ? values : {} if (isHoneypotTripped(data, honeypotField)) {return { message: texts.success, success: true }} const { [honeypotField]: _honeypot, ...fields } = data const ip = clientIp(await headers(), ipOptions) || undefined const result = await submitForm({ consentFieldName, data: fields, formId: String(formId), ip, maxPerMinute, payload, turnstileOnConfigError, turnstileToken: typeof turnstileToken === 'string' ? turnstileToken.trim() || undefined : undefined, }) const message = resolveFormMessage(result, texts) if (result.success) {return { message, success: true }} return { message, reason: result.reason, success: false, ...('field' in result && result.field ? { field: result.field } : {}), } } }