next-recaptcha-v3 2.0.0-beta.1 → 2.0.0-beta.2

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/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Roman Zhuravlov
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Roman Zhuravlov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,22 +1,30 @@
1
1
  import type { ReCaptchaContextProps } from "./ReCaptchaProvider.js";
2
2
  import type { IReCaptcha } from "./recaptcha.types.js";
3
+ export type ExecuteRecaptchaResult = readonly [token: string, error: null] | readonly [token: null, error: Error];
3
4
  export interface useReCaptchaProps extends ReCaptchaContextProps {
4
5
  /** reCAPTCHA instance */
5
6
  grecaptcha: IReCaptcha | null;
6
7
  /**
7
8
  * Executes the reCAPTCHA verification process for a given action.
8
9
  * Actions may only contain alphanumeric characters and slashes, and must not be user-specific.
10
+ * @throws Will throw an error if the reCAPTCHA site key is not defined or if reCAPTCHA has not been loaded.
9
11
  */
10
12
  executeRecaptcha: (action: string) => Promise<string>;
13
+ /**
14
+ * Safely executes the reCAPTCHA verification process for a given action.
15
+ * Returns a tuple containing the token or an error.
16
+ * @returns A tuple where the first element is the token (or null if there was an error) and the second element is an Error object (or null if successful).
17
+ */
18
+ executeRecaptchaSafe: (action: string) => Promise<ExecuteRecaptchaResult>;
11
19
  }
