nextjs-auto-swagger-gen 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +351 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +1564 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/index.mjs +1552 -0
- package/dist/cli/index.mjs.map +1 -0
- package/dist/index-2JCtJeEw.d.mts +241 -0
- package/dist/index-2JCtJeEw.d.ts +241 -0
- package/dist/index.d.mts +193 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.js +1977 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1941 -0
- package/dist/index.mjs.map +1 -0
- package/dist/register.d.mts +32 -0
- package/dist/register.d.ts +32 -0
- package/dist/register.js +1417 -0
- package/dist/register.js.map +1 -0
- package/dist/register.mjs +1398 -0
- package/dist/register.mjs.map +1 -0
- package/dist/ui/index.d.mts +36 -0
- package/dist/ui/index.d.ts +36 -0
- package/dist/ui/index.js +390 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/index.mjs +360 -0
- package/dist/ui/index.mjs.map +1 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 auto-swagger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
# auto-swagger
|
|
2
|
+
|
|
3
|
+
> Zero-config, code-first API documentation generator for Next.js and Node.js
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/auto-swagger)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
**Write APIs normally. Docs generate themselves.**
|
|
9
|
+
|
|
10
|
+
- ❌ NO manual Swagger files
|
|
11
|
+
- ❌ NO decorators everywhere
|
|
12
|
+
- ❌ NO YAML/JSON configs
|
|
13
|
+
- ❌ NO schema duplication
|
|
14
|
+
- ✅ Code is the source of truth
|
|
15
|
+
- ✅ TypeScript-first
|
|
16
|
+
- ✅ Zero or near-zero config
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# Using npm
|
|
22
|
+
npm install auto-swagger
|
|
23
|
+
|
|
24
|
+
# Using yarn
|
|
25
|
+
yarn add auto-swagger
|
|
26
|
+
|
|
27
|
+
# Using pnpm
|
|
28
|
+
pnpm add auto-swagger
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
### Option 1: CLI (Recommended for production builds)
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# Initialize config (optional)
|
|
37
|
+
npx auto-swagger init
|
|
38
|
+
|
|
39
|
+
# Generate documentation
|
|
40
|
+
npx auto-swagger generate
|
|
41
|
+
|
|
42
|
+
# Start documentation server
|
|
43
|
+
npx auto-swagger serve
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Option 2: Runtime (Development mode)
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// app/api-docs/route.ts
|
|
50
|
+
import { createDocsHandler } from 'auto-swagger';
|
|
51
|
+
|
|
52
|
+
export const { GET } = createDocsHandler();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
That's it! Visit `/api-docs` to see your documentation.
|
|
56
|
+
|
|
57
|
+
## How It Works
|
|
58
|
+
|
|
59
|
+
auto-swagger automatically:
|
|
60
|
+
|
|
61
|
+
1. **Scans** your project for API route files
|
|
62
|
+
2. **Parses** TypeScript AST to understand your code
|
|
63
|
+
3. **Infers** request/response schemas from types
|
|
64
|
+
4. **Generates** OpenAPI 3.1 specification
|
|
65
|
+
5. **Serves** interactive Swagger UI
|
|
66
|
+
|
|
67
|
+
### Supported Patterns
|
|
68
|
+
|
|
69
|
+
#### Next.js App Router
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
app/
|
|
73
|
+
├── api/
|
|
74
|
+
│ ├── users/
|
|
75
|
+
│ │ ├── route.ts → /api/users
|
|
76
|
+
│ │ └── [id]/
|
|
77
|
+
│ │ └── route.ts → /api/users/{id}
|
|
78
|
+
│ └── posts/
|
|
79
|
+
│ └── route.ts → /api/posts
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Next.js Pages Router
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
pages/
|
|
86
|
+
└── api/
|
|
87
|
+
├── users/
|
|
88
|
+
│ ├── index.ts → /api/users
|
|
89
|
+
│ └── [id].ts → /api/users/{id}
|
|
90
|
+
└── posts.ts → /api/posts
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## API Patterns
|
|
94
|
+
|
|
95
|
+
### Basic Route Handler
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// app/api/users/route.ts
|
|
99
|
+
|
|
100
|
+
export async function GET(request: Request) {
|
|
101
|
+
const users = await db.users.findMany();
|
|
102
|
+
return Response.json(users);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function POST(request: Request) {
|
|
106
|
+
const body = await request.json();
|
|
107
|
+
const user = await db.users.create(body);
|
|
108
|
+
return Response.json(user, { status: 201 });
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### With TypeScript Types
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// Types are automatically inferred
|
|
116
|
+
interface CreateUserInput {
|
|
117
|
+
email: string;
|
|
118
|
+
name: string;
|
|
119
|
+
role: 'admin' | 'user';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function POST(request: Request) {
|
|
123
|
+
const body = await request.json() as CreateUserInput;
|
|
124
|
+
// auto-swagger infers the request body schema
|
|
125
|
+
return Response.json({ success: true });
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### With Zod Schemas
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { z } from 'zod';
|
|
133
|
+
|
|
134
|
+
const CreateUserSchema = z.object({
|
|
135
|
+
email: z.string().email(),
|
|
136
|
+
name: z.string().min(1).max(100),
|
|
137
|
+
role: z.enum(['admin', 'user']),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
export async function POST(request: Request) {
|
|
141
|
+
const body = await request.json();
|
|
142
|
+
const validated = CreateUserSchema.parse(body);
|
|
143
|
+
// auto-swagger converts Zod schema to OpenAPI
|
|
144
|
+
return Response.json(validated);
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Comment-Based Hints
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
/**
|
|
152
|
+
* Get all users
|
|
153
|
+
* Returns a paginated list of users
|
|
154
|
+
*
|
|
155
|
+
* @tags users
|
|
156
|
+
* @auth bearer
|
|
157
|
+
* @rate-limit 100/min
|
|
158
|
+
*/
|
|
159
|
+
export async function GET(request: Request) {
|
|
160
|
+
// ...
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Delete a user
|
|
165
|
+
* @deprecated Use soft delete instead
|
|
166
|
+
*/
|
|
167
|
+
export async function DELETE(request: Request, { params }) {
|
|
168
|
+
// ...
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Configuration
|
|
173
|
+
|
|
174
|
+
Create `auto-swagger.config.js` in your project root:
|
|
175
|
+
|
|
176
|
+
```javascript
|
|
177
|
+
/** @type {import('auto-swagger').AutoSwaggerConfig} */
|
|
178
|
+
module.exports = {
|
|
179
|
+
// Scanner options
|
|
180
|
+
scanner: {
|
|
181
|
+
// Auto-detect framework (nextjs-app, nextjs-pages, express)
|
|
182
|
+
framework: 'auto',
|
|
183
|
+
|
|
184
|
+
// Custom include patterns
|
|
185
|
+
include: ['**/app/**/route.ts'],
|
|
186
|
+
|
|
187
|
+
// Exclude patterns
|
|
188
|
+
exclude: ['**/*.test.ts'],
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
// OpenAPI specification options
|
|
192
|
+
openapi: {
|
|
193
|
+
title: 'My API',
|
|
194
|
+
version: '1.0.0',
|
|
195
|
+
description: 'API documentation',
|
|
196
|
+
|
|
197
|
+
servers: [
|
|
198
|
+
{ url: 'http://localhost:3000', description: 'Development' },
|
|
199
|
+
{ url: 'https://api.example.com', description: 'Production' },
|
|
200
|
+
],
|
|
201
|
+
|
|
202
|
+
// Output format
|
|
203
|
+
outputFormat: 'both', // 'json' | 'yaml' | 'both'
|
|
204
|
+
outputPath: './docs',
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
// UI options
|
|
208
|
+
ui: {
|
|
209
|
+
enabled: true,
|
|
210
|
+
path: '/api-docs',
|
|
211
|
+
theme: 'auto', // 'light' | 'dark' | 'auto'
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## CLI Commands
|
|
217
|
+
|
|
218
|
+
### `auto-swagger init`
|
|
219
|
+
|
|
220
|
+
Initialize configuration file.
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
npx auto-swagger init
|
|
224
|
+
npx auto-swagger init --force # Overwrite existing
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `auto-swagger generate`
|
|
228
|
+
|
|
229
|
+
Generate OpenAPI specification and UI.
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
npx auto-swagger generate
|
|
233
|
+
npx auto-swagger generate --output ./docs
|
|
234
|
+
npx auto-swagger generate --json # Only JSON
|
|
235
|
+
npx auto-swagger generate --yaml # Only YAML
|
|
236
|
+
npx auto-swagger generate --no-ui # Skip UI
|
|
237
|
+
npx auto-swagger generate --verbose # Show details
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### `auto-swagger scan`
|
|
241
|
+
|
|
242
|
+
Scan and display detected routes.
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
npx auto-swagger scan
|
|
246
|
+
npx auto-swagger scan --json # Output as JSON
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### `auto-swagger serve`
|
|
250
|
+
|
|
251
|
+
Start a documentation server.
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
npx auto-swagger serve
|
|
255
|
+
npx auto-swagger serve --port 3001
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Programmatic API
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import { generate, detectRoutes, generateOpenAPISpec } from 'auto-swagger';
|
|
262
|
+
|
|
263
|
+
// Simple generation
|
|
264
|
+
const result = await generate({
|
|
265
|
+
rootDir: process.cwd(),
|
|
266
|
+
output: './docs',
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
console.log(result.spec); // OpenAPI spec object
|
|
270
|
+
console.log(result.json); // JSON string
|
|
271
|
+
console.log(result.yaml); // YAML string
|
|
272
|
+
console.log(result.html); // Swagger UI HTML
|
|
273
|
+
|
|
274
|
+
// Advanced usage
|
|
275
|
+
const scanResult = await detectRoutes({
|
|
276
|
+
config: {
|
|
277
|
+
rootDir: process.cwd(),
|
|
278
|
+
framework: 'auto',
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const spec = generateOpenAPISpec({
|
|
283
|
+
config: {
|
|
284
|
+
title: 'My API',
|
|
285
|
+
version: '1.0.0',
|
|
286
|
+
},
|
|
287
|
+
scanResult,
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Type Inference
|
|
292
|
+
|
|
293
|
+
auto-swagger infers types from multiple sources:
|
|
294
|
+
|
|
295
|
+
### Priority Order
|
|
296
|
+
|
|
297
|
+
1. **Zod schemas** - Highest fidelity, converts to JSON Schema
|
|
298
|
+
2. **TypeScript types** - Type assertions and annotations
|
|
299
|
+
3. **Code analysis** - Response.json() calls, status codes
|
|
300
|
+
4. **Smart defaults** - Based on naming conventions
|
|
301
|
+
|
|
302
|
+
### What's Inferred
|
|
303
|
+
|
|
304
|
+
| Pattern | Inferred As |
|
|
305
|
+
|---------|-------------|
|
|
306
|
+
| `await req.json() as Type` | Request body schema |
|
|
307
|
+
| `Response.json(data)` | Response schema |
|
|
308
|
+
| `{ status: 201 }` | Response status code |
|
|
309
|
+
| `[id]` in path | Path parameter |
|
|
310
|
+
| `@auth bearer` comment | Security requirement |
|
|
311
|
+
| `@deprecated` comment | Deprecated flag |
|
|
312
|
+
| `@tags name` comment | Operation tags |
|
|
313
|
+
|
|
314
|
+
## Supported Comment Hints
|
|
315
|
+
|
|
316
|
+
| Hint | Description | Example |
|
|
317
|
+
|------|-------------|---------|
|
|
318
|
+
| `@tags` | Operation tags | `@tags users, admin` |
|
|
319
|
+
| `@auth` | Auth requirement | `@auth bearer` |
|
|
320
|
+
| `@deprecated` | Mark as deprecated | `@deprecated` |
|
|
321
|
+
| `@summary` | Short description | `@summary Get all users` |
|
|
322
|
+
| `@description` | Long description | `@description Returns...` |
|
|
323
|
+
| `@rate-limit` | Rate limit info | `@rate-limit 100/min` |
|
|
324
|
+
|
|
325
|
+
## Limitations
|
|
326
|
+
|
|
327
|
+
### Current Limitations
|
|
328
|
+
|
|
329
|
+
- Express/Fastify support is planned but not yet available
|
|
330
|
+
- Complex TypeScript generics may not be fully resolved
|
|
331
|
+
- Runtime-only type information (like Prisma types) requires hints
|
|
332
|
+
- Dynamic routes with complex patterns need manual hints
|
|
333
|
+
|
|
334
|
+
### Best Practices
|
|
335
|
+
|
|
336
|
+
1. **Use Zod** for complex schemas - gives best inference
|
|
337
|
+
2. **Add type assertions** - helps TypeScript inference
|
|
338
|
+
3. **Write JSDoc comments** - provides descriptions
|
|
339
|
+
4. **Keep routes focused** - one resource per route file
|
|
340
|
+
|
|
341
|
+
## Examples
|
|
342
|
+
|
|
343
|
+
See the [example](./example) directory for a complete Next.js application demonstrating all features.
|
|
344
|
+
|
|
345
|
+
## Contributing
|
|
346
|
+
|
|
347
|
+
Contributions are welcome! Please read our contributing guide before submitting PRs.
|
|
348
|
+
|
|
349
|
+
## License
|
|
350
|
+
|
|
351
|
+
MIT © auto-swagger
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|