@captchafox/react 1.6.0 → 1.7.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
@@ -52,13 +52,20 @@ var __async = (__this, __arguments, generator) => {
52
52
  var src_exports = {};
53
53
  __export(src_exports, {
54
54
  CAPTCHA_RESPONSE_KEY: () => CAPTCHA_RESPONSE_KEY,
55
- CaptchaFox: () => CaptchaFox
55
+ CaptchaFox: () => CaptchaFox,
56
+ RetryError: () => RetryError,
57
+ TimeoutError: () => TimeoutError
56
58
  });
57
59
  module.exports = __toCommonJS(src_exports);
58
60
 
59
61
  // ../internal/dist/index.mjs
62
+ var RetryError = class extends Error {
63
+ };
64
+ var TimeoutError = class extends Error {
65
+ };
60
66
  var backoff = (retryCount) => Math.exp(retryCount) * 150;
61
67
  var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback, { attempts = 4 } = {}) {
68
+ var _a;
62
69
  let error;
63
70
  for (let i = 0; i < attempts; i++) {
64
71
  try {
@@ -68,7 +75,7 @@ var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback,
68
75
  yield new Promise((r) => setTimeout(r, backoff(i)));
69
76
  }
70
77
  }
71
- throw error != null ? error : new Error("Exhausted all retries");
78
+ throw new RetryError((_a = error == null ? void 0 : error.message) != null ? _a : "Exhausted all retries");
72
79
  });
73
80
  var mountInstance;
74
81
  var LOAD_FUNC_KEY = "captchaFoxOnLoad";
@@ -108,6 +115,7 @@ var isApiReady = () => typeof (window == null ? void 0 : window.captchafox) !==
108
115
  var import_react = __toESM(require("react"), 1);