12
20
  /**
13
21
  * Custom hook to use Google reCAPTCHA v3.
14
22
  *
15
23
  * @param [reCaptchaKey] - Optional reCAPTCHA site key. If not provided, it will use the key from the context.
16
- * @returns An object containing the reCAPTCHA context, grecaptcha instance, reCaptchaKey, and executeRecaptcha function.
24
+ * @returns An object containing the reCAPTCHA context, grecaptcha instance, reCaptchaKey, executeRecaptcha, and executeRecaptchaSafe functions.
17
25
  *
18
26
  * @example
19
- * const { executeRecaptcha } = useReCaptcha();
27
+ * const { executeRecaptcha, executeRecaptchaSafe } = useReCaptcha();
20
28
  *
21
29
  * const handleSubmit = async () => {
22
30
  * try {
@@ -7,10 +7,10 @@ import { useGrecaptcha, getGrecaptcha } from './utils.js';
7
7
  * Custom hook to use Google reCAPTCHA v3.
8
8
  *
9
9
  * @param [reCaptchaKey] - Optional reCAPTCHA site key. If not provided, it will use the key from the context.
10
- * @returns An object containing the reCAPTCHA context, grecaptcha instance, reCaptchaKey, and executeRecaptcha function.
10
+ * @returns An object containing the reCAPTCHA context, grecaptcha instance, reCaptchaKey, executeRecaptcha, and executeRecaptchaSafe functions.
11
11
  *
12
12
  * @example
13
- * const { executeRecaptcha } = useReCaptcha();
13
+ * const { executeRecaptcha, executeRecaptchaSafe } = useReCaptcha();
14
14
  *
15
15
  * const handleSubmit = async () => {
16
16
  * try {
@@ -42,11 +42,34 @@ const useReCaptcha = (reCaptchaKey) => {
42
42
  const result = await grecaptcha.execute(siteKey, { action });
43
43
  return result;
44
44
  }, [useEnterprise, siteKey]);
45
+ const executeRecaptchaSafe = useCallback(async (action) => {
46
+ try {
47
+ if (!siteKey) {
48
+ return [null, new Error("ReCaptcha sitekey is not defined")];
49
+ }
50
+ const grecaptcha = getGrecaptcha(useEnterprise);
51
+ if (typeof grecaptcha?.execute !== "function") {
52
+ return [null, new Error("Recaptcha has not been loaded")];
53
+ }
54
+ if (typeof grecaptcha.ready === "function") {
55
+ await new Promise((resolve) => {
56
+ grecaptcha.ready(resolve);
57
+ });
58
+ }
59
+ const result = await grecaptcha.execute(siteKey, { action });
60
+ return [result, null];
61
+ }
62
+ catch (err) {
63
+ const error = err instanceof Error ? err : new Error(String(err));
64
+ return [null, error];
65
+ }
66
+ }, [useEnterprise, siteKey]);
45
67
  return {
46
68
  ...context,
47
69
  grecaptcha,
48
70
  reCaptchaKey: siteKey,
49
71
  executeRecaptcha,
72
+ executeRecaptchaSafe,
50
73
  };
51
74
  };
52
75
 
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "next-recaptcha-v3",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "🤖 Next.js hook to add Google ReCaptcha to your application",
5
5
  "license": "MIT",
6
6
  "author": "Roman Zhuravlov",
7
- "repository": "https://github.com/snelsi/next-recaptcha-v3",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/snelsi/next-recaptcha-v3.git"
10
+ },
8
11
  "homepage": "https://github.com/snelsi/next-recaptcha-v3",
9
12
  "keywords": [
10
13
  "recaptcha",
@@ -31,38 +34,37 @@
31
34
  "prepublishOnly": "npm run rollup"
32
35
  },
33
36
  "peerDependencies": {
34
- "next": "^13 || ^14 || ^15",
37
+ "next": "^13 || ^14 || ^15 || ^16",
35
38
  "react": "^18 || ^19"
36
39
  },
37
40
  "engines": {
38
41
  "node": ">=18"
39
42
  },
40
43
  "devDependencies": {
41
- "@eslint/eslintrc": "^3.2.0",
42
- "@eslint/js": "^9.20.0",
43
- "@next/eslint-plugin-next": "^15.1.7",
44
- "@rollup/plugin-node-resolve": "^16.0.0",
45
- "@rollup/plugin-typescript": "^12.1.2",
46
- "@types/node": "^22.13.4",
47
- "@types/react": "^19.0.10",
48
- "@types/react-dom": "^19.0.4",
49
- "eslint": "^9.20.1",
50
- "eslint-config-next": "^15.1.7",
51
- "eslint-config-prettier": "^10.0.1",
52
- "eslint-plugin-react": "^7.37.4",
53
- "eslint-plugin-react-hooks": "^5.1.0",
44
+ "@eslint/js": "^9.39.2",
45
+ "@next/eslint-plugin-next": "^16.1.3",
46
+ "@rollup/plugin-node-resolve": "^16.0.3",
47
+ "@rollup/plugin-typescript": "^12.3.0",
48
+ "@types/node": "^25.0.9",
49
+ "@types/react": "^19.2.8",
50
+ "@types/react-dom": "^19.2.3",
51
+ "eslint": "^9.39.2",
52
+ "eslint-config-next": "^16.1.3",
53
+ "eslint-config-prettier": "^10.1.8",
54
+ "eslint-plugin-react": "^7.37.5",
55
+ "eslint-plugin-react-hooks": "^7.0.1",
54
56
  "husky": "^9.1.7",
55
- "lint-staged": "^15.4.3",
56
- "next": "^15.1.7",
57
- "prettier": "3.5.1",
58
- "pretty-quick": "^4.0.0",
59
- "react": "^19.0.0",
60
- "react-dom": "^19.0.0",
61
- "rollup": "^4.34.8",
62
- "rollup-plugin-node-externals": "^8.0.0",
57
+ "lint-staged": "^16.2.7",
58
+ "next": "^16.1.3",
59
+ "prettier": "3.8.0",
60
+ "pretty-quick": "^4.2.2",
61
+ "react": "^19.2.3",
62
+ "react-dom": "^19.2.3",
63
+ "rollup": "^4.55.1",
64
+ "rollup-plugin-node-externals": "^8.1.2",
63
65
  "rollup-plugin-preserve-directives": "^0.4.0",
64
- "typescript": "^5.7.3",
65
- "typescript-eslint": "^8.24.1"
66
+ "typescript": "^5.9.3",
67
+ "typescript-eslint": "^8.53.0"
66
68
  },
67
69
  "lint-staged": {
68
70
  "**/*.{js,jsx,ts,tsx}": [