@captchafox/react 1.5.0 → 1.6.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/dist/index.cjs CHANGED
@@ -57,31 +57,51 @@ __export(src_exports, {
57
57
  module.exports = __toCommonJS(src_exports);
58
58
 
59
59
  // ../internal/dist/index.mjs
60
- var resolveFn;
61
- var rejectFn;
62
- var mountInstance = new Promise((resolve, reject) => {
63
- resolveFn = resolve;
64
- rejectFn = reject;
60
+ var backoff = (retryCount) => Math.exp(retryCount) * 150;
61
+ var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback, { attempts = 4 } = {}) {
62
+ let error;
63
+ for (let i = 0; i < attempts; i++) {
64
+ try {
65
+ return yield callback();
66
+ } catch (err) {
67
+ error = err;
68
+ yield new Promise((r) => setTimeout(r, backoff(i)));
69
+ }
70
+ }
71
+ throw error != null ? error : new Error("Exhausted all retries");
65
72
  });
73
+ var mountInstance;
66
74
  var LOAD_FUNC_KEY = "captchaFoxOnLoad";
67
75
  var SCRIPT_SRC = `https://cdn.captchafox.com/api.js?render=explicit&onload=${LOAD_FUNC_KEY}`;
68
- function loadCaptchaScript() {
76
+ function loadScript() {
69
77
  return __async(this, arguments, function* ({ nonce } = {}) {
70
- if (document.querySelector(`script[src="${SCRIPT_SRC}"]`))
78
+ if (mountInstance && document.querySelector(`script[src="${SCRIPT_SRC}"]`)) {
71
79
  return mountInstance;
72
- window[LOAD_FUNC_KEY] = resolveFn;
73
- const script = document.createElement("script");
74
- script.src = SCRIPT_SRC;
75
- script.async = true;
76
- script.defer = true;
77
- script.onerror = rejectFn;
78
- if (nonce) {
79
- script.nonce = nonce;
80
80
  }
81
- document.body.appendChild(script);
81
+ mountInstance = new Promise((resolve, reject) => {
82
+ window[LOAD_FUNC_KEY] = resolve;
83
+ const script = document.createElement("script");
84
+ script.src = SCRIPT_SRC;
85
+ script.async = true;
86
+ script.defer = true;
87
+ script.onerror = (e) => {
88
+ script.remove();
89
+ mountInstance = void 0;
90
+ reject(e);
91
+ };
92
+ if (nonce) {
93
+ script.nonce = nonce;
94
+ }
95
+ document.body.appendChild(script);
96
+ });
82
97
  return mountInstance;
83
98
  });
84
99
  }
100
+ function loadCaptchaScript() {
101
+ return __async(this, arguments, function* (props = {}) {
102
+ return withRetry(() => loadScript(props));
103
+ });
104
+ }
85
105
  var isApiReady = () => typeof (window == null ? void 0 : window.captchafox) !== "undefined";
86
106
 
87
107
  // src/CaptchaFox.tsx
@@ -127,7 +147,7 @@ var CaptchaFox = (0, import_react.forwardRef)(
127
147
  console.warn("[CaptchaFox] Widget has not been loaded");
128
148
  return;
129
149
  }
130
- setWidgetId("");
150
+ setWidgetId(void 0);
131
151
  window.captchafox.remove(widgetId);
132
152
  },
133
153
  execute: () => {
@@ -140,11 +160,15 @@ var CaptchaFox = (0, import_react.forwardRef)(
140
160
  },
141
161
  [widgetId]
142
162
  );
163
+ (0, import_react.useEffect)(() => {
164
+ if (widgetId) {
165
+ onLoad == null ? void 0 : onLoad();
166
+ }
167
+ }, [widgetId]);
143
168
  const renderCaptcha = () => __async(void 0, null, function* () {
144
169
  var _a, _b, _c;
145
170
  (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
146
- if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1)
147
- return;
171
+ if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return;
148
172
  const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, {
149
173
  lang,
150
174
  sitekey,
@@ -157,11 +181,9 @@ var CaptchaFox = (0, import_react.forwardRef)(
157
181
  onVerify
158
182
  });
159
183
  setWidgetId(newWidgetId);
160
- onLoad == null ? void 0 : onLoad();
161
184
  });
162
185
  (0, import_react.useEffect)(() => {
163
- if (!containerRef)
164
- return;
186
+ if (!containerRef) return;
165
187
  if (firstRendered.current) {
166
188
  if (isApiReady()) {
167
189
  renderCaptcha();
package/dist/index.d.cts CHANGED
@@ -11,4 +11,4 @@ declare const CaptchaFox: React.ForwardRefExoticComponent<WidgetOptions & {
11
11
 
12
12
  declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
13
13
 
14
- export { CAPTCHA_RESPONSE_KEY, CaptchaFox, CaptchaFoxInstance };
14
+ export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance };
package/dist/index.d.ts CHANGED
@@ -11,4 +11,4 @@ declare const CaptchaFox: React.ForwardRefExoticComponent<WidgetOptions & {
11
11
 
12
12
  declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
13
13
 
14
- export { CAPTCHA_RESPONSE_KEY, CaptchaFox, CaptchaFoxInstance };
14
+ export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance };
package/dist/index.js CHANGED
@@ -21,31 +21,51 @@ var __async = (__this, __arguments, generator) => {
21
21
  };
22
22
 
23
23
  // ../internal/dist/index.mjs
24
- var resolveFn;
25
- var rejectFn;
26
- var mountInstance = new Promise((resolve, reject) => {
27
- resolveFn = resolve;
28
- rejectFn = reject;
24
+ var backoff = (retryCount) => Math.exp(retryCount) * 150;
25
+ var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback, { attempts = 4 } = {}) {
26
+ let error;
27
+ for (let i = 0; i < attempts; i++) {
28
+ try {
29
+ return yield callback();
30
+ } catch (err) {
31
+ error = err;
32
+ yield new Promise((r) => setTimeout(r, backoff(i)));
33
+ }
34
+ }
35
+ throw error != null ? error : new Error("Exhausted all retries");
29
36
  });
37
+ var mountInstance;
30
38
  var LOAD_FUNC_KEY = "captchaFoxOnLoad";
31
39
  var SCRIPT_SRC = `https://cdn.captchafox.com/api.js?render=explicit&onload=${LOAD_FUNC_KEY}`;
32
- function loadCaptchaScript() {
40
+ function loadScript() {
33
41
  return __async(this, arguments, function* ({ nonce } = {}) {
34
- if (document.querySelector(`script[src="${SCRIPT_SRC}"]`))
42
+ if (mountInstance && document.querySelector(`script[src="${SCRIPT_SRC}"]`)) {
35
43
  return mountInstance;
36
- window[LOAD_FUNC_KEY] = resolveFn;
37
- const script = document.createElement("script");
38
- script.src = SCRIPT_SRC;
39
- script.async = true;
40
- script.defer = true;
41
- script.onerror = rejectFn;
42
- if (nonce) {
43
- script.nonce = nonce;
44
44
  }
45
- document.body.appendChild(script);
45
+ mountInstance = new Promise((resolve, reject) => {
46
+ window[LOAD_FUNC_KEY] = resolve;
47
+ const script = document.createElement("script");
48
+ script.src = SCRIPT_SRC;
49
+ script.async = true;
50
+ script.defer = true;
51
+ script.onerror = (e) => {
52
+ script.remove();
53
+ mountInstance = void 0;
54
+ reject(e);
55
+ };
56
+ if (nonce) {
57
+ script.nonce = nonce;
58
+ }
59
+ document.body.appendChild(script);
60
+ });
46
61
  return mountInstance;
47
62
  });
48
63
  }
64
+ function loadCaptchaScript() {
65
+ return __async(this, arguments, function* (props = {}) {
66
+ return withRetry(() => loadScript(props));
67
+ });
68
+ }
49
69
  var isApiReady = () => typeof (window == null ? void 0 : window.captchafox) !== "undefined";
50
70
 
51
71
  // src/CaptchaFox.tsx
@@ -91,7 +111,7 @@ var CaptchaFox = forwardRef(
91
111
  console.warn("[CaptchaFox] Widget has not been loaded");
92
112
  return;
93
113
  }
94
- setWidgetId("");
114
+ setWidgetId(void 0);
95
115
  window.captchafox.remove(widgetId);
96
116
  },
97
117
  execute: () => {
@@ -104,11 +124,15 @@ var CaptchaFox = forwardRef(
104
124
  },
105
125
  [widgetId]
106
126
  );
127
+ useEffect(() => {
128
+ if (widgetId) {
129
+ onLoad == null ? void 0 : onLoad();
130
+ }
131
+ }, [widgetId]);
107
132
  const renderCaptcha = () => __async(void 0, null, function* () {
108
133
  var _a, _b, _c;
109
134
  (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
110
- if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1)
111
- return;
135
+ if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return;
112
136
  const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, {
113
137
  lang,
114
138
  sitekey,
@@ -121,11 +145,9 @@ var CaptchaFox = forwardRef(
121
145
  onVerify
122
146
  });
123
147
  setWidgetId(newWidgetId);
124
- onLoad == null ? void 0 : onLoad();
125
148
  });
126
149
  useEffect(() => {
127
- if (!containerRef)
128
- return;
150
+ if (!containerRef) return;
129
151
  if (firstRendered.current) {
130
152
  if (isApiReady()) {
131
153
  renderCaptcha();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@captchafox/react",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "main": "./dist/index.cjs",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "jest": "^29.7.0",
51
51
  "jest-environment-jsdom": "^29.7.0",
52
52
  "react": "^18.2.0",
53
- "tsup": "^7.2.0",
53
+ "tsup": "^8.3.5",
54
54
  "ts-jest": "^29.1.1",
55
55
  "typescript": "^5.0.2"
56
56
  },