Skip to content

Commit 8753a48

Browse files
committed
feat: Add Swagger documentation and API specification generation
- Integrated @fastify/swagger and @fastify/swagger-ui for API documentation. - Added new npm scripts for generating API specifications in JSON and YAML formats. - Updated logout route with OpenAPI documentation schema. - Created a script to start the server and fetch the OpenAPI spec. - Added API documentation markdown file detailing usage and commands. - Updated package.json to include new dependencies and scripts. - Removed favicon registration from plugin index and moved it to server.ts.
1 parent 7aca85a commit 8753a48

File tree

10 files changed

+1586
-43
lines changed

10 files changed

+1586
-43
lines changed

package-lock.json

Lines changed: 312 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# API Documentation Generation
2+
3+
This document explains how to generate and use the OpenAPI specification for the DeployStack Backend API.
4+
5+
## Overview
6+
7+
The DeployStack Backend uses Fastify with Swagger plugins to automatically generate OpenAPI 3.0 specifications from route definitions. This provides:
8+
9+
- **Interactive Documentation**: Swagger UI interface for testing APIs
10+
- **Postman Integration**: JSON/YAML specs that can be imported into Postman
11+
- **Automated Generation**: Specifications are generated from actual route code
12+
13+
## Available Commands
14+
15+
### 1. Generate Complete API Specification
16+
```bash
17+
npm run api:spec
18+
```
19+
This command:
20+
- Starts a temporary server
21+
- Generates both JSON and YAML specifications
22+
- Saves files to `api-spec.json` and `api-spec.yaml`
23+
- Provides URLs for interactive documentation
24+
- Automatically shuts down the server
25+
26+
**Output:**
27+
- `api-spec.json` - OpenAPI JSON specification (for Postman import)
28+
- `api-spec.yaml` - OpenAPI YAML specification
29+
30+
### 2. Generate JSON Specification (requires running server)
31+
```bash
32+
npm run api:spec:json
33+
```
34+
Requires the development server to be running (`npm run dev`).
35+
36+
### 3. Generate YAML Specification (requires running server)
37+
```bash
38+
npm run api:spec:yaml
39+
```
40+
Requires the development server to be running (`npm run dev`).
41+
42+
## Usage Examples
43+
44+
### Complete Generation (Recommended)
45+
```bash
46+
cd services/backend
47+
npm run api:spec
48+
```
49+
50+
### Manual Generation with Running Server
51+
```bash
52+
# Terminal 1: Start the server
53+
cd services/backend
54+
npm run dev
55+
56+
# Terminal 2: Generate specifications
57+
npm run api:spec:json
58+
npm run api:spec:yaml
59+
```
60+
61+
## Accessing Documentation
62+
63+
When the server is running (`npm run dev`), you can access:
64+
65+
- **Interactive Docs**: http://localhost:3000/documentation
66+
- **JSON Spec**: http://localhost:3000/documentation/json
67+
- **YAML Spec**: http://localhost:3000/documentation/yaml
68+
69+
## Importing into Postman
70+
71+
1. Run `npm run api:spec` to generate the specification
72+
2. Open Postman
73+
3. Click "Import"
74+
4. Select the generated `api-spec.json` file
75+
5. All API endpoints will be imported with proper documentation
76+
77+
## Adding Documentation to Routes
78+
79+
To add OpenAPI documentation to your routes, include a schema object:
80+
81+
```typescript
82+
const routeSchema = {
83+
tags: ['Category'],
84+
summary: 'Brief description',
85+
description: 'Detailed description of what this endpoint does',
86+
security: [{ cookieAuth: [] }], // If authentication required
87+
response: {
88+
200: {
89+
type: 'object',
90+
properties: {
91+
success: { type: 'boolean' },
92+
message: { type: 'string' }
93+
},
94+
required: ['success', 'message']
95+
}
96+
}
97+
};
98+
99+
fastify.post('/your-route', { schema: routeSchema }, async (request, reply) => {
100+
// Your route handler
101+
});
102+
```
103+
104+
## Example: Logout Route Documentation
105+
106+
The logout route (`/api/auth/logout`) demonstrates proper documentation:
107+
108+
```typescript
109+
const logoutSchema = {
110+
tags: ['Authentication'],
111+
summary: 'User logout',
112+
description: 'Invalidates the current user session and clears authentication cookies',
113+
security: [{ cookieAuth: [] }],
114+
response: {
115+
200: {
116+
type: 'object',
117+
properties: {
118+
success: {
119+
type: 'boolean',
120+
description: 'Indicates if the logout operation was successful'
121+
},
122+
message: {
123+
type: 'string',
124+
description: 'Human-readable message about the logout result'
125+
}
126+
},
127+
required: ['success', 'message'],
128+
examples: [
129+
{
130+
success: true,
131+
message: 'Logged out successfully.'
132+
}
133+
]
134+
}
135+
}
136+
};
137+
```
138+
139+
## Configuration
140+
141+
The Swagger configuration is in `src/server.ts`:
142+
143+
```typescript
144+
await server.register(fastifySwagger, {
145+
openapi: {
146+
openapi: '3.0.0',
147+
info: {
148+
title: 'DeployStack Backend API',
149+
description: 'API documentation for DeployStack Backend',
150+
version: '0.20.5'
151+
},
152+
servers: [
153+
{
154+
url: 'http://localhost:3000',
155+
description: 'Development server'
156+
}
157+
],
158+
components: {
159+
securitySchemes: {
160+
cookieAuth: {
161+
type: 'apiKey',
162+
in: 'cookie',
163+
name: 'auth_session'
164+
}
165+
}
166+
}
167+
}
168+
});
169+
```
170+
171+
## Troubleshooting
172+
173+
### "Route already declared" Error
174+
This happens when trying to manually add routes that Swagger UI already provides. The `/documentation/json` and `/documentation/yaml` endpoints are automatically created.
175+
176+
### "Failed to fetch API spec" Error
177+
Ensure the server is fully started before trying to fetch the specification. The generation script includes a 2-second delay to allow for complete initialization.
178+
179+
### Missing Route Documentation
180+
Routes without schema definitions will appear in the specification but with minimal documentation. Add schema objects to routes for complete documentation.
181+
182+
## Next Steps
183+
184+
To extend API documentation:
185+
186+
1. Add schema definitions to more routes
187+
2. Define reusable components in the OpenAPI configuration
188+
3. Add request body schemas for POST/PUT endpoints
189+
4. Include error response schemas (400, 401, 500, etc.)
190+
5. Add parameter validation schemas
191+
192+
## Files Generated
193+
194+
- `api-spec.json` - Complete OpenAPI 3.0 specification in JSON format
195+
- `api-spec.yaml` - Complete OpenAPI 3.0 specification in YAML format
196+
- Interactive documentation available at `/documentation` when server is running

0 commit comments

Comments
 (0)