InstructionsForAI
generate-nextjs-api-route-tests.txt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Generate Next.js API Route with Tests Create a new API route in Next.js 15 App Router with the following requirements: ## Requirements ### 1. File Structure- Create the API route in `app/api/[endpoint]/route.ts`- Include TypeScript types in `types/api.ts`- Create tests in `__tests__/api/[endpoint].test.ts` ### 2. Implementation Details **Route Handler:**- Implement GET, POST, PUT, DELETE methods as needed- Use Zod for request validation- Include proper error handling with appropriate HTTP status codes- Add request logging- Implement rate limiting (optional)- Use TypeScript with strict types **Validation:**- Define Zod schemas for request body validation- Validate query parameters- Return detailed error messages for validation failures **Error Handling:**- Custom error classes for different error types- Consistent error response format- Proper HTTP status codes (400, 401, 403, 404, 500)- Error logging for debugging **Response Format:**```typescript// Success response{ success: true, data: T} // Error response{ success: false, error: { code: string, message: string, details?: unknown }}``` ### 3. Testing **Test Coverage:**- Unit tests for each HTTP method- Validation error tests- Success case tests- Error case tests- Edge case tests **Test Framework:**- Use Jest with TypeScript- Mock database calls- Mock external API calls- Test rate limiting- Test authentication/authorization ## Example Request ```Endpoint: /api/usersMethods: GET, POST, PUT, DELETEDatabase: Supabase PostgreSQLAuthentication: Required for POST, PUT, DELETE``` ## Deliverables 1. **API Route File** (`app/api/users/route.ts`) - Complete implementation with all methods - Full TypeScript types - Comprehensive error handling 2. **Type Definitions** (`types/api.ts`) - Request/response types - Error types - Database model types 3. **Test File** (`__tests__/api/users.test.ts`) - All test cases - Mocks and fixtures - 80%+ code coverage 4. **Documentation** - API endpoint documentation - Request/response examples - Error codes and meanings ## Code Style - Use async/await for asynchronous operations- Follow Next.js 15 conventions- Use descriptive variable and function names- Add JSDoc comments for complex logic- Keep functions small and focused- Use early returns to reduce nesting Generate complete, production-ready code that I can copy and paste directly into my project.
MarkdownMarkdown