Skip to content

heymegabyte/agent-skills / 06-build-and-slice-loop

build-and-slice-loop

Verified

Use when implementing features or streaming OnePrompt site builds. Complete vertical slices with real content, strict TypeScript and Zod. Parallel section/data work, deterministic assembly, granular patches and immediate valid previews.

FileKindSize
SKILL.mdskill md7.2 KB
admin-dashboard.mdmarkdown5.5 KB
ai-chat-widget.mdmarkdown6.0 KB
blog-and-content-engine.mdmarkdown6.2 KB
build-breaking-rules.mdmarkdown7.4 KB
chat-native-dashboard.mdmarkdown3.6 KB
contact-forms-and-endpoints.mdmarkdown6.7 KB
copilot-and-ai-features.mdmarkdown7.4 KB
custom-error-pages.mdmarkdown4.5 KB
data-tables.mdmarkdown7.7 KB
domain-provisioning.mdmarkdown4.6 KB
easter-eggs.mdmarkdown4.7 KB
empty-states-and-loading.mdmarkdown5.7 KB
file-uploads-and-storage.mdmarkdown6.7 KB
internationalization.mdmarkdown4.3 KB
keyboard-shortcuts-and-command-palette.mdmarkdown6.7 KB
microcopy-library.mdmarkdown8.0 KB
notification-center.mdmarkdown8.4 KB
onboarding-and-first-run.mdmarkdown5.0 KB
pre-digested-builds.mdmarkdown3.6 KB
pwa-kit.mdmarkdown6.8 KB
realtime-and-websockets.mdmarkdown7.2 KB
rich-text-editor.mdmarkdown7.4 KB
site-search.mdmarkdown4.0 KB
stripe-first-donations.mdmarkdown10.7 KB
web-manifest-system.mdmarkdown11.9 KB
webhook-system.mdmarkdown9.5 KB

contact-forms-and-endpoints.md

View on GitHub
---
name: "Contact Forms and Endpoints"
version: "2.0.0"
updated: "2026-04-23"
description: "Every site gets a working contact form with Turnstile captcha, Zod validation, Resend email delivery, success/error/loading states, and 8-point form test matrix. Auto-creates the /api/contact endpoint, form HTML, and Playwright tests. No form is ever a dead end."
---

# Contact Forms and Endpoints

## Default Contact Form (EVERY site)

### Frontend

```html
<form id="contact" action="/api/contact" method="POST">
  <div class="form-group">
    <label for="name">Your name</label>
    <input id="name" name="name" type="text" required placeholder="Jane Smith" autocomplete="name">
  </div>
  <div class="form-group">
    <label for="email">Your email</label>
    <input id="email" name="email" type="email" required placeholder="jane@example.com" autocomplete="email">
  </div>
  <div class="form-group">
    <label for="message">Message</label>
    <textarea id="message" name="message" required placeholder="How can we help?" rows="5" maxlength="5000"></textarea>
    <span class="char-count"><span id="charCount">0</span>/5000</span>
  </div>
  <div class="cf-turnstile" data-sitekey="${TURNSTILE_SITE_KEY}" data-theme="dark"></div>
  <button type="submit" id="submitBtn">
    <span class="btn-text">Send Message</span>
    <span class="btn-loading" hidden>Sending...</span>
  </button>
  <div id="formSuccess" hidden class="success">Thanks! We'll be in touch shortly.</div>
  <div id="formError" hidden class="error">Something went wrong. Try again?</div>
</form>
```

### Backend (Hono)

```typescript
import { z } from 'zod';
import { Resend } from 'resend';

const ContactSchema = z.object({
  name: z.string().min(1).max(200),
  email: z.string().email(),
  message: z.string().min(1).max(5000),
  'cf-turnstile-response': z.string(),
});

app.post('/api/contact', async (c) => {
  const body = await c.req.parseBody();
  const parsed = ContactSchema.safeParse(body);
  if (!parsed.success) {
    return c.json({ error: 'Invalid input', details: parsed.error.flatten() }, 400);
  }
  const { name, email, message } = parsed.data;

  // Verify Turnstile
  const turnstileOk = await verifyTurnstile(parsed.data['cf-turnstile-response'], c.env);
  if (!turnstileOk) return c.json({ error: 'Captcha failed' }, 403);

  // Rate limit: 3 submissions per IP per hour
  if (!await rateLimit(c, `contact:${c.req.header('cf-connecting-ip')}`, 3, 3600)) {
    return c.json({ error: 'Too many messages. Try again later.' }, 429);
  }

  // Send via Resend
  const resend = new Resend(c.env.RESEND_API_KEY);
  await resend.emails.send({
    from: `${name} via Site <contact@${c.env.DOMAIN}>`,
    to: ['brian@megabyte.space'],
    replyTo: email,
    subject: `Contact from ${name}`,
    html: renderContactEmail({ name, email, message }),
  });

  // Confirmation email to sender
  await resend.emails.send({
    from: `Brian <brian@megabyte.space>`,
    to: [email],
    subject: "Got it — we'll be in touch",
    html: renderConfirmationEmail({ name }),
  });

  // Track in PostHog
  // posthog.capture('form_submit', { form_name: 'contact', success: true });

  return c.json({ success: true });
});
```

