@hcaptcha/react-hcaptcha 1.10.3 → 1.11.1

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/esm/index.js CHANGED
@@ -23,6 +23,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
23
23
  _this.resetCaptcha = _this.resetCaptcha.bind(_assertThisInitialized(_this));
24
24
  _this.removeCaptcha = _this.removeCaptcha.bind(_assertThisInitialized(_this));
25
25
  _this.isReady = _this.isReady.bind(_assertThisInitialized(_this));
26
+ _this._onReady = null;
26
27
 
27
28
  // Event Handlers
28
29
  _this.loadCaptcha = _this.loadCaptcha.bind(_assertThisInitialized(_this));
@@ -153,9 +154,17 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
153
154
  hCaptchaLoader(mountParams).then(this.handleOnLoad, this.handleError)["catch"](this.handleError);
154
155
  this.apiScriptRequested = true;
155
156
  };
156
- _proto.renderCaptcha = function renderCaptcha(onReady) {
157
- var isApiReady = this.state.isApiReady;
158
- if (!isApiReady) return;
157
+ _proto.renderCaptcha = function renderCaptcha(onRender) {
158
+ var _this4 = this;
159
+ var _this$state = this.state,
160
+ isApiReady = _this$state.isApiReady,
161
+ captchaId = _this$state.captchaId;
162
+ var onReady = this.props.onReady;
163
+
164
+ // Prevent calling hCaptcha render on two conditions:
165
+ // • API is not ready
166
+ // • Component has already been mounted
167
+ if (!isApiReady || captchaId) return;
159
168
  var renderParams = Object.assign({
160
169
  "open-callback": this.handleOpen,
161
170
  "close-callback": this.handleClose,
@@ -169,12 +178,14 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
169
178
  });
170
179
  var hcaptcha = this._hcaptcha;
171
180
  //Render hCaptcha widget and provide necessary callbacks - hCaptcha
172
- var captchaId = hcaptcha.render(this.ref.current, renderParams);
181
+ var id = hcaptcha.render(this.ref.current, renderParams);
173
182
  this.setState({
174
183
  isRemoved: false,
175
- captchaId: captchaId
184
+ captchaId: id
176
185
  }, function () {
186
+ onRender && onRender();
177
187
  onReady && onReady();
188
+ _this4._onReady && _this4._onReady(id);
178
189
  });
179
190
  };
180
191
  _proto.resetCaptcha = function resetCaptcha() {
@@ -208,32 +219,32 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
208
219
  });
209
220
  };
