diff --git a/README.md b/README.md index 49693e3..64683aa 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,10 @@ return { } }; + const getResponse = () => { + try { + const res = captchaRef.current.getResponse(); + console.log("Response: ", res); + + } catch (error) { + console.log(error); + } + }; + + const getRespKey = () => { + try { + const res = 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/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": [ diff --git a/src/index.js b/src/index.js index ac33bd1..46529ac 100644 --- a/src/index.js +++ b/src/index.js @@ -265,6 +265,28 @@ class HCaptcha extends React.Component { return hcaptcha.execute(captchaId, opts); } + setData (data) { + const { captchaId } = this.state; + + if (!this.isReady()) { + return; + } + + if (data && typeof data !== "object") { + data = null; + } + + hcaptcha.setData(captchaId, data); + } + + getResponse() { + return hcaptcha.getResponse(this.state.captchaId); + } + + getRespKey() { + return hcaptcha.getRespKey(this.state.captchaId) + } + render () { const { elementId } = this.state; return
diff --git a/tests/hcaptcha.spec.js b/tests/hcaptcha.spec.js index 0b3b8ef..b32750f 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,31 @@ 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); + 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); + 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); + 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", () => { expect(mockFns.onLoad.mock.calls.length).toBe(0); instance.handleOnLoad(); diff --git a/types/index.d.ts b/types/index.d.ts index 730a099..f8fafb5 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; + getRespKey(): string; + getResponse(): string; + setData(data: object): void; execute(opts: { async: true }): Promise; execute(opts?: { async: false }): void; execute(opts?: { async: boolean }): Promise | void;