van-turnstile
v0.1.0
Published
Cloudflare Turnstile wrapper for VanJS
Readme
VanTurnstile
Turnstile wrapper for the VanJS ecosystem with a handle-first API:
- Browser-first TypeScript package
- Cloudflare script loading with deduplication
- Reactive token state via
van.state - Imperative widget controls:
reset,execute,getResponse,remove,dispose
Install
bun add van-turnstile vanjs-coreOr with npm:
npm install van-turnstile vanjs-coreUsage
import van from "vanjs-core";
import { Turnstile } from "van-turnstile";
const captcha = Turnstile({
sitekey: "your-site-key",
onToken: (token) => {
console.log("token:", token);
},
onTokenExpired: () => {
console.log("expired");
},
onError: (code) => {
console.error("turnstile error:", code);
}
});
const target = document.querySelector("#app");
if (target) {
van.add(target, captcha.el);
}API
Turnstile(props: TurnstileProps): TurnstileHandle
TurnstileProps:
sitekey: string(required)onToken?: (token: string) => voidonTokenExpired?: () => voidonError?: (errorCode?: string) => voidautoRefresh?: boolean(defaulttrue)theme?: "auto" | "light" | "dark"(defaultauto)size?: "normal" | "compact" | "flexible"(defaultnormal)appearance?: "always" | "execute" | "interaction-only"execution?: "render" | "execute"(defaultrender)action?: stringcData?: stringclass?: string
TurnstileHandle:
el: HTMLDivElementtoken: State<string | null>reset(): voidremove(): voidexecute(): voidgetResponse(): string | nulldispose(): void
Demo
This repository includes a Vite demo that showcases:
- widget configuration (
theme,size,appearance,execution,action,cData) - token and event state updates
- imperative methods and remount flow
Run it locally:
bun install
bun run demoBuild the demo:
bun run demo:buildDevelopment
bun run test
bun run build
bun run lint
bun run checkNotes
- Browser-only package in v1.
- Turnstile token verification must happen on your server in production.
- Backend verification helpers are intentionally out of scope for this package.
