@webdecoy/fcaptcha-react 0.0.0-stage → 1.49.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.
Files changed (4) hide show
  1. package/README.md +28 -2
  2. package/index.d.ts +17 -0
  3. package/index.js +108 -0
  4. package/package.json +37 -4
package/README.md CHANGED
@@ -1,3 +1,29 @@
1
- # Temporary Holding Version
1
+ # `@webdecoy/fcaptcha-react`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The official React 18/19 component for FCaptcha's interactive widget.
4
+
5
+ ```jsx
6
+ import { FCaptcha } from '@webdecoy/fcaptcha-react';
7
+
8
+ export function ContactProtection() {
9
+ return (
10
+ <FCaptcha
11
+ siteKey="contact-form"
12
+ serverUrl="https://captcha.example.com"
13
+ scriptSrc="https://captcha.example.com/fcaptcha.js"
14
+ onVerify={(token) => submitToYourBackend(token)}
15
+ onError={(error) => console.error(error)}
16
+ />
17
+ );
18
+ }
19
+ ```
20
+
21
+ The token is not a verdict by itself. Send it to your backend and verify it with
22
+ FCaptcha's `/siteverify` endpoint before performing the protected action.
23
+
24
+ `serverUrl` configures a process-wide browser global. Every component on a page
25
+ must use the same value; the wrapper reports conflicting values through
26
+ `onError` rather than silently redirecting another widget's requests.
27
+
28
+ The package loads `scriptSrc` once per resolved URL, is safe to import during
29
+ server-side rendering, and cleans up its widget when React unmounts it.
package/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { CSSProperties, ReactElement } from 'react';
2
+
3
+ export interface FCaptchaProps {
4
+ siteKey: string;
5
+ onVerify?: (token: string) => void;
6
+ onExpire?: () => void;
7
+ onError?: (error: unknown) => void;
8
+ scriptSrc?: string;
9
+ serverUrl?: string;
10
+ theme?: 'light' | 'dark';
11
+ size?: 'normal';
12
+ lang?: string;
13
+ className?: string;
14
+ style?: CSSProperties;
15
+ }
16
+
17
+ export function FCaptcha(props: FCaptchaProps): ReactElement;
package/index.js ADDED
@@ -0,0 +1,108 @@
1
+ import { createElement, useEffect, useRef } from 'react';
2
+
3
+ const scriptLoads = new Map();
4
+
5
+ function resolvedUrl(value) {
6
+ return new URL(value, document.baseURI).href;
7
+ }
8
+
9
+ function loadScript(source) {
10
+ if (window.FCaptcha) return Promise.resolve(window.FCaptcha);
11
+
12
+ const url = resolvedUrl(source);
13
+ const pending = scriptLoads.get(url);
14
+ if (pending) return pending;
15
+
16
+ const script = document.createElement('script');
17
+ script.src = url;
18
+ script.async = true;
19
+
20
+ const load = new Promise((resolve, reject) => {
21
+ const fail = (message) => {
22
+ scriptLoads.delete(url);
23
+ script.remove();
24
+ reject(new Error(message));
25
+ };
26
+
27
+ script.addEventListener('load', () => {
28
+ if (window.FCaptcha) resolve(window.FCaptcha);
29
+ else fail('FCaptcha script loaded without exposing window.FCaptcha');
30
+ }, { once: true });
31
+ script.addEventListener('error', () => fail(`Failed to load FCaptcha from ${url}`), { once: true });
32
+ });
33
+
34
+ scriptLoads.set(url, load);
35
+ document.head.appendChild(script);
36
+ return load;
37
+ }
38
+
39
+ function normalizedServerUrl(value) {
40
+ if (!value) return '';
41
+ return resolvedUrl(value).replace(/\/+$/, '');
42
+ }
43
+
44
+ function configureServer(api, serverUrl) {
45
+ const requested = normalizedServerUrl(serverUrl);
46
+ if (!requested) return;
47
+
48
+ const current = normalizedServerUrl(api.serverUrl);
49
+ if (current && current !== requested) {
50
+ throw new Error(`FCaptcha is already configured for ${current}; refusing conflicting serverUrl ${requested}`);
51
+ }
52
+
53
+ if (!current) {
54
+ if (typeof api.configure !== 'function') {
55
+ throw new Error('Loaded FCaptcha API does not support configure({ serverUrl })');
56
+ }
57
+ api.configure({ serverUrl: requested });
58
+ }
59
+ }
60
+
61
+ export function FCaptcha({
62
+ siteKey,
63
+ onVerify,
64
+ onExpire,
65
+ onError,
66
+ scriptSrc = '/fcaptcha.js',
67
+ serverUrl,
68
+ theme,
69
+ size,
70
+ lang,
71
+ className,
72
+ style,
73
+ }) {
74
+ const containerRef = useRef(null);
75
+ const callbacks = useRef({ onVerify, onExpire, onError });
76
+ callbacks.current = { onVerify, onExpire, onError };
77
+
78
+ useEffect(() => {
79
+ let active = true;
80
+ let api;
81
+ let widgetId;
82
+
83
+ loadScript(scriptSrc).then((loadedApi) => {
84
+ if (!active || !containerRef.current) return;
85
+
86
+ api = loadedApi;
87
+ configureServer(api, serverUrl);
88
+ widgetId = api.render(containerRef.current, {
89
+ siteKey,
90
+ ...(theme === undefined ? {} : { theme }),
91
+ ...(size === undefined ? {} : { size }),
92
+ ...(lang === undefined ? {} : { lang }),
93
+ callback: (token) => callbacks.current.onVerify?.(token),
94
+ expiredCallback: () => callbacks.current.onExpire?.(),
95
+ errorCallback: (error) => callbacks.current.onError?.(error),
96
+ });
97
+ }).catch((error) => {
98
+ if (active) callbacks.current.onError?.(error);
99
+ });
100
+
101
+ return () => {
102
+ active = false;
103
+ if (api && widgetId !== undefined) api.destroy(widgetId);
104
+ };
105
+ }, [siteKey, scriptSrc, serverUrl, theme, size, lang]);
106
+
107
+ return createElement('div', { ref: containerRef, className, style });
108
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@webdecoy/fcaptcha-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.49.0",
4
+ "description": "Official React component for the FCaptcha browser widget",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "import": "./index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "index.d.ts",
17
+ "README.md"
18
+ ],
19
+ "sideEffects": false,
20
+ "peerDependencies": {
21
+ "react": "^18.0.0 || ^19.0.0"
22
+ },
23
+ "devDependencies": {
24
+ "@testing-library/react": "^16.3.0",
25
+ "jsdom": "^27.2.0",
26
+ "react": "^19.2.0",
27
+ "react-dom": "^19.2.0",
28
+ "vitest": "^4.0.0"
29
+ },
30
+ "scripts": {
31
+ "test": "vitest run --environment jsdom"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/WebDecoy/FCaptcha",
36
+ "directory": "packages/react"
37
+ },
38
+ "license": "MIT"
39
+ }