@captchafox/react
Advanced tools
Comparing version
import { WidgetApi, WidgetOptions } from '@captchafox/types'; | ||
import React from 'react'; | ||
declare class RetryError extends Error { | ||
} | ||
declare class TimeoutError extends Error { | ||
} | ||
type CaptchaFoxInstance = Omit<WidgetApi, 'render'>; | ||
@@ -10,2 +15,3 @@ declare const CaptchaFox: React.ForwardRefExoticComponent<WidgetOptions & { | ||
nonce?: string | undefined; | ||
executeTimeoutSeconds?: number | undefined; | ||
} & React.RefAttributes<CaptchaFoxInstance>>; | ||
@@ -15,2 +21,2 @@ | ||
export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance }; | ||
export { CAPTCHA_RESPONSE_KEY, CaptchaFox, type CaptchaFoxInstance, RetryError, TimeoutError }; |
@@ -24,4 +24,9 @@ "use client"; | ||
// ../internal/dist/index.mjs | ||
var RetryError = class extends Error { | ||
}; | ||
var TimeoutError = class extends Error { | ||
}; | ||
var backoff = (retryCount) => Math.exp(retryCount) * 150; | ||
var withRetry = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (callback, { attempts = 4 } = {}) { | ||
var _a; | ||
let error; | ||
@@ -36,3 +41,3 @@ for (let i = 0; i < attempts; i++) { | ||
} | ||
throw error != null ? error : new Error("Exhausted all retries"); | ||
throw new RetryError((_a = error == null ? void 0 : error.message) != null ? _a : "Exhausted all retries"); | ||
}); | ||
@@ -77,2 +82,3 @@ var mountInstance; | ||
({ | ||
executeTimeoutSeconds = 30, | ||
sitekey, | ||
@@ -94,2 +100,4 @@ lang, | ||
const firstRendered = useRef(false); | ||
const onReady = useRef(); | ||
const executeTimeout = useRef(); | ||
useImperativeHandle( | ||
@@ -123,3 +131,3 @@ ref, | ||
if (!isApiReady() || !widgetId) { | ||
return Promise.reject("[CaptchaFox] Widget has not been loaded"); | ||
return waitAndExecute(); | ||
} | ||
@@ -137,20 +145,6 @@ return window.captchafox.execute(widgetId); | ||
}, [widgetId]); | ||
const renderCaptcha = () => __async(void 0, null, function* () { | ||
var _a, _b, _c; | ||
(_a = window.captchafox) == null ? void 0 : _a.remove(widgetId); | ||
if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return; | ||
const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, { | ||
lang, | ||
sitekey, | ||
mode, | ||
theme, | ||
i18n, | ||
onError, | ||
onFail, | ||
onClose, | ||
onVerify | ||
}); | ||
setWidgetId(newWidgetId); | ||
}); | ||
useEffect(() => { | ||
return () => clearTimeout(executeTimeout.current); | ||
}, []); | ||
useEffect(() => { | ||
if (!containerRef) return; | ||
@@ -173,2 +167,37 @@ if (firstRendered.current) { | ||
}, [containerRef, sitekey, lang, mode]); | ||
const waitAndExecute = () => { | ||
return new Promise((resolve, reject) => { | ||
executeTimeout.current = setTimeout(() => { | ||
reject(new TimeoutError("Execute timed out")); | ||
}, executeTimeoutSeconds * 1e3); | ||
onReady.current = (id) => { | ||
clearTimeout(executeTimeout.current); | ||
window.captchafox.execute(id).then(resolve).catch(reject); | ||
}; | ||
}); | ||
}; | ||
const renderCaptcha = () => __async(void 0, null, function* () { | ||
var _a, _b, _c; | ||
(_a = window.captchafox) == null ? void 0 : _a.remove(widgetId); | ||
if (!containerRef || ((_b = containerRef == null ? void 0 : containerRef.children) == null ? void 0 : _b.length) === 1) return; | ||
const newWidgetId = yield (_c = window.captchafox) == null ? void 0 : _c.render(containerRef, { | ||
lang, | ||
sitekey, | ||
mode, | ||
theme, | ||
i18n, | ||
onError, | ||
onFail, | ||
onClose, | ||
onVerify | ||
}); | ||
if (!newWidgetId) { | ||
return; | ||
} | ||
setWidgetId(newWidgetId); | ||
if (onReady.current) { | ||
onReady.current(newWidgetId); | ||
onReady.current = void 0; | ||
} | ||
}); | ||
return /* @__PURE__ */ React.createElement("div", { ref: setContainerRef, id: widgetId, className }); | ||
@@ -183,3 +212,5 @@ } | ||
CAPTCHA_RESPONSE_KEY, | ||
CaptchaFox | ||
CaptchaFox, | ||
RetryError, | ||
TimeoutError | ||
}; |
{ | ||
"name": "@captchafox/react", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"main": "./dist/index.cjs", | ||
@@ -5,0 +5,0 @@ "module": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21828
11.37%464
17.47%