From 83dc24c170ab1d63415c77f03bdb02a5f75b1b54 Mon Sep 17 00:00:00 2001 From: Adekunle Adebayo Date: Wed, 20 Apr 2022 16:14:27 +0100 Subject: [PATCH 1/9] methods exposed --- examples/src/index.js | 22 ++++++++++++++++++++++ src/index.js | 34 ++++++++++++++++++++++++++++++++++ tests/hcaptcha.spec.js | 27 +++++++++++++++++++++++++++ types/index.d.ts | 3 +++ 4 files changed, 86 insertions(+) diff --git a/examples/src/index.js b/examples/src/index.js index 9d58bd0..387a518 100644 --- a/examples/src/index.js +++ b/examples/src/index.js @@ -16,6 +16,26 @@ const AsyncDemo = () => { } }; + const getResponse = async () => { + try { + const res = await captchaRef.current.getResponse(); + console.log("Response: ", res); + + } catch (error) { + console.log(error); + } + }; + + const getRespKey = async () => { + try { + const res = await captchaRef.current.getRespKey(); + console.log("Response Key: ", res); + + } catch (error) { + console.log(error); + } + }; + const handleOpen = () => { console.log("HCaptcha [onOpen]: The user display of a challenge starts."); }; @@ -40,6 +60,8 @@ const AsyncDemo = () => { onChalExpired={handleChallengeExpired} /> + + ); } diff --git a/src/index.js b/src/index.js index ac33bd1..b931e5f 100644 --- a/src/index.js +++ b/src/index.js @@ -265,6 +265,40 @@ class HCaptcha extends React.Component { return hcaptcha.execute(captchaId, opts); } + setData (opts) { + const { captchaId } = this.state; + + if (!this.isReady()) { + return; + } + + if (opts && typeof opts !== "object") { + opts = null; + } + + hcaptcha.setData(captchaId, opts); + } + + getResponse() { + const { captchaId } = this.state; + + if (!this.isReady()) { + return; + } + //Get response token from hCaptcha widget + return hcaptcha.getResponse(captchaId); + } + + getRespKey() { + const { captchaId } = this.state; + + if (!this.isReady()) { + return; + } + //Get current challenge session id from hCaptcha widget + return hcaptcha.getRespKey(captchaId) + } + render () { const { elementId } = this.state; return
diff --git a/tests/hcaptcha.spec.js b/tests/hcaptcha.spec.js index 0b3b8ef..346af7c 100644 --- a/tests/hcaptcha.spec.js +++ b/tests/hcaptcha.spec.js @@ -58,8 +58,14 @@ describe("hCaptcha", () => { it("has functions", () => { expect(typeof instance.execute).toBe("function"); expect(typeof instance.resetCaptcha).toBe("function"); + expect(typeof instance.getResponse).toBe("function"); + expect(typeof instance.getRespKey).toBe("function"); + expect(typeof instance.setData).toBe("function"); expect(instance.execute).toBeDefined(); expect(instance.resetCaptcha).toBeDefined(); + expect(instance.getResponse).toBeDefined(); + expect(instance.getRespKey).toBeDefined(); + expect(instance.setData).toBeDefined(); }); it("can execute synchronously without arguments", () => { @@ -120,6 +126,27 @@ describe("hCaptcha", () => { expect(window.hcaptcha.remove.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); }); + it("can get Response", () => { + expect(window.hcaptcha.getResponse.mock.calls.length).toBe(0); + instance.getResponse(); + expect(window.hcaptcha.getResponse.mock.calls.length).toBe(1); + expect(window.hcaptcha.getResponse.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); + }); + + it("can get RespKey", () => { + expect(window.hcaptcha.getRespKey.mock.calls.length).toBe(0); + instance.getRespKey(); + expect(window.hcaptcha.getRespKey.mock.calls.length).toBe(1); + expect(window.hcaptcha.getRespKey.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); + }); + + it("can set Data", () => { + expect(window.hcaptcha.setData.mock.calls.length).toBe(0); + instance.setData(); + expect(window.hcaptcha.setData.mock.calls.length).toBe(1); + expect(window.hcaptcha.setData.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); + }); + it("emits onLoad event", () => { expect(mockFns.onLoad.mock.calls.length).toBe(0); instance.handleOnLoad(); diff --git a/types/index.d.ts b/types/index.d.ts index 730a099..c1ad2dc 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -39,6 +39,9 @@ declare class HCaptcha extends React.Component { resetCaptcha(): void; renderCaptcha(): void; removeCaptcha(): void; + setData(): void; + getRespKey(): string; + getResponse(): string; execute(opts: { async: true }): Promise; execute(opts?: { async: false }): void; execute(opts?: { async: boolean }): Promise | void; From 09a2cab4b5e9dae2f5543b4e5839100ad0618c5a Mon Sep 17 00:00:00 2001 From: Adekunle Adebayo Date: Fri, 22 Apr 2022 14:02:50 +0100 Subject: [PATCH 2/9] PR comment updateds completed --- examples/src/index.js | 12 ++++++------ src/index.js | 16 ++-------------- tests/hcaptcha.spec.js | 10 +++++++--- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/examples/src/index.js b/examples/src/index.js index 387a518..4d23b06 100644 --- a/examples/src/index.js +++ b/examples/src/index.js @@ -16,9 +16,9 @@ const AsyncDemo = () => { } }; - const getResponse = async () => { + const getResponse = () => { try { - const res = await captchaRef.current.getResponse(); + const res = captchaRef.current.getResponse(); console.log("Response: ", res); } catch (error) { @@ -26,9 +26,9 @@ const AsyncDemo = () => { } }; - const getRespKey = async () => { + const getRespKey = () => { try { - const res = await captchaRef.current.getRespKey(); + const res = captchaRef.current.getRespKey(); console.log("Response Key: ", res); } catch (error) { @@ -60,8 +60,8 @@ const AsyncDemo = () => { onChalExpired={handleChallengeExpired} /> - - + + ); } diff --git a/src/index.js b/src/index.js index b931e5f..5137c35 100644 --- a/src/index.js +++ b/src/index.js @@ -280,23 +280,11 @@ class HCaptcha extends React.Component { } getResponse() { - const { captchaId } = this.state; - - if (!this.isReady()) { - return; - } - //Get response token from hCaptcha widget - return hcaptcha.getResponse(captchaId); + return hcaptcha.getResponse(this.state.captchaId); } getRespKey() { - const { captchaId } = this.state; - - if (!this.isReady()) { - return; - } - //Get current challenge session id from hCaptcha widget - return hcaptcha.getRespKey(captchaId) + return hcaptcha.getRespKey(this.state.captchaId) } render () { diff --git a/tests/hcaptcha.spec.js b/tests/hcaptcha.spec.js index 346af7c..b32750f 100644 --- a/tests/hcaptcha.spec.js +++ b/tests/hcaptcha.spec.js @@ -128,23 +128,27 @@ describe("hCaptcha", () => { it("can get Response", () => { expect(window.hcaptcha.getResponse.mock.calls.length).toBe(0); - instance.getResponse(); + const res = instance.getResponse(); expect(window.hcaptcha.getResponse.mock.calls.length).toBe(1); expect(window.hcaptcha.getResponse.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); + expect(res).toBe(MOCK_TOKEN); }); it("can get RespKey", () => { expect(window.hcaptcha.getRespKey.mock.calls.length).toBe(0); - instance.getRespKey(); + const res = instance.getRespKey(); expect(window.hcaptcha.getRespKey.mock.calls.length).toBe(1); expect(window.hcaptcha.getRespKey.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); + expect(res).toBe(MOCK_EKEY); }); it("can set Data", () => { expect(window.hcaptcha.setData.mock.calls.length).toBe(0); - instance.setData(); + const dataObj = { data: { nested: 1 } }; + instance.setData(dataObj); expect(window.hcaptcha.setData.mock.calls.length).toBe(1); expect(window.hcaptcha.setData.mock.calls[0][0]).toBe(MOCK_WIDGET_ID); + expect(window.hcaptcha.setData.mock.calls[0][1]).toBe(dataObj); }); it("emits onLoad event", () => { From 083c4b429bcedb32733d9bb116fb3c0ca6bfb0d0 Mon Sep 17 00:00:00 2001 From: Adekunle Adebayo Date: Fri, 22 Apr 2022 14:18:15 +0100 Subject: [PATCH 3/9] updated data to take object type --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index c1ad2dc..1a1a83c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -39,7 +39,7 @@ declare class HCaptcha extends React.Component { resetCaptcha(): void; renderCaptcha(): void; removeCaptcha(): void; - setData(): void; + setData(): Object; getRespKey(): string; getResponse(): string; execute(opts: { async: true }): Promise; From 1a82c6f2794a31345fde6ad273c38989202c1edb Mon Sep 17 00:00:00 2001 From: Adekunle Adebayo Date: Fri, 22 Apr 2022 14:20:58 +0100 Subject: [PATCH 4/9] Bump to new version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ac0f5b9..ae25ab5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hcaptcha/react-hcaptcha", - "version": "1.1.1", + "version": "1.2.0", "types": "types/index.d.ts", "main": "dist/index.js", "files": [ From 0107edbc01dc916d77583cbed7f133a44689cc56 Mon Sep 17 00:00:00 2001 From: Adekunle Adebayo Date: Fri, 22 Apr 2022 14:49:44 +0100 Subject: [PATCH 5/9] updated readme file with exposed methods --- README.md | 3 +++ src/index.js | 8 ++++---- types/index.d.ts | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 49693e3..81ab6ff 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,10 @@ return { resetCaptcha(): void; renderCaptcha(): void; removeCaptcha(): void; - setData(): Object; getRespKey(): string; getResponse(): string; + setData(data: object): void; execute(opts: { async: true }): Promise; execute(opts?: { async: false }): void; execute(opts?: { async: boolean }): Promise | void; From 4144cc9f87c245ca9f79acff5b601a8cf7228088 Mon Sep 17 00:00:00 2001 From: Adekunle Adebayo Date: Fri, 22 Apr 2022 14:54:14 +0100 Subject: [PATCH 6/9] typo fix in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81ab6ff..42e324c 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ return Date: Fri, 22 Apr 2022 17:01:54 +0100 Subject: [PATCH 7/9] updated readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42e324c..069b0d5 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ return Date: Mon, 25 Apr 2022 12:13:47 +0100 Subject: [PATCH 8/9] update readme add see enterprise docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 069b0d5..66462f3 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ return Date: Mon, 25 Apr 2022 12:16:45 +0100 Subject: [PATCH 9/9] readme typo change --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 66462f3..64683aa 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ return