Skip to content

Commit 5708616

Browse files
committed
feat: configurable retry and delay
1 parent f3d8482 commit 5708616

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

lib/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const SCRIPT_COMPLETE = 'script-loaded';
66
export const SENTRY_TAG = '@hCaptcha/loader';
77

88
export const MAX_RETRIES = 2;
9+
export const RETRY_DELAY = 1000;
910

1011
export enum SentryContext {
1112
ANDROID = 'Android',

lib/src/loader.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { generateQuery, getFrame, getMountElement } from './utils';
2-
import { HCAPTCHA_LOAD_FN_NAME, MAX_RETRIES, SCRIPT_ERROR, SCRIPT_ID, SENTRY_TAG } from './constants';
2+
import { HCAPTCHA_LOAD_FN_NAME, MAX_RETRIES, RETRY_DELAY, SCRIPT_ERROR, SCRIPT_ID, SENTRY_TAG } from './constants';
33
import { initSentry } from './sentry';
44
import { fetchScript } from './script';
55

@@ -123,8 +123,18 @@ export function hCaptchaApi(params: ILoaderParams = { cleanup: false }, sentry:
123123
}
124124
}
125125

126-
export async function loadScript(params, sentry, retries = 0) {
127-
const message = retries < MAX_RETRIES ? 'Retry loading hCaptcha Api' : 'Exceeded maximum retries';
126+
function delay(ms: number): Promise<void> {
127+
return new Promise(resolve => setTimeout(resolve, ms));
128+
}
129+
130+
export async function loadScript(
131+
params: ILoaderParams,
132+
sentry: SentryHub,
133+
retries = 0
134+
): Promise<any> {
135+
const maxRetries = params.maxRetries ?? MAX_RETRIES;
136+
const retryDelay = params.retryDelay ?? RETRY_DELAY;
137+
const message = retries < maxRetries ? 'Retry loading hCaptcha Api' : 'Exceeded maximum retries';
128138

129139
try {
130140
const result = await hCaptchaApi(params, sentry);
@@ -139,19 +149,25 @@ export async function loadScript(params, sentry, retries = 0) {
139149
message,
140150
});
141151

142-
if (retries >= MAX_RETRIES) {
152+
if (retries >= maxRetries) {
143153
lastLoadFailed = true;
144-
154+
145155
sentry.captureException(error);
146156
return Promise.reject(error);
147157
} else {
158+
sentry.addBreadcrumb({
159+
category: SENTRY_TAG,
160+
message: `Waiting ${retryDelay}ms before retry attempt ${retries + 1}`,
161+
});
162+
163+
await delay(retryDelay);
148164
retries += 1;
165+
149166
return loadScript(params, sentry, retries);
150167
}
151168
}
152169
}
153170

154-
155171
export async function hCaptchaLoader(params: ILoaderParams = {}) {
156172
const sentry = initSentry(params.sentry);
157173

lib/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface ILoaderParams extends IScriptParams {
2323
hl?: string;
2424
cleanup?: boolean;
2525
uj?: boolean;
26+
maxRetries?: number;
27+
retryDelay?: number;
2628
}
2729

2830
export interface SentryHub {

0 commit comments

Comments
 (0)