@captchafox/react 1.7.0 → 1.9.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
@@ -134,6 +134,8 @@ var CaptchaFox = (0, import_react.forwardRef)(
134
134
  const firstRendered = (0, import_react.useRef)(false);
135
135
  const onReady = (0, import_react.useRef)();
136
136
  const executeTimeout = (0, import_react.useRef)();
137
+ const scriptErrorListener = (0, import_react.useRef)();
138
+ const hasScriptError = (0, import_react.useRef)(false);
137
139
  (0, import_react.useImperativeHandle)(
138
140
  ref,
139
141
  () => {
@@ -160,12 +162,21 @@ var CaptchaFox = (0, import_react.forwardRef)(
160
162
  setWidgetId(void 0);
161
163
  window.captchafox.remove(widgetId);
162
164
  },
163
- execute: () => {
165
+ execute: () => __async(void 0, null, function* () {
166
+ if (hasScriptError.current) {
167
+ return Promise.reject(new RetryError());
168
+ }
164
169
  if (!isApiReady() || !widgetId) {
165
170
  return waitAndExecute();
166
171
  }
167
- return window.captchafox.execute(widgetId);
168
- }
172
+ try {
173
+ const token = yield window.captchafox.execute(widgetId);
174
+ return token;
175
+ } catch (error) {
176
+ const errorType = getErrorType(error);
177
+ return Promise.reject(errorType);
178
+ }
179
+ })
169
180
  };
170
181
  },
171
182
  [widgetId]
@@ -176,7 +187,9 @@ var CaptchaFox = (0, import_react.forwardRef)(
176
187
  }
177
188
  }, [widgetId]);
178
189
  (0, import_react.useEffect)(() => {
179
- return () => clearTimeout(executeTimeout.current);
190
+ return () => {
191
+ clearEvents();
192
+ };
180
193
  }, []);
181
194
  (0, import_react.useEffect)(() => {
182
195
  if (!containerRef) return;
@@ -191,22 +204,41 @@ var CaptchaFox = (0, import_react.forwardRef)(
191
204
  yield renderCaptcha();
192
205
  }
193
206
  })).catch((err) => {
207
+ var _a;
194
208
  onError == null ? void 0 : onError(err);
209
+ hasScriptError.current = true;
210
+ (_a = scriptErrorListener.current) == null ? void 0 : _a.call(scriptErrorListener);
195
211
  console.error("[CaptchaFox] Could not load script:", err);
196
212
  });
197
213
  }
198
214
  }, [containerRef, sitekey, lang, mode]);
215
+ const clearEvents = () => {
216
+ clearTimeout(executeTimeout.current);
217
+ };
199
218
  const waitAndExecute = () => {
200
219
  return new Promise((resolve, reject) => {
220
+ scriptErrorListener.current = () => {
221
+ clearEvents();
222
+ reject(new RetryError());
223
+ };
201
224
  executeTimeout.current = setTimeout(() => {
202
225
  reject(new TimeoutError("Execute timed out"));
203
226
  }, executeTimeoutSeconds * 1e3);
204
227
  onReady.current = (id) => {
205
- clearTimeout(executeTimeout.current);
206
- window.captchafox.execute(id).then(resolve).catch(reject);
228
+ clearEvents();
229
+ window.captchafox.execute(id).then(resolve).catch((error) => {
230
+ const errorType = getErrorType(error);
231
+ reject(errorType);
232
+ });
207
233
  };
208
234
  });
209
235
  };
236
+ const getErrorType = (error) => {
237
+ if (error !== "challenge-aborted" && error !== "rate-limited") {
238
+ return new RetryError();
239
+ }
240
+ return error;
241
+ };
210
242
  const renderCaptcha = () => __async(void 0, null, function* () {
211
243
  var _a, _b, _c;
212
244
  (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
package/dist/index.js CHANGED
@@ -96,6 +96,8 @@ var CaptchaFox = forwardRef(
96
96
  const firstRendered = useRef(false);
97
97
  const onReady = useRef();
98
98
  const executeTimeout = useRef();
99
+ const scriptErrorListener = useRef();
100
+ const hasScriptError = useRef(false);
99
101
  useImperativeHandle(
100
102
  ref,
101
103
  () => {
@@ -122,12 +124,21 @@ var CaptchaFox = forwardRef(
122
124
  setWidgetId(void 0);
123
125
  window.captchafox.remove(widgetId);
124
126
  },
125
- execute: () => {
127
+ execute: () => __async(void 0, null, function* () {
128
+ if (hasScriptError.current) {
129
+ return Promise.reject(new RetryError());
130
+ }
126
131
  if (!isApiReady() || !widgetId) {
127
132
  return waitAndExecute();
128
133
  }
129
- return window.captchafox.execute(widgetId);
130
- }
134
+ try {
135
+ const token = yield window.captchafox.execute(widgetId);
136
+ return token;
137
+ } catch (error) {
138
+ const errorType = getErrorType(error);
139
+ return Promise.reject(errorType);
140
+ }
141
+ })
131
142
  };
132
143
  },
133
144
  [widgetId]
@@ -138,7 +149,9 @@ var CaptchaFox = forwardRef(
138
149
  }
139
150
  }, [widgetId]);
140
151
  useEffect(() => {
141
- return () => clearTimeout(executeTimeout.current);
152
+ return () => {
153
+ clearEvents();
154
+ };
142
155
  }, []);
143
156
  useEffect(() => {
144
157
  if (!containerRef) return;
@@ -153,22 +166,41 @@ var CaptchaFox = forwardRef(
153
166
  yield renderCaptcha();
154
167
  }
155
168
  })).catch((err) => {
169
+ var _a;
156
170
  onError == null ? void 0 : onError(err);
171
+ hasScriptError.current = true;
172
+ (_a = scriptErrorListener.current) == null ? void 0 : _a.call(scriptErrorListener);
157
173
  console.error("[CaptchaFox] Could not load script:", err);
158
174
  });
159
175
  }
160
176
  }, [containerRef, sitekey, lang, mode]);
177
+ const clearEvents = () => {
178
+ clearTimeout(executeTimeout.current);
179
+ };
161
180
  const waitAndExecute = () => {
162
181
  return new Promise((resolve, reject) => {
182
+ scriptErrorListener.current = () => {
183
+ clearEvents();
184
+ reject(new RetryError());
185
+ };
163
186
  executeTimeout.current = setTimeout(() => {
164
187
  reject(new TimeoutError("Execute timed out"));
165
188
  }, executeTimeoutSeconds * 1e3);
166
189
  onReady.current = (id) => {
167
- clearTimeout(executeTimeout.current);
168
- window.captchafox.execute(id).then(resolve).catch(reject);
190
+ clearEvents();
191
+ window.captchafox.execute(id).then(resolve).catch((error) => {
192
+ const errorType = getErrorType(error);
193
+ reject(errorType);
194
+ });
169
195
  };
170
196
  });
171
197
  };
198
+ const getErrorType = (error) => {
199
+ if (error !== "challenge-aborted" && error !== "rate-limited") {
200
+ return new RetryError();
201
+ }
202
+ return error;
203
+ };
172
204
  const renderCaptcha = () => __async(void 0, null, function* () {
173
205
  var _a, _b, _c;
174
206
  (_a = window.captchafox) == null ? void 0 : _a.remove(widgetId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@captchafox/react",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "main": "./dist/index.cjs",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",