@hcaptcha/react-hcaptcha 1.10.3 → 1.11.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/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));
@@ -154,6 +155,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
154
155
  this.apiScriptRequested = true;
155
156
  };
156
157
  _proto.renderCaptcha = function renderCaptcha(onReady) {
158
+ var _this4 = this;
157
159
  var isApiReady = this.state.isApiReady;
158
160
  if (!isApiReady) return;
159
161
  var renderParams = Object.assign({
@@ -175,6 +177,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
175
177
  captchaId: captchaId
176
178
  }, function () {
177
179
  onReady && onReady();
180
+ _this4._onReady && _this4._onReady(captchaId);
178
181
  });
179
182
  };
180
183
  _proto.resetCaptcha = function resetCaptcha() {
@@ -208,24 +211,24 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
208
211
  });
209
212
  };
210
213
  _proto.handleOnLoad = function handleOnLoad() {
211
- var _this4 = this;
214
+ var _this5 = this;
212
215
  this.setState({
213
216
  isApiReady: true
214
217
  }, function () {
215
218
  try {
216
- var element = getMountElement(_this4.props.scriptLocation);
219
+ var element = getMountElement(_this5.props.scriptLocation);
217
220
  var frame = getFrame(element);
218
- _this4._hcaptcha = frame.window.hcaptcha;
221
+ _this5._hcaptcha = frame.window.hcaptcha;
219
222
 
220
223
  // render captcha and wait for captcha id
221
- _this4.renderCaptcha(function () {
224
+ _this5.renderCaptcha(function () {
222
225
  // trigger onLoad if it exists
223
226
 
224
- var onLoad = _this4.props.onLoad;
227
+ var onLoad = _this5.props.onLoad;
225
228
  if (onLoad) onLoad();
226
229
  });
227
230
  } catch (error) {
228
- _this4.sentryHub.captureException(error);
231
+ _this5.sentryHub.captureException(error);
229
232
  }
230
233
  });
231
234
  };
@@ -290,21 +293,39 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
290
293
  this.props.onChalExpired();
291
294
  };
292
295
  _proto.execute = function execute(opts) {
296
+ var _this6 = this;
293
297
  if (opts === void 0) {
294
298
  opts = null;
295
299
  }
300
+ opts = typeof opts === 'object' ? opts : null;
296
301
  try {
297
302
  var captchaId = this.state.captchaId;
298
303
  var hcaptcha = this._hcaptcha;
299
304
  if (!this.isReady()) {
300
- return;
301
- }
302
- if (opts && typeof opts !== "object") {
303
- opts = null;
305
+ var _opts;
306
+ var onReady = new Promise(function (resolve, reject) {
307
+ _this6._onReady = function (id) {
308
+ try {
309
+ var _hcaptcha = _this6._hcaptcha;
310
+ if (opts && opts.async) {
311
+ _hcaptcha.execute(id, opts).then(resolve)["catch"](reject);
312
+ } else {
313
+ resolve(_hcaptcha.execute(id, opts));
314
+ }
315
+ } catch (e) {
316
+ reject(e);
317
+ }
318
+ };
319
+ });
320
+ return (_opts = opts) != null && _opts.async ? onReady : null;
304
321
  }
305
322
  return hcaptcha.execute(captchaId, opts);
306
323
  } catch (error) {
307
324
  this.sentryHub.captureException(error);
325
+ if (opts && opts.async) {
326
+ return Promise.reject(error);
327
+ }
328
+ return null;
308
329
  }
309
330
  };
310
331
  _proto.setData = function setData(data) {
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));
@@ -183,6 +184,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
183
184
  }, {
184
185
  key: "renderCaptcha",
185
186
  value: function renderCaptcha(onReady) {
187
+ var _this4 = this;
186
188
  var isApiReady = this.state.isApiReady;
187
189
  if (!isApiReady) return;
188
190
  var renderParams = Object.assign({
@@ -204,6 +206,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
204
206
  captchaId: captchaId
205
207
  }, function () {
206
208
  onReady && onReady();
209
+ _this4._onReady && _this4._onReady(captchaId);
207
210
  });
208
211
  }
209
212
  }, {
@@ -243,24 +246,24 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
243
246
  }, {
244
247
  key: "handleOnLoad",
245
248
  value: function handleOnLoad() {
246
- var _this4 = this;
249
+ var _this5 = this;
247
250
  this.setState({
248
251
  isApiReady: true
249
252
  }, function () {
250
253
  try {
251
- var element = (0, _utils.getMountElement)(_this4.props.scriptLocation);
254
+ var element = (0, _utils.getMountElement)(_this5.props.scriptLocation);
252
255
  var frame = (0, _utils.getFrame)(element);
253
- _this4._hcaptcha = frame.window.hcaptcha;
256
+ _this5._hcaptcha = frame.window.hcaptcha;
254
257
 
255
258
  // render captcha and wait for captcha id
256
- _this4.renderCaptcha(function () {
259
+ _this5.renderCaptcha(function () {
257
260
  // trigger onLoad if it exists
258
261
 
259
- var onLoad = _this4.props.onLoad;
262
+ var onLoad = _this5.props.onLoad;
260
263
  if (onLoad) onLoad();
261
264
  });
262
265
  } catch (error) {
263
- _this4.sentryHub.captureException(error);
266
+ _this5.sentryHub.captureException(error);
264
267
  }
265
268
  });
266
269
  }
@@ -341,19 +344,37 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
341
344
  }, {
342
345
  key: "execute",
343
346
  value: function execute() {
347
+ var _this6 = this;
344
348
  var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
349
+ opts = (0, _typeof2["default"])(opts) === 'object' ? opts : null;
345
350
  try {
346
351
  var captchaId = this.state.captchaId;
347
352
  var hcaptcha = this._hcaptcha;
348
353
  if (!this.isReady()) {
349
- return;
350
- }
351
- if (opts && (0, _typeof2["default"])(opts) !== "object") {
352
- opts = null;
354
+ var _opts;
355
+ var onReady = new Promise(function (resolve, reject) {
356
+ _this6._onReady = function (id) {
357
+ try {
358
+ var _hcaptcha = _this6._hcaptcha;
359
+ if (opts && opts.async) {
360
+ _hcaptcha.execute(id, opts).then(resolve)["catch"](reject);
361
+ } else {
362
+ resolve(_hcaptcha.execute(id, opts));
363
+ }
364
+ } catch (e) {
365
+ reject(e);
366
+ }
367
+ };
368
+ });
369
+ return (_opts = opts) !== null && _opts !== void 0 && _opts.async ? onReady : null;
353
370
  }
354
371
  return hcaptcha.execute(captchaId, opts);
355
372
  } catch (error) {
356
373
  this.sentryHub.captureException(error);
374
+ if (opts && opts.async) {
375
+ return Promise.reject(error);
376
+ }
377
+ return null;
357
378
  }
358
379
  }
359
380
  }, {
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.0",
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);
@@ -191,6 +192,7 @@ class HCaptcha extends React.Component {
191
192
 
192
193
  this.setState({ isRemoved: false, captchaId }, () => {
193
194
  onReady && onReady();
195
+ this._onReady && this._onReady(captchaId);
194
196
  });
195
197
  }
196
198
 
@@ -230,7 +232,7 @@ class HCaptcha extends React.Component {
230
232
  });
231
233
  }