### JavaScript (Progressive Enhancement)

```javascript
document.getElementById('contact')?.addEventListener('submit', async (e) => {
  e.preventDefault();
  const form = e.target;
  const btn = form.querySelector('#submitBtn');
  const btnText = btn.querySelector('.btn-text');
  const btnLoading = btn.querySelector('.btn-loading');
  const success = form.querySelector('#formSuccess');
  const error = form.querySelector('#formError');

  btn.disabled = true;
  btnText.hidden = true;
  btnLoading.hidden = false;
  success.hidden = true;
  error.hidden = true;

  try {
    const res = await fetch('/api/contact', {
      method: 'POST',
      body: new FormData(form),
    });
    if (res.ok) {
      success.hidden = false;
      form.reset();
    } else {
      const data = await res.json();
      error.textContent = data.error || 'Something went wrong. Try again?';
      error.hidden = false;
    }
  } catch {
    error.textContent = 'Network error. Check your connection.';
    error.hidden = false;
  } finally {
    btn.disabled = false;
    btnText.hidden = false;
    btnLoading.hidden = true;
  }
});
```

## 8-Point Form Test Matrix (EVERY form)

```typescript
test.describe('Contact form', () => {
  test('1. Empty submission shows errors', async ({ page }) => {
    await page.goto('/contact');
    await page.click('button[type="submit"]');
    // HTML5 validation should prevent submission
  });

  test('2. Invalid email rejected', async ({ page }) => {
    await page.goto('/contact');
    await page.fill('#name', 'Test');
    await page.fill('#email', 'notanemail');
    await page.fill('#message', 'Hello');
    await page.click('button[type="submit"]');
  });

  test('3. XSS sanitized', async ({ page }) => {
    await page.goto('/contact');
    await page.fill('#name', '<script>alert(1)</script>');
    await page.fill('#email', 'test@test.com');
    await page.fill('#message', '<img onerror=alert(1) src=x>');
    await page.click('button[type="submit"]');
    // Zod sanitizes; no script execution
  });

  test('4. Max-length enforced', async ({ page }) => {
    await page.goto('/contact');
    const longText = 'a'.repeat(5001);
    await page.fill('#message', longText);
    const value = await page.inputValue('#message');
    expect(value.length).toBeLessThanOrEqual(5000);
  });

  test('5. Success path works', async ({ page }) => {
    await page.goto('/contact');
    await page.fill('#name', 'Test User');
    await page.fill('#email', 'test@example.com');
    await page.fill('#message', 'Test message from Playwright');
    // Turnstile may block in test — mock or use test key
  });

  test('6. Errors show inline', async ({ page }) => {
    await page.goto('/contact');
    // Verify error messages appear near fields, not as alert()
  });

  test('7. Loading state shows', async ({ page }) => {
    await page.goto('/contact');
    // Submit and check button shows "Sending..." and is disabled
  });

  test('8. Double-submit prevented', async ({ page }) => {
    await page.goto('/contact');
    // Click submit twice rapidly; verify button is disabled after first click
  });
});
```

## Other Form Types

### Newsletter Signup (Listmonk)

```html
<form action="/api/subscribe" method="POST">
  <input type="email" name="email" required placeholder="you@example.com">
  <div class="cf-turnstile" data-sitekey="${KEY}" data-theme="dark"></div>
  <button type="submit">Subscribe</button>
</form>
```

### Donation Form (Stripe — `13/stripe-billing`)

Redirect to Stripe Checkout. No form processing needed on our side.

### Feedback Form (`13/user-feedback-collection`)

In-app widget with rating + text. Stored in D1.
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall