Refactor action and route handlers to use async schema validation (#361)
- Removed the `zodParseFactory` utility and replaced it with direct async schema validation using `safeParseAsync` in both `enhanceAction` and `enhanceRouteHandler`. - Improved error handling to return meaningful messages for invalid request bodies. - Streamlined the codebase by deleting the now-unnecessary `utils/index.ts` file.
This commit is contained in:
committed by
GitHub
parent
2b8572baaa
commit
3c13b5ec1e
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "next-supabase-saas-kit-turbo",
|
||||
"version": "2.15.0",
|
||||
"version": "2.15.1",
|
||||
"private": true,
|
||||
"sideEffects": false,
|
||||
"engines": {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Schema> {
|
||||
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({
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const zodParseFactory =
|
||||
<T extends z.ZodTypeAny>(schema: T) =>
|
||||
(data: unknown): z.infer<T> => {
|
||||
try {
|
||||
return schema.parse(data) as unknown;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
// handle error
|
||||
throw new Error(`Invalid data: ${err as string}`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user