109
116
  var CaptchaFox = (0, import_react.forwardRef)(
110
117
  ({
118
+ executeTimeoutSeconds = 30,
111
119
  sitekey,
112
120
  lang,
113
121
  mode,
@@ -124,6 +132,8 @@ var CaptchaFox = (0, import_react.forwardRef)(
124
132
  const [containerRef, setContainerRef] = (0, import_react.useState)();
125
133
  const [widgetId, setWidgetId] = (0, import_react.useState)();
126
134
  const firstRendered = (0, import_react.useRef)(false);
135
+ const onReady = (0, import_react.useRef)();
136
+ const executeTimeout = (0, import_react.useRef)();
127
137
  (0, import_react.useImperativeHandle)(
128
138
  ref,
129
139
  () => {
@@ -152,7 +162,7 @@ var CaptchaFox = (0, import_react.forwardRef)(
152
162
  },
153
163
  execute: () => {
154
164
  if (!isApiReady() || !widgetId) {
155
- return Promise.reject("[CaptchaFox] Widget has not been loaded");
165
+ return waitAndExecute();
156
166
  }
157
167
  return window.captchafox.execute(widgetId);
158
168
  }
@@ -165,23 +175,9 @@ var CaptchaFox = (0, import_react.forwardRef)(
165
175
  onLoad == null ? void 0 : onLoad();
166
176
  }
167
177
  }, [widgetId]);
168
- const renderCaptcha = () => __async(void 0, null, function* () {
169
- var _a, _b, _c;
170
- (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
171
- if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return;
172
- const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, {
173
- lang,
174
- sitekey,
175
- mode,
176
- theme,
177
- i18n,
178
- onError,
179
- onFail,
180
- onClose,
181
- onVerify
182
- });
183
- setWidgetId(newWidgetId);
184
- });
178
+ (0, import_react.useEffect)(() => {
179
+ return () => clearTimeout(executeTimeout.current);
180
+ }, []);
185
181
  (0, import_react.useEffect)(() => {
186
182
  if (!containerRef) return;
187
183
  if (firstRendered.current) {
@@ -200,6 +196,41 @@ var CaptchaFox = (0, import_react.forwardRef)(
200
196
  });
201
197
  }
202
198
  }, [containerRef, sitekey, lang, mode]);
199
+ const waitAndExecute = () => {
200
+ return new Promise((resolve, reject) => {
201
+ executeTimeout.current = setTimeout(() => {
202
+ reject(new TimeoutError("Execute timed out"));
203
+ }, executeTimeoutSeconds * 1e3);
204
+ onReady.current = (id) => {
205
+ clearTimeout(executeTimeout.current);
206
+ window.captchafox.execute(id).then(resolve).catch(reject);
207
+ };
208
+ });
209
+ };
210
+ const renderCaptcha = () => __async(void 0, null, function* () {
211
+ var _a, _b, _c;
212
+ (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
213
+ if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return;
214
+ const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, {
215
+ lang,
216
+ sitekey,
217
+ mode,
218
+ theme,
219
+ i18n,
220
+ onError,
221
+ onFail,
222
+ onClose,
223
+ onVerify
224
+ });
225
+ if (!newWidgetId) {
226
+ return;
227
+ }
228
+ setWidgetId(newWidgetId);
229
+ if (onReady.current) {
230
+ onReady.current(newWidgetId);
231
+ onReady.current = void 0;
232
+ }
233
+ });
203
234
  return /* @__PURE__ */ import_react.default.createElement("div", { ref: setContainerRef, id: widgetId, className });
204
235
  }
205
236
  );
@@ -210,5 +241,7 @@ var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
210
241
  // Annotate the CommonJS export names for ESM import in node:
211
242
  0 && (module.exports = {
212
243
  CAPTCHA_RESPONSE_KEY,
213
- CaptchaFox
244
+ CaptchaFox,
245
+ RetryError,
246
+ TimeoutError
214
247
  });
package/dist/index.d.cts CHANGED
@@ -1,14 +1,20 @@
1
1
  import { WidgetApi, WidgetOptions } from '@captchafox/types';
2
2
  import React from 'react';
3
3
 
4
+ declare class RetryError extends Error {
5
+ }
6
+ declare class TimeoutError extends Error {
7
+ }
8
+
4
9
  type CaptchaFoxInstance = Omit<WidgetApi, 'render'>;
5
10
  declare const CaptchaFox: React.ForwardRefExoticComponent<WidgetOptions & {
6
11
  /** Called after the widget has been loaded */
7
12
  onLoad?: (() => void) | undefined;
8
13
  className?: string | undefined;
9
14
  nonce?: string | undefined;
15
+ executeTimeoutSeconds?: number | undefined;
10
16
  } & React.RefAttributes<CaptchaFoxInstance>>;
11
17
 
12
18
  declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
13
19
 
14
- export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance };
20
+ export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance, RetryError, TimeoutError };
package/dist/index.d.ts CHANGED
@@ -1,14 +1,20 @@
1
1
  import { WidgetApi, WidgetOptions } from '@captchafox/types';
2
2
  import React from 'react';
3
3
 
4
+ declare class RetryError extends Error {
5
+ }
6
+ declare class TimeoutError extends Error {
7
+ }
8
+
4
9
  type CaptchaFoxInstance = Omit<WidgetApi, 'render'>;
5
10
  declare const CaptchaFox: React.ForwardRefExoticComponent<WidgetOptions & {
6
11
  /** Called after the widget has been loaded */
7
12
  onLoad?: (() => void) | undefined;
8
13
  className?: string | undefined;
9
14
  nonce?: string | undefined;
15
+ executeTimeoutSeconds?: number | undefined;
10
16
  } & React.RefAttributes<CaptchaFoxInstance>>;
11
17
 
12
18
  declare const CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
13
19
 
14
- export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance };
20
+ export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance, RetryError, TimeoutError };
package/dist/index.js CHANGED
@@ -21,8 +21,13 @@ var __async = (__this, __arguments, generator) => {
21
21
  };
22
22
 
23
23
  // ../internal/dist/index.mjs
24
+ var RetryError = class extends Error {
25
+ };
26
+ var TimeoutError = class extends Error {
27
+ };
24
28
  var backoff = (retryCount) => Math.exp(retryCount) * 150;
25
29
  var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback, { attempts = 4 } = {}) {
30
+ var _a;
26
31
  let error;
27
32
  for (let i = 0; i < attempts; i++) {
28
33
  try {
@@ -32,7 +37,7 @@ var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback,
32
37
  yield new Promise((r) => setTimeout(r, backoff(i)));
33
38
  }
34
39
  }
35
- throw error != null ? error : new Error("Exhausted all retries");
40
+ throw new RetryError((_a = error == null ? void 0 : error.message) != null ? _a : "Exhausted all retries");
36
41
  });
