| name | scaffold-api |
| description | Generate a new authenticated Next.js API route following RDO project conventions |
| argument-hint | <route-path> (e.g. companies/[id]/notes) |
Create a new Next.js API route at src/pages/api/$ARGUMENTS.ts following these project conventions exactly:
Required Structure
- Swagger JSDoc comment block at the top of the file:
- Imports — always use these exact patterns:
import type { NextApiResponse } from 'next';
import { requireAuth, AuthenticatedRequest } from '@infrastructure/auth/middleware';
import pool from '@infrastructure/database/connection';
- Handler function with method check and userId extraction:
async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
const userId = req.userId!;
try {
const { rows } = await pool.query('SELECT ...', [userId]);
return res.status(200).json(rows);
} catch (error) {
console.error('[API_NAME] Error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
}
- Default export with auth wrapper:
export default requireAuth(handler);
Rules
- Use
requireAdmin instead of requireAuth if the route is admin-only
- Use
AuthenticatedRequest type, never NextApiRequest
- Access user via
req.userId! (non-null assertion)
- Use
pool.query() for database access — no ORM
- Never use
do as a SQL alias (PostgreSQL reserved word) — use d instead
- Parse numeric scores with
parseFloat(String(value)).toFixed(1)
- Define TypeScript interfaces for response shapes at the top of the file
- Add proper query parameter validation before database queries