$npx -y skills add zakelfassi/skills-driven-development --skill api-endpointScaffold a REST API endpoint with route, controller, validation, and tests. Use when creating new API endpoints, adding resources, or when asked to build a new backend route.
| 1 | # API Endpoint Scaffold |
| 2 | |
| 3 | Create a new REST API endpoint following project conventions. |
| 4 | |
| 5 | ## Inputs |
| 6 | - Entity name (singular, e.g., `comment`) |
| 7 | - Fields (name:type pairs, e.g., `body:string, author_id:number`) |
| 8 | - Auth required? (boolean, defaults to `true`) |
| 9 | - Nested under? (optional parent entity, e.g., `post`) |
| 10 | |
| 11 | ## Steps |
| 12 | |
| 13 | 1. **Create the route file** |
| 14 | ``` |
| 15 | src/routes/{entity}.routes.ts |
| 16 | ``` |
| 17 | - Define CRUD routes: `GET /`, `GET /:id`, `POST /`, `PUT /:id`, `DELETE /:id` |
| 18 | - If nested: `GET /{parent}s/:parentId/{entity}s` |
| 19 | |
| 20 | 2. **Create the controller** |
| 21 | ``` |
| 22 | src/controllers/{entity}.controller.ts |
| 23 | ``` |
| 24 | - One method per route |
| 25 | - All responses use `{ data, error, meta }` shape |
| 26 | - Handle not-found with 404, validation with 422 |
| 27 | |
| 28 | 3. **Create the validation schema** |
| 29 | ``` |
| 30 | src/validators/{entity}.validator.ts |
| 31 | ``` |
| 32 | - Use Zod schemas matching the field definitions |
| 33 | - Separate schemas for create vs. update (update = partial) |
| 34 | |
| 35 | 4. **Create the test file** |
| 36 | ``` |
| 37 | tests/api/{entity}.test.ts |
| 38 | ``` |
| 39 | - Test each CRUD operation |
| 40 | - Test validation (bad input returns 422) |
| 41 | - Test auth (if required: 401 without token) |
| 42 | - Use test factories for fixtures |
| 43 | |
| 44 | 5. **Register the route** |
| 45 | ``` |
| 46 | src/routes/index.ts |
| 47 | ``` |
| 48 | - Add import + `app.use('/{entity}s', {entity}Routes)` |
| 49 | - If auth required: add auth middleware |
| 50 | |
| 51 | ## Conventions |
| 52 | - Route paths are plural (`/comments`, not `/comment`) |
| 53 | - File names are singular (`comment.routes.ts`) |
| 54 | - All endpoints return `{ data, error, meta }` |
| 55 | - `meta` includes pagination for list endpoints |
| 56 | - Auth middleware is applied at the route level, not controller level |
| 57 | |
| 58 | ## Edge Cases |
| 59 | - **File upload endpoints:** Add `multer` middleware, accept `multipart/form-data` |
| 60 | - **Nested routes:** Parent ID is validated in middleware (404 if parent doesn't exist) |
| 61 | - **Soft delete:** `DELETE` sets `deleted_at` timestamp, doesn't remove the row |