232
234
 
233
- handleOnLoad () {
235
+ handleOnLoad () {
234
236
  this.setState({ isApiReady: true }, () => {
235
237
  try {
236
238
  const element = getMountElement(this.props.scriptLocation);
@@ -327,22 +329,41 @@ class HCaptcha extends React.Component {
327
329
  }
328
330
 
329
331
  execute (opts = null) {
332
+
333
+ opts = typeof opts === 'object' ? opts : null;
334
+
330
335
  try {
331
336
  const { captchaId } = this.state;
332
337
  const hcaptcha = this._hcaptcha;
333
-
334
-
338
+
335
339
  if (!this.isReady()) {
336
- return;
337
- }
338
-
339
- if (opts && typeof opts !== "object") {
340
- opts = null;
340
+ const onReady = new Promise((resolve, reject) => {
341
+
342
+ this._onReady = (id) => {
343
+ try {
344
+ const hcaptcha = this._hcaptcha;
345
+
346
+ if (opts && opts.async) {
347
+ hcaptcha.execute(id, opts).then(resolve).catch(reject);
348
+ } else {
349
+ resolve(hcaptcha.execute(id, opts));
350
+ }
351
+ } catch (e) {
352
+ reject(e);
353
+ }
354
+ };
355
+ });
356
+
357
+ return opts?.async ? onReady : null;
341
358
  }
342
-
359
+
343
360
  return hcaptcha.execute(captchaId, opts);
344
361
  } catch (error) {
345
- this.sentryHub.captureException(error);
362
+ this.sentryHub.captureException(error);
363
+ if (opts && opts.async) {
364
+ return Promise.reject(error);
365
+ }
366
+ return null;
346
367
  }
347
368
  }
348
369