diff --git a/package.json b/package.json index 9e6e17629..356d5d560 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-supabase-saas-kit-turbo", - "version": "2.15.0", + "version": "2.15.1", "private": true, "sideEffects": false, "engines": { diff --git a/packages/next/src/actions/index.ts b/packages/next/src/actions/index.ts index afd837460..7105b83bb 100644 --- a/packages/next/src/actions/index.ts +++ b/packages/next/src/actions/index.ts @@ -9,8 +9,6 @@ import { requireUser } from '@kit/supabase/require-user'; import { getSupabaseServerClient } from '@kit/supabase/server-client'; import { JWTUserData } from '@kit/supabase/types'; -import { zodParseFactory } from '../utils'; - /** * @name enhanceAction * @description Enhance an action with captcha, schema and auth checks @@ -42,9 +40,21 @@ export function enhanceAction< let user: UserParam = undefined as UserParam; // validate the schema passed in the config if it exists - const data = config.schema - ? zodParseFactory(config.schema)(params) - : params; + const validateData = async () => { + if (config.schema) { + const parsed = await config.schema.safeParseAsync(params); + + if (parsed.success) { + return parsed.data; + } + + throw new Error(parsed.error.message || 'Invalid request body'); + } + + return params; + }; + + const data = await validateData(); // by default, the CAPTCHA token is not required const verifyCaptcha = config.captcha ?? false; diff --git a/packages/next/src/routes/index.ts b/packages/next/src/routes/index.ts index 853808446..0ad30f17d 100644 --- a/packages/next/src/routes/index.ts +++ b/packages/next/src/routes/index.ts @@ -10,8 +10,6 @@ import { requireUser } from '@kit/supabase/require-user'; import { getSupabaseServerClient } from '@kit/supabase/server-client'; import { JWTUserData } from '@kit/supabase/types'; -import { zodParseFactory } from '../utils'; - interface Config { auth?: boolean; captcha?: boolean; @@ -117,8 +115,16 @@ export const enhanceRouteHandler = < // clone the request to read the body // so that we can pass it to the handler safely const json = await request.clone().json(); + const parsedBody = await params.schema.safeParseAsync(json); - body = zodParseFactory(params.schema)(json); + if (parsedBody.success) { + body = parsedBody.data; + } else { + return NextResponse.json( + { error: parsedBody.error.message || 'Invalid request body' }, + { status: 400 }, + ); + } } return handler({ diff --git a/packages/next/src/utils/index.ts b/packages/next/src/utils/index.ts deleted file mode 100644 index 6eb64adea..000000000 --- a/packages/next/src/utils/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { z } from 'zod'; - -export const zodParseFactory = - (schema: T) => - (data: unknown): z.infer => { - try { - return schema.parse(data) as unknown; - } catch (err) { - console.error(err); - - // handle error - throw new Error(`Invalid data: ${err as string}`); - } - };