37
42
  var mountInstance;
38
43
  var LOAD_FUNC_KEY = "captchaFoxOnLoad";
@@ -72,6 +77,7 @@ var isApiReady = () => typeof (window == null ? void 0 : window.captchafox) !==
72
77
  import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
73
78
  var CaptchaFox = forwardRef(
74
79
  ({
80
+ executeTimeoutSeconds = 30,
75
81
  sitekey,
76
82
  lang,
77
83
  mode,
@@ -88,6 +94,8 @@ var CaptchaFox = forwardRef(
88
94
  const [containerRef, setContainerRef] = useState();
89
95
  const [widgetId, setWidgetId] = useState();
90
96
  const firstRendered = useRef(false);
97
+ const onReady = useRef();
98
+ const executeTimeout = useRef();
91
99
  useImperativeHandle(
92
100
  ref,
93
101
  () => {
@@ -116,7 +124,7 @@ var CaptchaFox = forwardRef(
116
124
  },
117
125
  execute: () => {
118
126
  if (!isApiReady() || !widgetId) {
119
- return Promise.reject("[CaptchaFox] Widget has not been loaded");
127
+ return waitAndExecute();
120
128
  }
121
129
  return window.captchafox.execute(widgetId);
122
130
  }
@@ -129,23 +137,9 @@ var CaptchaFox = forwardRef(
129
137
  onLoad == null ? void 0 : onLoad();
130
138
  }
131
139
  }, [widgetId]);
132
- const renderCaptcha = () => __async(void 0, null, function* () {
133
- var _a, _b, _c;
134
- (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
135
- if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return;
136
- const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, {
137
- lang,
138
- sitekey,
139
- mode,
140
- theme,
141
- i18n,
142
- onError,
143
- onFail,
144
- onClose,
145
- onVerify
146
- });
147
- setWidgetId(newWidgetId);
148
- });
140
+ useEffect(() => {
141
+ return () => clearTimeout(executeTimeout.current);
142
+ }, []);
149
143
  useEffect(() => {
150
144
  if (!containerRef) return;
151
145
  if (firstRendered.current) {
@@ -164,6 +158,41 @@ var CaptchaFox = forwardRef(
164
158
  });
165
159
  }
166
160
  }, [containerRef, sitekey, lang, mode]);
161
+ const waitAndExecute = () => {
162
+ return new Promise((resolve, reject) => {
163
+ executeTimeout.current = setTimeout(() => {
164
+ reject(new TimeoutError("Execute timed out"));
165
+ }, executeTimeoutSeconds * 1e3);
166
+ onReady.current = (id) => {
167
+ clearTimeout(executeTimeout.current);
168
+ window.captchafox.execute(id).then(resolve).catch(reject);
169
+ };
170
+ });
171
+ };
172
+ const renderCaptcha = () => __async(void 0, null, function* () {
173
+ var _a, _b, _c;
174
+ (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
175
+ if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return;
176
+ const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, {
177
+ lang,
178
+ sitekey,
179
+ mode,
180
+ theme,
181
+ i18n,
182
+ onError,
183
+ onFail,
184
+ onClose,
185
+ onVerify
186
+ });
187
+ if (!newWidgetId) {
188
+ return;
189
+ }
190
+ setWidgetId(newWidgetId);
191
+ if (onReady.current) {
192
+ onReady.current(newWidgetId);
193
+ onReady.current = void 0;
194
+ }
195
+ });
167
196
  return /* @__PURE__ */ React.createElement("div", { ref: setContainerRef, id: widgetId, className });
168
197
  }
169
198
  );
@@ -173,5 +202,7 @@ CaptchaFox.displayName = "CaptchaFox";
173
202
  var CAPTCHA_RESPONSE_KEY = "cf-captcha-response";
174
203
  export {
175
204
  CAPTCHA_RESPONSE_KEY,
176
- CaptchaFox
205
+ CaptchaFox,
206
+ RetryError,
207
+ TimeoutError
177
208
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@captchafox/react",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "main": "./dist/index.cjs",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",