210
221
  _proto.handleOnLoad = function handleOnLoad() {
211
- var _this4 = this;
222
+ var _this5 = this;
212
223
  this.setState({
213
224
  isApiReady: true
214
225
  }, function () {
215
226
  try {
216
- var element = getMountElement(_this4.props.scriptLocation);
227
+ var element = getMountElement(_this5.props.scriptLocation);
217
228
  var frame = getFrame(element);
218
- _this4._hcaptcha = frame.window.hcaptcha;
229
+ _this5._hcaptcha = frame.window.hcaptcha;
219
230
 
220
231
  // render captcha and wait for captcha id
221
- _this4.renderCaptcha(function () {
232
+ _this5.renderCaptcha(function () {
222
233
  // trigger onLoad if it exists
223
234
 
224
- var onLoad = _this4.props.onLoad;
235
+ var onLoad = _this5.props.onLoad;
225
236
  if (onLoad) onLoad();
226
237
  });
227
238
  } catch (error) {
228
- _this4.sentryHub.captureException(error);
239
+ _this5.sentryHub.captureException(error);
229
240
  }
230
241
  });
231
242
  };
232
243
  _proto.handleSubmit = function handleSubmit(event) {
233
244
  var onVerify = this.props.onVerify;
234
- var _this$state = this.state,
235
- isRemoved = _this$state.isRemoved,
236
- captchaId = _this$state.captchaId;
245
+ var _this$state2 = this.state,
246
+ isRemoved = _this$state2.isRemoved,
247
+ captchaId = _this$state2.captchaId;
237
248
  var hcaptcha = this._hcaptcha;
238
249
  if (typeof hcaptcha === 'undefined' || isRemoved) return;
239
250
  var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget
@@ -266,9 +277,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
266
277
  if (onError) onError(event);
267
278
  };
268
279
  _proto.isReady = function isReady() {
269
- var _this$state2 = this.state,
270
- isApiReady = _this$state2.isApiReady,
271
- isRemoved = _this$state2.isRemoved;
280
+ var _this$state3 = this.state,
281
+ isApiReady = _this$state3.isApiReady,
282
+ isRemoved = _this$state3.isRemoved;
272
283
  return isApiReady && !isRemoved;
273
284
  };
274
285
  _proto.handleOpen = function handleOpen() {
@@ -290,22 +301,48 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
290
301
  this.props.onChalExpired();
291
302
  };
292
303
  _proto.execute = function execute(opts) {
304
+ var _this6 = this;
293
305
  if (opts === void 0) {
294
306
  opts = null;
295
307
  }
308
+ opts = typeof opts === 'object' ? opts : null;
296
309
  try {
297
310
  var captchaId = this.state.captchaId;
298
311
  var hcaptcha = this._hcaptcha;
299
312
  if (!this.isReady()) {
300
- return;
301
- }
302
- if (opts && typeof opts !== "object") {
303
- opts = null;
313
+ var _opts;
314
+ var onReady = new Promise(function (resolve, reject) {
315
+ _this6._onReady = function (id) {
316
+ try {
317
+ var _hcaptcha = _this6._hcaptcha;
318
+ if (opts && opts.async) {
319
+ _hcaptcha.execute(id, opts).then(resolve)["catch"](reject);
320
+ } else {
321
+ resolve(_hcaptcha.execute(id, opts));
322
+ }
323
+ } catch (e) {
324
+ reject(e);
325
+ }
326
+ };
327
+ });
328
+ return (_opts = opts) != null && _opts.async ? onReady : null;
304
329
  }
305
330
  return hcaptcha.execute(captchaId, opts);
306
331
  } catch (error) {
307
332
  this.sentryHub.captureException(error);
333
+ if (opts && opts.async) {
334
+ return Promise.reject(error);
335
+ }
336
+ return null;
337
+ }
338
+ };
339
+ _proto.close = function close() {
340
+ var captchaId = this.state.captchaId;
341
+ var hcaptcha = this._hcaptcha;
342
+ if (!this.isReady()) {
343
+ return;
308
344
  }
345
+ return hcaptcha.close(captchaId);
309
346
  };
310
347
  _proto.setData = function setData(data) {
311
348
  var captchaId = this.state.captchaId;
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
41
41
  _this.resetCaptcha = _this.resetCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
42
42
  _this.removeCaptcha = _this.removeCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
43
43
  _this.isReady = _this.isReady.bind((0, _assertThisInitialized2["default"])(_this));
44
+ _this._onReady = null;
44
45
 
45
46
  // Event Handlers
46
47
  _this.loadCaptcha = _this.loadCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
@@ -182,9 +183,17 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
182
183
  }
183
184
  }, {
184
185
  key: "renderCaptcha",
185
- value: function renderCaptcha(onReady) {
186
- var isApiReady = this.state.isApiReady;
187
- if (!isApiReady) return;
186
+ value: function renderCaptcha(onRender) {
187
+ var _this4 = this;
188
+ var _this$state = this.state,
189
+ isApiReady = _this$state.isApiReady,
190
+ captchaId = _this$state.captchaId;
191
+ var onReady = this.props.onReady;
192
+
193
+ // Prevent calling hCaptcha render on two conditions:
194
+ // • API is not ready
195
+ // • Component has already been mounted
196
+ if (!isApiReady || captchaId) return;
188
197
  var renderParams = Object.assign({
189
198
  "open-callback": this.handleOpen,
190
199
  "close-callback": this.handleClose,
@@ -198,12 +207,14 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
198
207
  });
199
208
  var hcaptcha = this._hcaptcha;
200
209
  //Render hCaptcha widget and provide necessary callbacks - hCaptcha
201
- var captchaId = hcaptcha.render(this.ref.current, renderParams);
210
+ var id = hcaptcha.render(this.ref.current, renderParams);
202
211
  this.setState({
203
212
  isRemoved: false,
204
- captchaId: captchaId
213
+ captchaId: id
205
214
  }, function () {
215
+ onRender && onRender();
206
216
  onReady && onReady();
217
+ _this4._onReady && _this4._onReady(id);
207
218
  });
208
219
  }
209
220
  }, {
@@ -243,24 +254,24 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
243
254
  }, {
244
255
  key: "handleOnLoad",
245
256
  value: function handleOnLoad() {
246
- var _this4 = this;
257
+ var _this5 = this;
247
258
  this.setState({
248
259
  isApiReady: true
249
260
  }, function () {
250
261
  try {
251
- var element = (0, _utils.getMountElement)(_this4.props.scriptLocation);
262
+ var element = (0, _utils.getMountElement)(_this5.props.scriptLocation);
252
263
  var frame = (0, _utils.getFrame)(element);
253
- _this4._hcaptcha = frame.window.hcaptcha;
264
+ _this5._hcaptcha = frame.window.hcaptcha;
254
265
 
255
266
  // render captcha and wait for captcha id
256
- _this4.renderCaptcha(function () {
267
+ _this5.renderCaptcha(function () {
257
268
  // trigger onLoad if it exists
258
269
 
259
- var onLoad = _this4.props.onLoad;
270
+ var onLoad = _this5.props.onLoad;
260
271
  if (onLoad) onLoad();
261
272
  });
262
273
  } catch (error) {
263
- _this4.sentryHub.captureException(error);
274
+ _this5.sentryHub.captureException(error);
264
275
  }
265
276
  });
266
277
  }
@@ -268,9 +279,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
268
279
  key: "handleSubmit",
269
280
  value: function handleSubmit(event) {
270
281
  var onVerify = this.props.onVerify;
271
- var _this$state = this.state,
272
- isRemoved = _this$state.isRemoved,
273
- captchaId = _this$state.captchaId;
282
+ var _this$state2 = this.state,
283
+ isRemoved = _this$state2.isRemoved,
284
+ captchaId = _this$state2.captchaId;
274
285
  var hcaptcha = this._hcaptcha;
275
286
  if (typeof hcaptcha === 'undefined' || isRemoved) return;
276
287
  var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget
@@ -309,9 +320,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
309
320
  }, {
310
321
  key: "isReady",
311
322
  value: function isReady() {
312
- var _this$state2 = this.state,
313
- isApiReady = _this$state2.isApiReady,
314
- isRemoved = _this$state2.isRemoved;
323
+ var _this$state3 = this.state,
324
+ isApiReady = _this$state3.isApiReady,
325
+ isRemoved = _this$state3.isRemoved;
315
326
  return isApiReady && !isRemoved;
316
327
  }
317
328
  }, {
@@ -341,20 +352,48 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
341
352
  }, {
342
353
  key: "execute",
343
354
  value: function execute() {
355
+ var _this6 = this;
344
356
  var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
357
+ opts = (0, _typeof2["default"])(opts) === 'object' ? opts : null;
345
358
  try {
346
359
  var captchaId = this.state.captchaId;
347
360
  var hcaptcha = this._hcaptcha;
348
361
  if (!this.isReady()) {
349
- return;
350
- }
351
- if (opts && (0, _typeof2["default"])(opts) !== "object") {
352
- opts = null;
362
+ var _opts;
363
+ var onReady = new Promise(function (resolve, reject) {
364
+ _this6._onReady = function (id) {
365
+ try {
366
+ var _hcaptcha = _this6._hcaptcha;
367
+ if (opts && opts.async) {
368
+ _hcaptcha.execute(id, opts).then(resolve)["catch"](reject);
369
+ } else {
370
+ resolve(_hcaptcha.execute(id, opts));
371
+ }
372
+ } catch (e) {
373
+ reject(e);
374
+ }
375
+ };
376
+ });
377
+ return (_opts = opts) !== null && _opts !== void 0 && _opts.async ? onReady : null;
353
378
  }
354
379
  return hcaptcha.execute(captchaId, opts);
355
380
  } catch (error) {
356
381
  this.sentryHub.captureException(error);
382
+ if (opts && opts.async) {
383
+ return Promise.reject(error);
384
+ }
385
+ return null;
386
+ }
387
+ }
388
+ }, {
389
+ key: "close",
390
+ value: function close() {
391
+ var captchaId = this.state.captchaId;
392
+ var hcaptcha = this._hcaptcha;
393
+ if (!this.isReady()) {
394
+ return;
357
395
  }
396
+ return hcaptcha.close(captchaId);
358
397
  }
359
398
  }, {
360
399
  key: "setData",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hcaptcha/react-hcaptcha",
3
- "version": "1.10.3",
3
+ "version": "1.11.1",
4
4
  "types": "types/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/index.js CHANGED
@@ -22,6 +22,7 @@ class HCaptcha extends React.Component {
22
22
  this.resetCaptcha = this.resetCaptcha.bind(this);
23
23
  this.removeCaptcha = this.removeCaptcha.bind(this);
24
24
  this.isReady = this.isReady.bind(this);
25
+ this._onReady = null;
25
26
 
26
27
  // Event Handlers
27
28
  this.loadCaptcha = this.loadCaptcha.bind(this);
@@ -169,9 +170,14 @@ class HCaptcha extends React.Component {
169
170
  this.apiScriptRequested = true;
170
171
  }
171
172
 
172
- renderCaptcha(onReady) {
173
- const { isApiReady } = this.state;
174
- if (!isApiReady) return;
173
+ renderCaptcha(onRender) {
174
+ const { isApiReady, captchaId } = this.state;
175
+ const { onReady } = this.props;
176
+
177
+ // Prevent calling hCaptcha render on two conditions:
178
+ // • API is not ready
179
+ // • Component has already been mounted
180
+ if (!isApiReady || captchaId) return;
175
181
 
176
182
  const renderParams = Object.assign({
177
183
  "open-callback" : this.handleOpen,
@@ -187,10 +193,12 @@ class HCaptcha extends React.Component {
187
193
 
188
194
  const hcaptcha = this._hcaptcha;
189
195
  //Render hCaptcha widget and provide necessary callbacks - hCaptcha
190
- const captchaId = hcaptcha.render(this.ref.current, renderParams);
196
+ const id = hcaptcha.render(this.ref.current, renderParams);
191
197
 
192
- this.setState({ isRemoved: false, captchaId }, () => {
198
+ this.setState({ isRemoved: false, captchaId: id }, () => {
199
+ onRender && onRender();
193
200
  onReady && onReady();
201
+ this._onReady && this._onReady(id);
194
202
  });
195
203
  }
196
204
 
@@ -230,7 +238,7 @@ class HCaptcha extends React.Component {
230
238
  });
231
239
  }
232
240
 
233
- handleOnLoad () {
241
+ handleOnLoad () {
234
242
  this.setState({ isApiReady: true }, () => {
235
243
  try {
236
244
  const element = getMountElement(this.props.scriptLocation);
@@ -327,23 +335,53 @@ class HCaptcha extends React.Component {
327
335
  }
328
336
 
329
337
  execute (opts = null) {
338
+
339
+ opts = typeof opts === 'object' ? opts : null;
340
+
330
341
  try {
331
342
  const { captchaId } = this.state;
332
343
  const hcaptcha = this._hcaptcha;
333
-
334
-
344
+
335
345
  if (!this.isReady()) {
336
- return;
337
- }
338
-
339
- if (opts && typeof opts !== "object") {
340
- opts = null;
346
+ const onReady = new Promise((resolve, reject) => {
347
+
348
+ this._onReady = (id) => {
349
+ try {
350
+ const hcaptcha = this._hcaptcha;
351
+
352
+ if (opts && opts.async) {
353
+ hcaptcha.execute(id, opts).then(resolve).catch(reject);
354
+ } else {
355
+ resolve(hcaptcha.execute(id, opts));
356
+ }
357
+ } catch (e) {
358
+ reject(e);
359
+ }
360
+ };
361
+ });
362
+
363
+ return opts?.async ? onReady : null;
341
364
  }
342
-
365
+
343
366
  return hcaptcha.execute(captchaId, opts);
344
367
  } catch (error) {
345
- this.sentryHub.captureException(error);
368
+ this.sentryHub.captureException(error);
369
+ if (opts && opts.async) {
370
+ return Promise.reject(error);
371
+ }
372
+ return null;
373
+ }
374
+ }
375
+
376
+ close() {
377
+ const { captchaId } = this.state;
378
+ const hcaptcha = this._hcaptcha;
379
+
380
+ if (!this.isReady()) {
381
+ return;
346
382
  }
383
+
384
+ return hcaptcha.close(captchaId);
347
385
  }
348
386
 
349
387
  setData (data) {