Update documentation rules for various contexts and functionalities (#235)
Update cusor rules for various contexts and functionalities
This commit is contained in:
committed by
GitHub
parent
53b09fcb8e
commit
1030c84eee
@@ -9,20 +9,19 @@ alwaysApply: false
|
||||
- Always name the server actions file as "server-actions.ts"
|
||||
- Always name exported Server Actions suffixed as "Action", ex. "createPostAction"
|
||||
- Always use the `enhanceAction` function from the "@kit/supabase/actions" package [index.ts](mdc:packages/next/src/actions/index.ts)
|
||||
- Always use the 'use server' directive at the top of the file
|
||||
- Place the Zod schema in a separate file so it can be reused with `react-hook-form`
|
||||
|
||||
```tsx
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
|
||||
const ZodSchema = z.object({
|
||||
email: z.string().email(),
|
||||
password: z.string().min(6),
|
||||
});
|
||||
import { EntitySchema } from '../entity.schema.ts`;
|
||||
|
||||
export const myServerAction = enhanceAction(
|
||||
async function (data, user) {
|
||||
// 1. "data" is already a valid ZodSchema and it's safe to use
|
||||
// 1. "data" is already a valid EntitySchema and it's safe to use
|
||||
// 2. "user" is the authenticated user
|
||||
|
||||
// ... your code here
|
||||
@@ -32,30 +31,7 @@ export const myServerAction = enhanceAction(
|
||||
},
|
||||
{
|
||||
auth: true,
|
||||
schema: ZodSchema,
|
||||
schema: EntitySchema,
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
## Logging
|
||||
|
||||
Consider logging asynchronous requests using the `@kit/shared/logger` package in a structured way to provide context to the logs in both server actions and route handlers.
|
||||
|
||||
```tsx
|
||||
const ctx = {
|
||||
name: 'my-server-action', // use a meaningful name
|
||||
userId: user.id, // use the authenticated user's ID
|
||||
};
|
||||
|
||||
logger.info(ctx, 'Request started...');
|
||||
|
||||
const { data, error } = await supabase.from('notes').select('*');
|
||||
|
||||
if (error) {
|
||||
logger.error(ctx, 'Request failed...');
|
||||
// handle error
|
||||
} else {
|
||||
logger.info(ctx, 'Request succeeded...');
|
||||
// use data
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user