Automated API Documentation System
Ever Works includes an automated OpenAPI documentation system that generates comprehensive API docs from your code.
Overviewβ
The system provides:
- π Automated generation - From code annotations to OpenAPI spec
- π Hybrid approach - Preserves manual docs, adds automated ones
- π― Type-safe - TypeScript integration
- π Swagger UI - Interactive API explorer
- π§ Hot reload - Auto-regenerates during development
Architectureβ
Hybrid Approachβ
- β
Preserves existing
public/openapi.jsonfile - β
Adds
@swaggerannotations in route code - β Merges both sources automatically
- β Generates complete and consistent OpenAPI file
Installationβ
1. Install Dependenciesβ
# Run the installation script
./scripts/install-swagger-deps.sh
# Or manually with npm
npm install -D swagger-jsdoc @types/swagger-jsdoc tsx nodemon
2. Available Scriptsβ
# Generate documentation once
npm run generate-docs
# Watch mode for development (auto-regenerates)
npm run docs:watch
# Development with automatic generation
npm run dev
Usageβ
Adding Annotations to Routesβ
// app/api/example/route.ts
import { NextRequest, NextResponse } from 'next/server';
/**
* @swagger
* /api/example:
* get:
* tags: ["Example"]
* summary: "Get example data"
* description: "Returns example data from the API"
* responses:
* 200:
* description: "Success"
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: string
*/
export async function GET() {
return NextResponse.json({ success: true, data: ["example"] });
}
Using Annotation Utilitiesβ
import { createAdminRouteAnnotation, CommonAnnotations } from '@/lib/swagger/annotations';
/**
* @swagger
* /api/admin/users:
* get:
* tags: ["Admin"]
* summary: "Get all users"
* security:
* - bearerAuth: []
* responses:
* 200:
* description: "Success"
* 401:
* $ref: '#/components/responses/Unauthorized'
* 500:
* $ref: '#/components/responses/ServerError'
*/
export async function GET() {
// Implementation
}
Common Annotationsβ
The system provides reusable annotation components:
// lib/swagger/annotations.ts
export const CommonAnnotations = {
responses: {
unauthorized: {
description: "Unauthorized - Invalid or missing authentication",
content: {
"application/json": {
schema: {
type: "object",
properties: {
error: { type: "string", example: "Unauthorized" }
}
}
}
}
},
serverError: {
description: "Internal Server Error",
content: {
"application/json": {
schema: {
type: "object",
properties: {
error: { type: "string", example: "Internal server error" }
}
}
}
}
}
},
security: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT"
}
}
};
File Structureβ
scripts/
βββ generate-openapi.ts # Main generation script
βββ tsconfig.json # TypeScript config for scripts
βββ install-swagger-deps.sh # Dependencies installer
lib/swagger/
βββ annotations.ts # Reusable annotation utilities
templates/
βββ route-template.ts # Template for new routes
public/
βββ openapi.json # Generated OpenAPI specification
Configurationβ
OpenAPI Base Configurationβ
// scripts/generate-openapi.ts
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'Ever Works API',
version: '1.0.0',
description: 'API documentation for Ever Works directory platform',
},
servers: [
{
url: 'http://localhost:3000',
description: 'Development server',
},
{
url: 'https://yourdomain.com',
description: 'Production server',
},
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
};
Swagger UI Configurationβ
Access the interactive API documentation at:
- Development:
http://localhost:3000/api-docs - Production:
https://yourdomain.com/api-docs
Best Practicesβ
1. Consistent Taggingβ
Group related endpoints with tags:
/**
* @swagger
* /api/items:
* get:
* tags: ["Items"] // Use consistent tag names
*/
2. Detailed Descriptionsβ
Provide clear descriptions and examples:
/**
* @swagger
* /api/items/{id}:
* get:
* summary: "Get item by ID"
* description: "Retrieves a single item from the directory by its unique identifier"
* parameters:
* - name: id
* in: path
* required: true
* description: "Unique item identifier"
* schema:
* type: string
* example: "item-123"
*/
3. Schema Definitionsβ
Define reusable schemas in components:
/**
* @swagger
* components:
* schemas:
* Item:
* type: object
* required:
* - id
* - name
* properties:
* id:
* type: string
* example: "item-123"
* name:
* type: string
* example: "Example Item"
* description:
* type: string
* example: "Item description"
*/
4. Error Responsesβ
Document all possible error responses:
/**
* @swagger
* /api/items:
* post:
* responses:
* 201:
* description: "Item created successfully"
* 400:
* description: "Invalid request data"
* 401:
* description: "Unauthorized"
* 500:
* description: "Server error"
*/
Troubleshootingβ
Documentation Not Generatingβ
Issue: OpenAPI file not updating
Solution: Check the generation script
# Run manually to see errors
npm run generate-docs
# Check for syntax errors in annotations
Swagger UI Not Loadingβ
Issue: API docs page shows error
Solution: Verify OpenAPI file is valid
# Validate OpenAPI spec
npx swagger-cli validate public/openapi.json
Annotations Not Detectedβ
Issue: Route annotations not appearing in docs
Solution: Ensure correct format
// β
Correct
/**
* @swagger
* /api/route:
* get:
* ...
*/
// β Incorrect (missing @swagger tag)
/**
* /api/route:
* get:
* ...
*/
Advanced Featuresβ
Request Body Schemasβ
/**
* @swagger
* /api/items:
* post:
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* properties:
* name:
* type: string
* description:
* type: string
*/
Authenticationβ
/**
* @swagger
* /api/admin/settings:
* get:
* security:
* - bearerAuth: []
* responses:
* 401:
* description: "Unauthorized"
*/
Query Parametersβ
/**
* @swagger
* /api/items:
* get:
* parameters:
* - name: page
* in: query
* schema:
* type: integer
* default: 1
* - name: limit
* in: query
* schema:
* type: integer
* default: 10
*/
Next Stepsβ
- Testing - Test your API endpoints
- Local Setup - Set up development environment
- Deployment - Deploy your API