@react-consulting/rc-kit 0.0.0-stage → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +93 -2
- package/devkit/README.md +301 -0
- package/devkit/cli.test.ts +211 -0
- package/devkit/cli.ts +120 -0
- package/devkit/commands/build.ts +127 -0
- package/devkit/commands/checks.ts +123 -0
- package/devkit/commands/context.ts +47 -0
- package/devkit/commands/db.ts +154 -0
- package/devkit/commands/deploy.ts +46 -0
- package/devkit/commands/dev.ts +191 -0
- package/devkit/commands/doctor.ts +25 -0
- package/devkit/commands/kit.ts +45 -0
- package/devkit/commands/new.ts +81 -0
- package/devkit/commands/registry.ts +20 -0
- package/devkit/commands/secrets.ts +254 -0
- package/devkit/lib/args.ts +65 -0
- package/devkit/lib/audit.test.ts +405 -0
- package/devkit/lib/audit.ts +813 -0
- package/devkit/lib/config.test.ts +203 -0
- package/devkit/lib/config.ts +417 -0
- package/devkit/lib/database/index.test.ts +223 -0
- package/devkit/lib/database/index.ts +314 -0
- package/devkit/lib/database/neon.test.ts +233 -0
- package/devkit/lib/database/neon.ts +357 -0
- package/devkit/lib/database/prepare.ts +54 -0
- package/devkit/lib/deploy-config.test.ts +217 -0
- package/devkit/lib/deploy-config.ts +133 -0
- package/devkit/lib/deploy.test.ts +275 -0
- package/devkit/lib/deploy.ts +177 -0
- package/devkit/lib/dev-gateway.test.ts +128 -0
- package/devkit/lib/dev-gateway.ts +162 -0
- package/devkit/lib/dev-menu.test.ts +114 -0
- package/devkit/lib/dev-menu.ts +332 -0
- package/devkit/lib/dev-secrets.test.ts +156 -0
- package/devkit/lib/dev-secrets.ts +97 -0
- package/devkit/lib/dev.test.ts +194 -0
- package/devkit/lib/dev.ts +119 -0
- package/devkit/lib/doctor.test.ts +144 -0
- package/devkit/lib/doctor.ts +264 -0
- package/devkit/lib/env-file.test.ts +102 -0
- package/devkit/lib/env-file.ts +96 -0
- package/devkit/lib/glob.ts +31 -0
- package/devkit/lib/infisical.test.ts +208 -0
- package/devkit/lib/infisical.ts +139 -0
- package/devkit/lib/json-schema.ts +220 -0
- package/devkit/lib/jsonc.test.ts +53 -0
- package/devkit/lib/jsonc.ts +93 -0
- package/devkit/lib/kit-migrate.test.ts +215 -0
- package/devkit/lib/kit-migrate.ts +624 -0
- package/devkit/lib/kit-package.test.ts +83 -0
- package/devkit/lib/kit-package.ts +57 -0
- package/devkit/lib/kit-version.ts +22 -0
- package/devkit/lib/naming.test.ts +547 -0
- package/devkit/lib/naming.ts +796 -0
- package/devkit/lib/paths.ts +7 -0
- package/devkit/lib/process.ts +78 -0
- package/devkit/lib/project-checks.ts +86 -0
- package/devkit/lib/scaffold.test.ts +302 -0
- package/devkit/lib/scaffold.ts +315 -0
- package/devkit/lib/secrets.test.ts +101 -0
- package/devkit/lib/secrets.ts +125 -0
- package/devkit/lib/sourcemaps.test.ts +86 -0
- package/devkit/lib/sourcemaps.ts +98 -0
- package/devkit/lib/test-utils.ts +68 -0
- package/devkit/lib/workspace.ts +87 -0
- package/devkit/lib/wrangler.ts +231 -0
- package/docs/rc-config.md +93 -0
- package/package.json +86 -4
- package/presets/build.d.ts +15 -0
- package/presets/build.js +53 -0
- package/presets/vite-plus.d.ts +28 -0
- package/presets/vite-plus.js +153 -0
- package/registry/README.md +294 -0
- package/registry/public/r/avatar-stack.json +18 -0
- package/registry/public/r/avatar.json +20 -0
- package/registry/public/r/back-link.json +21 -0
- package/registry/public/r/badge.json +17 -0
- package/registry/public/r/button.json +18 -0
- package/registry/public/r/calendar-days.json +14 -0
- package/registry/public/r/calendar-popover.json +24 -0
- package/registry/public/r/calendar.json +23 -0
- package/registry/public/r/callout.json +20 -0
- package/registry/public/r/card.json +18 -0
- package/registry/public/r/character-counter.json +17 -0
- package/registry/public/r/checkbox.json +22 -0
- package/registry/public/r/choice-cards.json +22 -0
- package/registry/public/r/color-tag.json +17 -0
- package/registry/public/r/combobox.json +24 -0
- package/registry/public/r/confirm-button.json +24 -0
- package/registry/public/r/confirm-dialog.json +23 -0
- package/registry/public/r/copy-button.json +24 -0
- package/registry/public/r/date-picker.json +24 -0
- package/registry/public/r/date-range-picker.json +28 -0
- package/registry/public/r/detail-header.json +17 -0
- package/registry/public/r/detail-row.json +17 -0
- package/registry/public/r/dev-inbox-api.json +33 -0
- package/registry/public/r/dev-toolbar.json +109 -0
- package/registry/public/r/dev-tools-contract.json +15 -0
- package/registry/public/r/dialog.json +23 -0
- package/registry/public/r/empty-state.json +20 -0
- package/registry/public/r/error-state.json +23 -0
- package/registry/public/r/eyebrow.json +17 -0
- package/registry/public/r/fab.json +20 -0
- package/registry/public/r/field-error.json +17 -0
- package/registry/public/r/field-grid.json +17 -0
- package/registry/public/r/field.json +19 -0
- package/registry/public/r/file-picker.json +22 -0
- package/registry/public/r/filter-chips.json +17 -0
- package/registry/public/r/form-section.json +17 -0
- package/registry/public/r/form-sheet.json +22 -0
- package/registry/public/r/icon-button.json +23 -0
- package/registry/public/r/input.json +18 -0
- package/registry/public/r/label.json +18 -0
- package/registry/public/r/list-row.json +17 -0
- package/registry/public/r/loading-state.json +18 -0
- package/registry/public/r/localized-field.json +23 -0
- package/registry/public/r/markdown-text.json +17 -0
- package/registry/public/r/menu.json +21 -0
- package/registry/public/r/money-input.json +17 -0
- package/registry/public/r/month-day-select.json +18 -0
- package/registry/public/r/numbered-step.json +20 -0
- package/registry/public/r/page-actions.json +14 -0
- package/registry/public/r/page-header.json +17 -0
- package/registry/public/r/pending.json +17 -0
- package/registry/public/r/person-option.json +21 -0
- package/registry/public/r/popover.json +20 -0
- package/registry/public/r/progress-bar.json +17 -0
- package/registry/public/r/query-view.json +22 -0
- package/registry/public/r/registry.json +1352 -0
- package/registry/public/r/retry-button.json +21 -0
- package/registry/public/r/rich-text-editor.json +32 -0
- package/registry/public/r/save-bar.json +22 -0
- package/registry/public/r/search-input.json +23 -0
- package/registry/public/r/search-select.json +24 -0
- package/registry/public/r/section-tabs.json +17 -0
- package/registry/public/r/segmented.json +18 -0
- package/registry/public/r/select.json +23 -0
- package/registry/public/r/sheet.json +24 -0
- package/registry/public/r/skeleton.json +17 -0
- package/registry/public/r/spinner.json +20 -0
- package/registry/public/r/stale-notice.json +23 -0
- package/registry/public/r/stat-tile.json +23 -0
- package/registry/public/r/stepper.json +23 -0
- package/registry/public/r/suggestion-strip.json +21 -0
- package/registry/public/r/swatch-picker.json +22 -0
- package/registry/public/r/switch.json +21 -0
- package/registry/public/r/tabs.json +20 -0
- package/registry/public/r/textarea.json +18 -0
- package/registry/public/r/theme.json +15 -0
- package/registry/public/r/timezone-select.json +19 -0
- package/registry/public/r/toaster.json +21 -0
- package/registry/public/r/tooltip.json +17 -0
- package/registry/public/r/ui-labels.json +14 -0
- package/registry/public/r/use-copy-to-clipboard.json +20 -0
- package/registry/public/r/use-online.json +14 -0
- package/registry/public/r/use-stale-queries.json +17 -0
- package/registry/public/r/utils.json +18 -0
- package/registry/public/r/weekday-picker.json +19 -0
- package/registry/public/r/yes-no.json +20 -0
- package/registry/serve.ts +29 -0
- package/rules/architecture.md +47 -0
- package/rules/cloudflare.md +74 -0
- package/rules/code.md +12 -0
- package/rules/git.md +10 -0
- package/rules/index.md +14 -0
- package/rules/observability.md +13 -0
- package/rules/performance.md +39 -0
- package/rules/runtime.md +9 -0
- package/rules/secrets.md +11 -0
- package/rules/stack.md +33 -0
- package/rules/ui.md +23 -0
- package/runtime/ai.test.ts +106 -0
- package/runtime/ai.ts +125 -0
- package/runtime/observability/allowlists.ts +42 -0
- package/runtime/observability/anonymize.test.ts +111 -0
- package/runtime/observability/anonymize.ts +249 -0
- package/runtime/observability/browser.test.ts +46 -0
- package/runtime/observability/browser.ts +126 -0
- package/runtime/observability/server.test.ts +84 -0
- package/runtime/observability/server.ts +152 -0
- package/runtime/observability/tunnel.test.ts +276 -0
- package/runtime/observability/tunnel.ts +360 -0
- package/runtime/observability/user-agent.ts +51 -0
- package/schema/rc.config.schema.json +333 -0
- package/template/.gitattributes +1 -0
- package/template/.github/workflows/ci.yml +64 -0
- package/template/CLAUDE.md +48 -0
- package/template/README.md +50 -0
- package/template/apps/admin/.dev.vars.example +5 -0
- package/template/apps/admin/components.json +24 -0
- package/template/apps/admin/package.json +48 -0
- package/template/apps/admin/public/favicon.svg +1 -0
- package/template/apps/admin/src/components/app-shell.tsx +41 -0
- package/template/apps/admin/src/components/dev-sign-in-links.tsx +57 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-context.ts +13 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-environment-panel.tsx +105 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-list.tsx +54 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-message.tsx +104 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-panel.tsx +130 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-launcher.tsx +45 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-panel.tsx +90 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-types.ts +11 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar.tsx +61 -0
- package/template/apps/admin/src/components/dev-toolbar/development-devtools.tsx +32 -0
- package/template/apps/admin/src/components/login-form.tsx +115 -0
- package/template/apps/admin/src/components/login-sent.tsx +23 -0
- package/template/apps/admin/src/components/message-list.tsx +44 -0
- package/template/apps/admin/src/components/turnstile-challenge.tsx +140 -0
- package/template/apps/admin/src/components/ui/button.tsx +87 -0
- package/template/apps/admin/src/components/ui/callout.tsx +84 -0
- package/template/apps/admin/src/components/ui/card.tsx +48 -0
- package/template/apps/admin/src/components/ui/eyebrow.tsx +33 -0
- package/template/apps/admin/src/components/ui/field-error.tsx +24 -0
- package/template/apps/admin/src/components/ui/field.tsx +100 -0
- package/template/apps/admin/src/components/ui/input.tsx +73 -0
- package/template/apps/admin/src/components/ui/label.tsx +39 -0
- package/template/apps/admin/src/components/ui/skeleton.tsx +225 -0
- package/template/apps/admin/src/components/ui/spinner.tsx +12 -0
- package/template/apps/admin/src/components/ui/toaster.tsx +33 -0
- package/template/apps/admin/src/components/ui/tooltip.tsx +51 -0
- package/template/apps/admin/src/hooks/use-dev-inbox.ts +25 -0
- package/template/apps/admin/src/hooks/use-dev-toolbar-state.ts +48 -0
- package/template/apps/admin/src/lib/api.ts +39 -0
- package/template/apps/admin/src/lib/auth-client.ts +7 -0
- package/template/apps/admin/src/lib/dev-inbox-links.ts +66 -0
- package/template/apps/admin/src/lib/dev-tools-client.ts +54 -0
- package/template/apps/admin/src/lib/dev-tools-contract.ts +52 -0
- package/template/apps/admin/src/lib/messages.ts +5 -0
- package/template/apps/admin/src/lib/query-keys.ts +3 -0
- package/template/apps/admin/src/lib/security-headers.ts +18 -0
- package/template/apps/admin/src/lib/session.ts +27 -0
- package/template/apps/admin/src/lib/site-url.ts +7 -0
- package/template/apps/admin/src/lib/turnstile.ts +70 -0
- package/template/apps/admin/src/lib/ui-labels.tsx +182 -0
- package/template/apps/admin/src/lib/utils.ts +6 -0
- package/template/apps/admin/src/routeTree.gen.ts +109 -0
- package/template/apps/admin/src/router.tsx +29 -0
- package/template/apps/admin/src/routes/__root.tsx +56 -0
- package/template/apps/admin/src/routes/_app/index.tsx +38 -0
- package/template/apps/admin/src/routes/_app.tsx +26 -0
- package/template/apps/admin/src/routes/login.tsx +48 -0
- package/template/apps/admin/src/styles/app.css +11 -0
- package/template/apps/admin/src/worker.ts +88 -0
- package/template/apps/admin/tsconfig.json +19 -0
- package/template/apps/admin/vite.config.ts +69 -0
- package/template/apps/admin/wrangler.jsonc +92 -0
- package/template/apps/web/.env.example +1 -0
- package/template/apps/web/astro.config.ts +46 -0
- package/template/apps/web/package.json +33 -0
- package/template/apps/web/public/favicon.svg +1 -0
- package/template/apps/web/src/components/ContactForm.astro +78 -0
- package/template/apps/web/src/env.d.ts +11 -0
- package/template/apps/web/src/layouts/BaseLayout.astro +32 -0
- package/template/apps/web/src/lib/api.ts +8 -0
- package/template/apps/web/src/pages/api/messages.ts +32 -0
- package/template/apps/web/src/pages/index.astro +26 -0
- package/template/apps/web/src/pages/media/[...key].ts +4 -0
- package/template/apps/web/src/scripts/contact-form.ts +105 -0
- package/template/apps/web/src/scripts/error-reporting.ts +4 -0
- package/template/apps/web/src/scripts/turnstile.ts +177 -0
- package/template/apps/web/src/styles/global.css +11 -0
- package/template/apps/web/src/worker.ts +34 -0
- package/template/apps/web/tsconfig.json +11 -0
- package/template/apps/web/wrangler.jsonc +45 -0
- package/template/bun.lock +1925 -0
- package/template/bunfig.toml +2 -0
- package/template/gitignore +23 -0
- package/template/package.json +74 -0
- package/template/packages/api/components.json +21 -0
- package/template/packages/api/package.json +30 -0
- package/template/packages/api/src/client.ts +13 -0
- package/template/packages/api/src/cloudflare-env.d.ts +26 -0
- package/template/packages/api/src/emails/escape-html.ts +13 -0
- package/template/packages/api/src/emails/layout.ts +22 -0
- package/template/packages/api/src/emails/magic-link.ts +32 -0
- package/template/packages/api/src/emails/new-message.ts +32 -0
- package/template/packages/api/src/emails/send.ts +14 -0
- package/template/packages/api/src/lib/auth-guard.ts +16 -0
- package/template/packages/api/src/lib/auth.ts +108 -0
- package/template/packages/api/src/lib/context.ts +7 -0
- package/template/packages/api/src/lib/dev-inbox/routes.ts +77 -0
- package/template/packages/api/src/lib/dev-inbox/send.ts +57 -0
- package/template/packages/api/src/lib/dev-inbox/store.ts +185 -0
- package/template/packages/api/src/lib/dev-inbox.ts +6 -0
- package/template/packages/api/src/lib/dev-tools-contract.ts +52 -0
- package/template/packages/api/src/lib/environment.ts +17 -0
- package/template/packages/api/src/lib/turnstile.ts +33 -0
- package/template/packages/api/src/media.ts +45 -0
- package/template/packages/api/src/routes/admin-messages.ts +21 -0
- package/template/packages/api/src/routes/dev.ts +31 -0
- package/template/packages/api/src/routes/health.ts +7 -0
- package/template/packages/api/src/routes/messages.ts +58 -0
- package/template/packages/api/src/schemas.ts +18 -0
- package/template/packages/api/src/server.ts +29 -0
- package/template/packages/api/tsconfig.json +10 -0
- package/template/packages/db/migrations/0001_init.sql +79 -0
- package/template/packages/db/package.json +34 -0
- package/template/packages/db/prisma/schema.prisma +95 -0
- package/template/packages/db/prisma.config.ts +9 -0
- package/template/packages/db/scripts/migrate.ts +15 -0
- package/template/packages/db/scripts/new-migration.ts +185 -0
- package/template/packages/db/scripts/reset.ts +54 -0
- package/template/packages/db/scripts/seed.ts +44 -0
- package/template/packages/db/scripts/wrangler.ts +35 -0
- package/template/packages/db/src/d1.ts +7 -0
- package/template/packages/db/src/migration-guard.test.ts +43 -0
- package/template/packages/db/src/migration-guard.ts +51 -0
- package/template/packages/db/src/new-migration.test.ts +16 -0
- package/template/packages/db/src/scope.ts +38 -0
- package/template/packages/db/src/seed.ts +30 -0
- package/template/packages/db/src/sql.ts +42 -0
- package/template/packages/db/tsconfig.json +8 -0
- package/template/packages/shared/components.json +21 -0
- package/template/packages/shared/package.json +33 -0
- package/template/packages/shared/src/edge/origins.ts +20 -0
- package/template/packages/shared/src/edge/turnstile.ts +11 -0
- package/template/packages/shared/src/locales.ts +13 -0
- package/template/packages/shared/src/message-rules.ts +16 -0
- package/template/packages/shared/src/messages.test.ts +26 -0
- package/template/packages/shared/src/messages.ts +30 -0
- package/template/packages/shared/src/observability/build-release.ts +9 -0
- package/template/packages/shared/src/observability/error-reporting.ts +31 -0
- package/template/packages/shared/src/observability/error-tunnel.ts +20 -0
- package/template/packages/shared/src/observability/sentry-browser.ts +1 -0
- package/template/packages/shared/src/observability/server-errors.ts +74 -0
- package/template/packages/shared/src/project.ts +1 -0
- package/template/packages/shared/src/styles/rc-theme.css +138 -0
- package/template/packages/shared/src/styles/theme.css +9 -0
- package/template/packages/shared/tsconfig.json +10 -0
- package/template/rc.config.json +25 -0
- package/template/scripts/bootstrap-local.ts +42 -0
- package/template/tsconfig.base.json +20 -0
- package/template/tsconfig.json +8 -0
- package/template/tsconfig.worker.json +7 -0
- package/template/turbo.json +40 -0
- package/template/vite.config.ts +8 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { MESSAGE_ERRORS } from "@tpl/shared/message-rules"
|
|
2
|
+
import {
|
|
3
|
+
getTurnstileToken,
|
|
4
|
+
prepareTurnstile,
|
|
5
|
+
resetTurnstile,
|
|
6
|
+
TurnstileChallengeError,
|
|
7
|
+
TurnstileUnavailableError,
|
|
8
|
+
} from "@/scripts/turnstile"
|
|
9
|
+
|
|
10
|
+
const TEXTS = {
|
|
11
|
+
sending: "Envoi…",
|
|
12
|
+
sent: "Merci, votre message est bien parti.",
|
|
13
|
+
invalid:
|
|
14
|
+
"Vérifiez les champs : nom, e-mail valide et message d'au moins 10 caractères.",
|
|
15
|
+
rateLimited: "Trop d'envois, réessayez dans une minute.",
|
|
16
|
+
captcha: "La vérification anti-robot a échoué. Réessayez.",
|
|
17
|
+
captchaUnavailable:
|
|
18
|
+
"La vérification anti-robot n'a pas pu se charger (bloqueur de contenu ?).",
|
|
19
|
+
failed: "L'envoi a échoué. Réessayez dans un instant.",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function setStatus(
|
|
23
|
+
form: HTMLFormElement,
|
|
24
|
+
state: "idle" | "sending" | "sent" | "error",
|
|
25
|
+
text = ""
|
|
26
|
+
): void {
|
|
27
|
+
const status = form.querySelector<HTMLElement>("[data-form-status]")
|
|
28
|
+
|
|
29
|
+
form.dataset.state = state
|
|
30
|
+
|
|
31
|
+
if (status) {
|
|
32
|
+
status.textContent = text
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function responseError(response: Response): Promise<string> {
|
|
37
|
+
if (response.status === 429) {
|
|
38
|
+
return TEXTS.rateLimited
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const body = (await response.json().catch(() => null)) as {
|
|
42
|
+
message?: unknown
|
|
43
|
+
} | null
|
|
44
|
+
|
|
45
|
+
if (body?.message === MESSAGE_ERRORS.captcha) {
|
|
46
|
+
return TEXTS.captcha
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return response.status === 400 || response.status === 422
|
|
50
|
+
? TEXTS.invalid
|
|
51
|
+
: TEXTS.failed
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function thrownError(error: unknown): string {
|
|
55
|
+
if (error instanceof TurnstileUnavailableError) {
|
|
56
|
+
return TEXTS.captchaUnavailable
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return error instanceof TurnstileChallengeError ? TEXTS.captcha : TEXTS.failed
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function submit(form: HTMLFormElement): Promise<void> {
|
|
63
|
+
const button = form.querySelector<HTMLButtonElement>("button[type=submit]")
|
|
64
|
+
|
|
65
|
+
if (!form.checkValidity()) {
|
|
66
|
+
setStatus(form, "error", TEXTS.invalid)
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
button?.setAttribute("disabled", "")
|
|
71
|
+
setStatus(form, "sending", TEXTS.sending)
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const turnstileToken = await getTurnstileToken(form)
|
|
75
|
+
const fields = Object.fromEntries(new FormData(form))
|
|
76
|
+
|
|
77
|
+
const response = await fetch("/api/messages", {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: { "content-type": "application/json" },
|
|
80
|
+
body: JSON.stringify({ ...fields, turnstileToken }),
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
if (response.ok) {
|
|
84
|
+
form.reset()
|
|
85
|
+
setStatus(form, "sent", TEXTS.sent)
|
|
86
|
+
} else {
|
|
87
|
+
setStatus(form, "error", await responseError(response))
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
setStatus(form, "error", thrownError(error))
|
|
91
|
+
} finally {
|
|
92
|
+
button?.removeAttribute("disabled")
|
|
93
|
+
resetTurnstile(form)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const form of document.querySelectorAll<HTMLFormElement>(
|
|
98
|
+
"form[data-contact-form]"
|
|
99
|
+
)) {
|
|
100
|
+
prepareTurnstile(form)
|
|
101
|
+
form.addEventListener("submit", (event) => {
|
|
102
|
+
event.preventDefault()
|
|
103
|
+
void submit(form)
|
|
104
|
+
})
|
|
105
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
const TURNSTILE_SCRIPT_URL =
|
|
2
|
+
"https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit"
|
|
3
|
+
|
|
4
|
+
interface TTurnstileOptions {
|
|
5
|
+
sitekey: string
|
|
6
|
+
action?: string
|
|
7
|
+
execution?: "render" | "execute"
|
|
8
|
+
appearance?: "always" | "execute" | "interaction-only"
|
|
9
|
+
"response-field"?: boolean
|
|
10
|
+
"refresh-expired"?: "auto" | "manual" | "never"
|
|
11
|
+
callback?: (token: string) => void
|
|
12
|
+
"error-callback"?: (code: string) => boolean
|
|
13
|
+
"expired-callback"?: () => void
|
|
14
|
+
"timeout-callback"?: () => void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface TTurnstileApi {
|
|
18
|
+
render: (
|
|
19
|
+
container: HTMLElement,
|
|
20
|
+
options: TTurnstileOptions
|
|
21
|
+
) => string | undefined
|
|
22
|
+
execute: (widgetId: string) => void
|
|
23
|
+
reset: (widgetId: string) => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
declare global {
|
|
27
|
+
interface Window {
|
|
28
|
+
turnstile?: TTurnstileApi
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface TPendingToken {
|
|
33
|
+
resolve: (token: string) => void
|
|
34
|
+
reject: (error: Error) => void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface TWidget {
|
|
38
|
+
id: string
|
|
39
|
+
pending?: TPendingToken
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class TurnstileUnavailableError extends Error {}
|
|
43
|
+
|
|
44
|
+
export class TurnstileChallengeError extends Error {}
|
|
45
|
+
|
|
46
|
+
const widgets = new WeakMap<HTMLFormElement, Promise<TWidget>>()
|
|
47
|
+
|
|
48
|
+
let scriptPromise: Promise<TTurnstileApi> | undefined
|
|
49
|
+
|
|
50
|
+
function loadTurnstile(): Promise<TTurnstileApi> {
|
|
51
|
+
if (window.turnstile) {
|
|
52
|
+
return Promise.resolve(window.turnstile)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
scriptPromise ??= new Promise<TTurnstileApi>((resolve, reject) => {
|
|
56
|
+
const script = document.createElement("script")
|
|
57
|
+
|
|
58
|
+
script.src = TURNSTILE_SCRIPT_URL
|
|
59
|
+
script.async = true
|
|
60
|
+
script.addEventListener("load", () => {
|
|
61
|
+
if (window.turnstile) {
|
|
62
|
+
resolve(window.turnstile)
|
|
63
|
+
} else {
|
|
64
|
+
reject(new TurnstileUnavailableError("turnstile missing"))
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
script.addEventListener("error", () => {
|
|
68
|
+
scriptPromise = undefined
|
|
69
|
+
script.remove()
|
|
70
|
+
reject(new TurnstileUnavailableError("turnstile blocked"))
|
|
71
|
+
})
|
|
72
|
+
document.head.appendChild(script)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
return scriptPromise
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function settle(
|
|
79
|
+
widget: TWidget,
|
|
80
|
+
outcome: { token: string } | { error: Error }
|
|
81
|
+
): void {
|
|
82
|
+
const pending = widget.pending
|
|
83
|
+
|
|
84
|
+
widget.pending = undefined
|
|
85
|
+
|
|
86
|
+
if (!pending) {
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if ("token" in outcome) {
|
|
91
|
+
pending.resolve(outcome.token)
|
|
92
|
+
} else {
|
|
93
|
+
pending.reject(outcome.error)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function renderWidget(form: HTMLFormElement): Promise<TWidget> {
|
|
98
|
+
const api = await loadTurnstile()
|
|
99
|
+
const slot = form.querySelector<HTMLElement>("[data-turnstile-slot]")
|
|
100
|
+
const sitekey = form.dataset.turnstileSiteKey
|
|
101
|
+
|
|
102
|
+
if (!slot || !sitekey) {
|
|
103
|
+
throw new TurnstileUnavailableError("turnstile not configured")
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const widget: TWidget = { id: "" }
|
|
107
|
+
const id = api.render(slot, {
|
|
108
|
+
sitekey,
|
|
109
|
+
action: "contact",
|
|
110
|
+
execution: "execute",
|
|
111
|
+
appearance: "interaction-only",
|
|
112
|
+
"response-field": false,
|
|
113
|
+
"refresh-expired": "manual",
|
|
114
|
+
callback: (token) => settle(widget, { token }),
|
|
115
|
+
"error-callback": (code) => {
|
|
116
|
+
settle(widget, { error: new TurnstileChallengeError(code) })
|
|
117
|
+
return true
|
|
118
|
+
},
|
|
119
|
+
"expired-callback": () =>
|
|
120
|
+
settle(widget, { error: new TurnstileChallengeError("expired") }),
|
|
121
|
+
"timeout-callback": () =>
|
|
122
|
+
settle(widget, { error: new TurnstileChallengeError("timeout") }),
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
if (!id) {
|
|
126
|
+
throw new TurnstileUnavailableError("turnstile render failed")
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
widget.id = id
|
|
130
|
+
|
|
131
|
+
return widget
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function ensureWidget(form: HTMLFormElement): Promise<TWidget> {
|
|
135
|
+
const existing = widgets.get(form)
|
|
136
|
+
|
|
137
|
+
if (existing) {
|
|
138
|
+
return existing
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const widget = renderWidget(form)
|
|
142
|
+
|
|
143
|
+
widgets.set(form, widget)
|
|
144
|
+
widget.catch(() => widgets.delete(form))
|
|
145
|
+
|
|
146
|
+
return widget
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Invisible: the challenge runs on submit and only shows up if Cloudflare asks for an interaction. */
|
|
150
|
+
export async function getTurnstileToken(
|
|
151
|
+
form: HTMLFormElement
|
|
152
|
+
): Promise<string> {
|
|
153
|
+
const widget = await ensureWidget(form)
|
|
154
|
+
const api = await loadTurnstile()
|
|
155
|
+
|
|
156
|
+
settle(widget, { error: new TurnstileChallengeError("superseded") })
|
|
157
|
+
|
|
158
|
+
return new Promise<string>((resolve, reject) => {
|
|
159
|
+
widget.pending = { resolve, reject }
|
|
160
|
+
api.execute(widget.id)
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function resetTurnstile(form: HTMLFormElement): void {
|
|
165
|
+
void widgets
|
|
166
|
+
.get(form)
|
|
167
|
+
?.then((widget) => window.turnstile?.reset(widget.id))
|
|
168
|
+
.catch(() => undefined)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function prepareTurnstile(form: HTMLFormElement): void {
|
|
172
|
+
form.addEventListener(
|
|
173
|
+
"focusin",
|
|
174
|
+
() => void ensureWidget(form).catch(() => undefined),
|
|
175
|
+
{ once: true }
|
|
176
|
+
)
|
|
177
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import astro from "@astrojs/cloudflare/entrypoints/server"
|
|
2
|
+
import {
|
|
3
|
+
ERROR_TUNNEL_PATH,
|
|
4
|
+
serveErrorTunnel,
|
|
5
|
+
} from "@tpl/shared/observability/error-tunnel"
|
|
6
|
+
import {
|
|
7
|
+
reportServerError,
|
|
8
|
+
withErrorTracking,
|
|
9
|
+
} from "@tpl/shared/observability/server-errors"
|
|
10
|
+
|
|
11
|
+
function renderFailure(request: Request, error: unknown): Response {
|
|
12
|
+
reportServerError(error, {
|
|
13
|
+
method: request.method,
|
|
14
|
+
route: new URL(request.url).pathname,
|
|
15
|
+
surface: "render",
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
return new Response("Internal Server Error", {
|
|
19
|
+
headers: { "cache-control": "no-store" },
|
|
20
|
+
status: 500,
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default withErrorTracking("tpl-web", {
|
|
25
|
+
async fetch(request, env, ctx) {
|
|
26
|
+
if (new URL(request.url).pathname === ERROR_TUNNEL_PATH) {
|
|
27
|
+
return await serveErrorTunnel(request, env, ctx)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return await astro
|
|
31
|
+
.fetch(request, env, ctx)
|
|
32
|
+
.catch((error: unknown) => renderFailure(request, error))
|
|
33
|
+
},
|
|
34
|
+
} satisfies ExportedHandler<Cloudflare.Env>)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../../node_modules/wrangler/config-schema.json",
|
|
3
|
+
"name": "tpl-web-local",
|
|
4
|
+
"account_id": "00000000000000000000000000000000",
|
|
5
|
+
"main": "src/worker.ts",
|
|
6
|
+
"compatibility_date": "2026-10-01",
|
|
7
|
+
"compatibility_flags": ["nodejs_compat"],
|
|
8
|
+
"workers_dev": false,
|
|
9
|
+
"preview_urls": false,
|
|
10
|
+
"observability": {
|
|
11
|
+
"enabled": true,
|
|
12
|
+
"traces": { "enabled": true, "head_sampling_rate": 1 },
|
|
13
|
+
},
|
|
14
|
+
// The top level is local development; production is env.production, which inherits no binding and no var.
|
|
15
|
+
// The site reads everything through the admin Worker, which alone holds D1, R2 and the secrets.
|
|
16
|
+
"services": [{ "binding": "API", "service": "tpl-admin-local" }],
|
|
17
|
+
"vars": {
|
|
18
|
+
"ENVIRONMENT": "local",
|
|
19
|
+
"SENTRY_DSN": "",
|
|
20
|
+
"SENTRY_TUNNEL_DSNS": "",
|
|
21
|
+
"PUBLIC_SENTRY_DSN": "",
|
|
22
|
+
},
|
|
23
|
+
"env": {
|
|
24
|
+
"production": {
|
|
25
|
+
"observability": {
|
|
26
|
+
"enabled": true,
|
|
27
|
+
"traces": { "enabled": true, "head_sampling_rate": 1 },
|
|
28
|
+
},
|
|
29
|
+
"name": "tpl-web-prod",
|
|
30
|
+
"upload_source_maps": true,
|
|
31
|
+
"routes": [
|
|
32
|
+
{ "pattern": "example.com", "custom_domain": true },
|
|
33
|
+
{ "pattern": "www.example.com", "custom_domain": true },
|
|
34
|
+
],
|
|
35
|
+
"services": [{ "binding": "API", "service": "tpl-admin-prod" }],
|
|
36
|
+
"vars": {
|
|
37
|
+
"ENVIRONMENT": "production",
|
|
38
|
+
// The DSN of the Sentry project tpl-web, the same in all three.
|
|
39
|
+
"SENTRY_DSN": "",
|
|
40
|
+
"SENTRY_TUNNEL_DSNS": "",
|
|
41
|
+
"PUBLIC_SENTRY_DSN": "",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
}
|