generate-api-route-with-tests.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Generate API Route with Tests ## Command Prompt Generate a new Next.js API route handler with the following specifications: **Route**: `/api/[resource]/route.ts` **Requirements**:1. Create the route handler with GET, POST, PUT, and DELETE methods2. Use TypeScript with strict types3. Include Zod schema validation for request bodies4. Add proper error handling with try-catch blocks5. Return appropriate HTTP status codes6. Include JSDoc comments for each method7. Create corresponding test file using Vitest8. Mock Supabase client for tests9. Test all CRUD operations with success and error cases10. Include request/response type definitions **Example Structure**: ```typescriptimport { NextRequest, NextResponse } from "next/server";import { z } from "zod";import { createClient } from "@/lib/supabase/server"; const schema = z.object({ name: z.string(), email: z.string().email(),}); export async function GET() { // Implementation} export async function POST(request: NextRequest) { // Implementation with validation}``` **Test Coverage**:- Test successful GET requests- Test POST with valid/invalid data- Test error handling- Test database connection failures- Test authentication requirements Generate complete, production-ready code with all imports and proper error messages.
/generate-api-route-with-testsMarkdown