@onderwijsin/nuxt-turnstile 0.2.8 → 0.2.10

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/README.md CHANGED
@@ -138,7 +138,7 @@ after Turnstile succeeds; the module does not replace the route's existing submi
138
138
  ## Compatibility
139
139
 
140
140
  - Nuxt 4
141
- - Node.js 22+
141
+ - Node.js 24+; Node.js 22 may work but is untested and unsupported.
142
142
  - Node and Cloudflare Workers-compatible server runtime
143
143
  - No Sentry dependency or telemetry is included
144
144
 
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@onderwijsin/nuxt-turnstile",
3
3
  "configKey": "turnstile",
4
- "version": "0.2.8",
4
+ "version": "0.2.10",
5
5
  "compatibility": {
6
6
  "nuxt": "^4.0.0"
7
7
  },
package/dist/module.mjs CHANGED
@@ -3,7 +3,7 @@ import { defu } from 'defu';
3
3
  import { enabled, resolveModuleName, resolveLoggerScope, moduleSetup, validateModuleOptions, transpileRuntime, moduleDependenciesWhenEnabled } from '@onderwijsin/nuxt-module-utils/build';
4
4
  import { z } from 'zod';
5
5
 
6
- const version = "0.2.8";
6
+ const version = "0.2.10";
7
7
 
8
8
  const turnstileOptionsSchema = z.object({
9
9
  enabled,
@@ -57,9 +57,7 @@ const module$1 = defineNuxtModule({
57
57
  siteKey: options.siteKey
58
58
  }
59
59
  );
60
- if (!nuxt.options.turnstile) {
61
- nuxt.options.turnstile = {};
62
- }
60
+ nuxt.options.turnstile = defu(nuxt.options.turnstile, {});
63
61
  nuxt.options.turnstile.siteKey ??= options.siteKey;
64
62
  transpileRuntime(nuxt, runtimeDir);
65
63
  addImportsDir(resolver.resolve(runtimeDir, "app", "composables"));
@@ -1,5 +1,43 @@
1
1
  import type { H3Event } from "h3";
2
+ import { z } from "zod";
2
3
  import type { TurnstileErrorCode, TurnstileErrorData } from "../../types/errors.js";
4
+ export declare const turnstileValidationErrorCodeSchema: z.ZodEnum<{
5
+ "missing-input-secret": "missing-input-secret";
6
+ "invalid-input-secret": "invalid-input-secret";
7
+ "missing-input-response": "missing-input-response";
8
+ "invalid-input-response": "invalid-input-response";
9
+ "bad-request": "bad-request";
10
+ "timeout-or-duplicate": "timeout-or-duplicate";
11
+ "internal-error": "internal-error";
12
+ }>;
13
+ export declare const turnstileValidationResponseSchema: z.ZodObject<{
14
+ success: z.ZodBoolean;
15
+ hostname: z.ZodString;
16
+ "error-codes": z.ZodArray<z.ZodEnum<{
17
+ "missing-input-secret": "missing-input-secret";
18
+ "invalid-input-secret": "invalid-input-secret";
19
+ "missing-input-response": "missing-input-response";
20
+ "invalid-input-response": "invalid-input-response";
21
+ "bad-request": "bad-request";
22
+ "timeout-or-duplicate": "timeout-or-duplicate";
23
+ "internal-error": "internal-error";
24
+ }>>;
25
+ challenge_ts: z.ZodOptional<z.ZodString>;
26
+ action: z.ZodOptional<z.ZodString>;
27
+ cdata: z.ZodOptional<z.ZodString>;
28
+ metadata: z.ZodOptional<z.ZodObject<{
29
+ result_with_testing_key: z.ZodOptional<z.ZodBoolean>;
30
+ }, z.core.$strip>>;
31
+ }, z.core.$strip>;
32
+ export type TurnstileValidationResponse = z.infer<typeof turnstileValidationResponseSchema>;
33
+ /**
34
+ * Verifies a Turnstile token with the Cloudflare Turnstile API.
35
+ * @param token - The Turnstile token to verify.
36
+ * @param event - Optional H3 request event.
37
+ * @param signal - Optional AbortSignal for request cancellation.
38
+ * @returns The parsed Turnstile validation response.
39
+ */
40
+ export declare const verifyTurnstileToken: (token: string, event?: H3Event, signal?: AbortSignal) => Promise<TurnstileValidationResponse>;
3
41
  /**
4
42
  * Validates the Turnstile token from a protected request.
5
43
  * @param event - H3 request event.
@@ -1,6 +1,5 @@
1
1
  import { createError, getRequestHeader } from "h3";
2
2
  import { useRuntimeConfig } from "#imports";
3
- import { verifyTurnstileToken } from "@nuxtjs/turnstile/runtime/server/utils/verify.js";
4
3
  import { isAdmin } from "@onderwijsin/nuxt-module-utils/server";
5
4
  import { attempt, hasKey, isNumber, isRecord } from "@onderwijsin/nuxt-module-utils/shared";
6
5
  import { z } from "zod";
@@ -10,6 +9,38 @@ const turnstileVerificationSchema = z.object({
10
9
  action: z.string().optional(),
11
10
  metadata: z.object({ result_with_testing_key: z.boolean().optional() }).optional()
12
11
  });
12
+ export const turnstileValidationErrorCodeSchema = z.enum([
13
+ "missing-input-secret",
14
+ "invalid-input-secret",
15
+ "missing-input-response",
16
+ "invalid-input-response",
17
+ "bad-request",
18
+ "timeout-or-duplicate",
19
+ "internal-error"
20
+ ]);
21
+ export const turnstileValidationResponseSchema = z.object({
22
+ success: z.boolean(),
23
+ hostname: z.string(),
24
+ "error-codes": z.array(turnstileValidationErrorCodeSchema),
25
+ challenge_ts: z.string().optional(),
26
+ action: z.string().optional(),
27
+ cdata: z.string().optional(),
28
+ metadata: z.object({ result_with_testing_key: z.boolean().optional() }).optional()
29
+ });
30
+ const endpoint = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
31
+ export const verifyTurnstileToken = async (token, event, signal) => {
32
+ const secretKey = useRuntimeConfig(event).turnstile.secretKey;
33
+ const response = await $fetch(endpoint, {
34
+ method: "POST",
35
+ body: `secret=${encodeURIComponent(secretKey)}&response=${encodeURIComponent(token)}`,
36
+ headers: {
37
+ "content-type": "application/x-www-form-urlencoded"
38
+ },
39
+ signal
40
+ });
41
+ const data = turnstileValidationResponseSchema.parse(response);
42
+ return data;
43
+ };
13
44
  export async function assertTurnstileToken(event, expectedAction) {
14
45
  const config = useRuntimeConfig(event);
15
46
  const turnstileConfig = config.turnstile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onderwijsin/nuxt-turnstile",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "Nuxt Turnstile integration with client helpers and server-side validation.",
5
5
  "homepage": "https://github.com/onderwijsin/nuxt-modules#readme",
6
6
  "bugs": {
@@ -43,7 +43,7 @@
43
43
  "defu": "6.1.7",
44
44
  "h3": "1.15.11",
45
45
  "zod": "4.4.3",
46
- "@onderwijsin/nuxt-module-utils": "^0.5.0"
46
+ "@onderwijsin/nuxt-module-utils": "^0.5.1"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@nuxt/module-builder": "1.0.3",
@@ -53,7 +53,7 @@
53
53
  "unbuild": "3.6.1"
54
54
  },
55
55
  "engines": {
56
- "node": ">=22"
56
+ "node": ">=24"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "nuxt-module-build build",