@ingram-tech/bot-protection 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -59,6 +59,51 @@ export async function POST(request: Request) {
59
59
  `verifyHuman` also accepts a plain object (`{ formData: { ...fields } }`) and a
60
60
  `timing` window (`{ minMs, maxMs }`). Pass `botid: false` to skip layer 3.
61
61
 
62
+ ### Client forms (JSON POST)
63
+
64
+ For client components that POST JSON instead of a server-rendered `<form>`, use
65
+ the `/react` hook. It fetches the token from your route's `GET` on mount and
66
+ hands you the fields to merge into the body. The route's `GET` returns the
67
+ token; its `POST` verifies:
68
+
69
+ ```tsx
70
+ "use client";
71
+ import { HoneypotInput, useBotProtection } from "@ingram-tech/bot-protection/react";
72
+
73
+ export function ContactForm() {
74
+ const { honeypotRef, botFields } = useBotProtection("/api/contact");
75
+
76
+ async function onSubmit(values: FormValues) {
77
+ await fetch("/api/contact", {
78
+ method: "POST",
79
+ headers: { "content-type": "application/json" },
80
+ body: JSON.stringify({ ...values, ...botFields() }),
81
+ });
82
+ }
83
+ return (
84
+ <form onSubmit={/* ... */}>
85
+ {/* ...your real fields... */}
86
+ <HoneypotInput inputRef={honeypotRef} />
87
+ </form>
88
+ );
89
+ }
90
+ ```
91
+
92
+ ```ts
93
+ // app/api/contact/route.ts
94
+ import { createFormToken, verifyHuman } from "@ingram-tech/bot-protection";
95
+
96
+ export const GET = () => Response.json({ token: createFormToken() });
97
+
98
+ export async function POST(request: Request) {
99
+ const body = await request.json();
100
+ const result = await verifyHuman({ formData: body });
101
+ if (!result.ok) return Response.json({ ok: true }); // silently drop
102
+ // ...send the email / save the lead...
103
+ return Response.json({ ok: true });
104
+ }
105
+ ```
106
+
62
107
  The honeypot field defaults to a name browsers and password managers won't
63
108
  autofill (filling it would falsely flag real users). If that default collides
64
109
  with a real field in your form, override it on both sides — they must match:
@@ -0,0 +1,31 @@
1
+ import { type RefObject } from "react";
2
+ /**
3
+ * Client-side bot protection for forms that POST JSON to your own route.
4
+ *
5
+ * Fetches a signed timing token from `tokenEndpoint` on mount — the route's GET
6
+ * handler should return `{ token: createFormToken() }` — then exposes a honeypot
7
+ * ref plus a `botFields()` helper returning the fields to spread into the
8
+ * request body. The matching POST handler calls `verifyHuman({ formData: body })`.
9
+ *
10
+ * Pair with {@link HoneypotInput}, which renders the trap and wires the ref.
11
+ *
12
+ * @param tokenEndpoint - GET route that mints the token (e.g. "/api/contact").
13
+ *
14
+ * @example
15
+ * const { honeypotRef, botFields } = useBotProtection("/api/contact");
16
+ * // ...in the form: <HoneypotInput inputRef={honeypotRef} />
17
+ * // ...on submit: body: JSON.stringify({ ...values, ...botFields() })
18
+ */
19
+ export declare function useBotProtection(tokenEndpoint: string): {
20
+ honeypotRef: RefObject<HTMLInputElement | null>;
21
+ botFields: () => Record<string, string>;
22
+ };
23
+ /**
24
+ * Visually-hidden honeypot input for {@link useBotProtection}. Real users never
25
+ * see or fill it (off-screen, aria-hidden, tabIndex -1, autofill opted out);
26
+ * bots that fill every field give themselves away.
27
+ */
28
+ export declare function HoneypotInput({ inputRef, }: {
29
+ inputRef: RefObject<HTMLInputElement | null>;
30
+ }): import("react/jsx-runtime").JSX.Element;
31
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,SAAS,EAA+B,MAAM,OAAO,CAAC;AAGpE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,aAAa,EAAE,MAAM;;qBAW/B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;EAM5C;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,EAC7B,QAAQ,GACR,EAAE;IACF,QAAQ,EAAE,SAAS,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC;CAC7C,2CAuBA"}
package/dist/react.js ADDED
@@ -0,0 +1,51 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useEffect, useRef, useState } from "react";
4
+ import { HONEYPOT_FIELD, TOKEN_FIELD } from "./fields";
5
+ /**
6
+ * Client-side bot protection for forms that POST JSON to your own route.
7
+ *
8
+ * Fetches a signed timing token from `tokenEndpoint` on mount — the route's GET
9
+ * handler should return `{ token: createFormToken() }` — then exposes a honeypot
10
+ * ref plus a `botFields()` helper returning the fields to spread into the
11
+ * request body. The matching POST handler calls `verifyHuman({ formData: body })`.
12
+ *
13
+ * Pair with {@link HoneypotInput}, which renders the trap and wires the ref.
14
+ *
15
+ * @param tokenEndpoint - GET route that mints the token (e.g. "/api/contact").
16
+ *
17
+ * @example
18
+ * const { honeypotRef, botFields } = useBotProtection("/api/contact");
19
+ * // ...in the form: <HoneypotInput inputRef={honeypotRef} />
20
+ * // ...on submit: body: JSON.stringify({ ...values, ...botFields() })
21
+ */
22
+ export function useBotProtection(tokenEndpoint) {
23
+ const [token, setToken] = useState("");
24
+ const honeypotRef = useRef(null);
25
+ useEffect(() => {
26
+ fetch(tokenEndpoint)
27
+ .then((res) => res.json())
28
+ .then((data) => setToken(data.token ?? ""))
29
+ .catch(() => { });
30
+ }, [tokenEndpoint]);
31
+ const botFields = () => ({
32
+ [HONEYPOT_FIELD]: honeypotRef.current?.value ?? "",
33
+ [TOKEN_FIELD]: token,
34
+ });
35
+ return { honeypotRef, botFields };
36
+ }
37
+ /**
38
+ * Visually-hidden honeypot input for {@link useBotProtection}. Real users never
39
+ * see or fill it (off-screen, aria-hidden, tabIndex -1, autofill opted out);
40
+ * bots that fill every field give themselves away.
41
+ */
42
+ export function HoneypotInput({ inputRef, }) {
43
+ return (_jsx("input", { ref: inputRef, type: "text", name: HONEYPOT_FIELD, tabIndex: -1, autoComplete: "off", "aria-hidden": "true", defaultValue: "", "data-1p-ignore": true, "data-lpignore": "true", "data-form-type": "other", style: {
44
+ position: "absolute",
45
+ left: "-9999px",
46
+ width: "1px",
47
+ height: "1px",
48
+ overflow: "hidden",
49
+ } }));
50
+ }
51
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAkB,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,gBAAgB,CAAC,aAAqB;IACrD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IACvC,MAAM,WAAW,GAAG,MAAM,CAAmB,IAAI,CAAC,CAAC;IAEnD,SAAS,CAAC,GAAG,EAAE;QACd,KAAK,CAAC,aAAa,CAAC;aAClB,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;aACzB,IAAI,CAAC,CAAC,IAAwB,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;aAC9D,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACnB,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC;IAEpB,MAAM,SAAS,GAAG,GAA2B,EAAE,CAAC,CAAC;QAChD,CAAC,cAAc,CAAC,EAAE,WAAW,CAAC,OAAO,EAAE,KAAK,IAAI,EAAE;QAClD,CAAC,WAAW,CAAC,EAAE,KAAK;KACpB,CAAC,CAAC;IAEH,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,EAC7B,QAAQ,GAGR;IACA,OAAO,CACN,gBACC,GAAG,EAAE,QAAQ,EACb,IAAI,EAAC,MAAM,EACX,IAAI,EAAE,cAAc,EACpB,QAAQ,EAAE,CAAC,CAAC,EACZ,YAAY,EAAC,KAAK,iBACN,MAAM,EAClB,YAAY,EAAC,EAAE,2CAGD,MAAM,oBACL,OAAO,EACtB,KAAK,EAAE;YACN,QAAQ,EAAE,UAAU;YACpB,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,KAAK;YACZ,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,QAAQ;SAClB,GACA,CACF,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ingram-tech/bot-protection",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Invisible, layered bot protection for forms — honeypot + signed timing token + Vercel BotID.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,6 +24,10 @@
24
24
  "types": "./dist/honeypot.d.ts",
25
25
  "import": "./dist/honeypot.js"
26
26
  },
27
+ "./react": {
28
+ "types": "./dist/react.d.ts",
29
+ "import": "./dist/react.js"
30
+ },
27
31
  "./fields": {
28
32
  "types": "./dist/fields.d.ts",
29
33
  "import": "./dist/fields.js"