next-openapi-gen 0.5.0 → 0.5.1
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/README.md +47 -39
- package/dist/lib/route-processor.js +2 -2
- package/dist/lib/schema-processor.js +2 -1
- package/dist/lib/utils.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,8 +57,8 @@ During initialization (`npx next-openapi-gen init`), a configuration file `next.
|
|
|
57
57
|
}
|
|
58
58
|
],
|
|
59
59
|
"apiDir": "src/app/api",
|
|
60
|
-
"schemaDir": "src/types",
|
|
61
|
-
"schemaType": "typescript",
|
|
60
|
+
"schemaDir": "src/types", // or e.g. "src/schemas" for Zod schemas
|
|
61
|
+
"schemaType": "typescript", // or "zod" for Zod schemas
|
|
62
62
|
"outputFile": "openapi.json",
|
|
63
63
|
"docsUrl": "/api-docs",
|
|
64
64
|
"includeOpenApiRoutes": false
|
|
@@ -67,13 +67,13 @@ During initialization (`npx next-openapi-gen init`), a configuration file `next.
|
|
|
67
67
|
|
|
68
68
|
### Configuration Options
|
|
69
69
|
|
|
70
|
-
| Option
|
|
71
|
-
|
|
72
|
-
| `apiDir`
|
|
73
|
-
| `schemaDir`
|
|
74
|
-
| `schemaType`
|
|
75
|
-
| `outputFile`
|
|
76
|
-
| `docsUrl`
|
|
70
|
+
| Option | Description |
|
|
71
|
+
| ---------------------- | ------------------------------------------------ |
|
|
72
|
+
| `apiDir` | Path to the API directory |
|
|
73
|
+
| `schemaDir` | Path to the types/schemas directory |
|
|
74
|
+
| `schemaType` | Schema type: `"typescript"` or `"zod"` |
|
|
75
|
+
| `outputFile` | Path to the OpenAPI output file |
|
|
76
|
+
| `docsUrl` | API documentation URL (for Swagger UI) |
|
|
77
77
|
| `includeOpenApiRoutes` | Whether to include only routes with @openapi tag |
|
|
78
78
|
|
|
79
79
|
## Documenting Your API
|
|
@@ -83,7 +83,7 @@ During initialization (`npx next-openapi-gen init`), a configuration file `next.
|
|
|
83
83
|
```typescript
|
|
84
84
|
// src/app/api/users/[id]/route.ts
|
|
85
85
|
|
|
86
|
-
import { NextRequest, NextResponse } from
|
|
86
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
87
87
|
|
|
88
88
|
type UserParams = {
|
|
89
89
|
id: string; // User ID
|
|
@@ -115,17 +115,17 @@ export async function GET(
|
|
|
115
115
|
```typescript
|
|
116
116
|
// src/app/api/products/[id]/route.ts
|
|
117
117
|
|
|
118
|
-
import { NextRequest, NextResponse } from
|
|
119
|
-
import { z } from
|
|
118
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
119
|
+
import { z } from "zod";
|
|
120
120
|
|
|
121
121
|
export const ProductParams = z.object({
|
|
122
|
-
id: z.string().describe("Product ID")
|
|
122
|
+
id: z.string().describe("Product ID"),
|
|
123
123
|
});
|
|
124
124
|
|
|
125
125
|
export const ProductResponse = z.object({
|
|
126
126
|
id: z.string().describe("Product ID"),
|
|
127
127
|
name: z.string().describe("Product name"),
|
|
128
|
-
price: z.number().positive().describe("Product price")
|
|
128
|
+
price: z.number().positive().describe("Product price"),
|
|
129
129
|
});
|
|
130
130
|
|
|
131
131
|
/**
|
|
@@ -145,15 +145,16 @@ export async function GET(
|
|
|
145
145
|
|
|
146
146
|
## JSDoc Documentation Tags
|
|
147
147
|
|
|
148
|
-
| Tag
|
|
149
|
-
|
|
150
|
-
| `@desc`
|
|
151
|
-
| `@pathParams` | Path parameters type/schema
|
|
152
|
-
| `@params`
|
|
153
|
-
| `@body`
|
|
154
|
-
| `@response`
|
|
155
|
-
| `@auth`
|
|
156
|
-
| `@
|
|
148
|
+
| Tag | Description |
|
|
149
|
+
| ------------- | ----------------------------------------------------------------------------------- |
|
|
150
|
+
| `@desc` | Endpoint description |
|
|
151
|
+
| `@pathParams` | Path parameters type/schema |
|
|
152
|
+
| `@params` | Query parameters type/schema |
|
|
153
|
+
| `@body` | Request body type/schema |
|
|
154
|
+
| `@response` | Response type/schema |
|
|
155
|
+
| `@auth` | Authorization type (`bearer`, `basic`, `apikey`) |
|
|
156
|
+
| `@tag` | Custom tag |
|
|
157
|
+
| `@openapi` | Marks the route for inclusion in documentation (if includeOpenApiRoutes is enabled) |
|
|
157
158
|
|
|
158
159
|
## CLI Usage
|
|
159
160
|
|
|
@@ -164,6 +165,7 @@ npx next-openapi-gen init
|
|
|
164
165
|
```
|
|
165
166
|
|
|
166
167
|
This command will generate following elements:
|
|
168
|
+
|
|
167
169
|
- Generate `next.openapi.json` configuration file
|
|
168
170
|
- Install UI interface (default `Scalar`)
|
|
169
171
|
- Add `/api-docs` page to display OpenAPI documentation
|
|
@@ -175,6 +177,7 @@ npx next-openapi-gen generate
|
|
|
175
177
|
```
|
|
176
178
|
|
|
177
179
|
This command will generate OpenAPI documentation based on your API code:
|
|
180
|
+
|
|
178
181
|
- Scan API directories for routes
|
|
179
182
|
- Analyze types/schemas
|
|
180
183
|
- Generate OpenAPI file (`openapi.json`) in `public` folder
|
|
@@ -182,7 +185,7 @@ This command will generate OpenAPI documentation based on your API code:
|
|
|
182
185
|
|
|
183
186
|
### 3. View API Documentation
|
|
184
187
|
|
|
185
|
-
To see API documenation go to `http://localhost:3000/api-docs`
|
|
188
|
+
To see API documenation go to `http://localhost:3000/api-docs`
|
|
186
189
|
|
|
187
190
|
## Examples
|
|
188
191
|
|
|
@@ -198,7 +201,7 @@ type UserParams = {
|
|
|
198
201
|
|
|
199
202
|
// Or Zod
|
|
200
203
|
const UserParams = z.object({
|
|
201
|
-
id: z.string().describe("User ID")
|
|
204
|
+
id: z.string().describe("User ID"),
|
|
202
205
|
});
|
|
203
206
|
|
|
204
207
|
/**
|
|
@@ -225,7 +228,7 @@ type UsersQueryParams = {
|
|
|
225
228
|
const UsersQueryParams = z.object({
|
|
226
229
|
page: z.number().optional().describe("Page number"),
|
|
227
230
|
limit: z.number().optional().describe("Results per page"),
|
|
228
|
-
search: z.string().optional().describe("Search phrase")
|
|
231
|
+
search: z.string().optional().describe("Search phrase"),
|
|
229
232
|
});
|
|
230
233
|
|
|
231
234
|
/**
|
|
@@ -252,7 +255,7 @@ type CreateUserBody = {
|
|
|
252
255
|
const CreateUserBody = z.object({
|
|
253
256
|
name: z.string().describe("Full name"),
|
|
254
257
|
email: z.string().email().describe("Email address"),
|
|
255
|
-
password: z.string().min(8).describe("Password")
|
|
258
|
+
password: z.string().min(8).describe("Password"),
|
|
256
259
|
});
|
|
257
260
|
|
|
258
261
|
/**
|
|
@@ -281,7 +284,7 @@ const UserResponse = z.object({
|
|
|
281
284
|
id: z.string().describe("User ID"),
|
|
282
285
|
name: z.string().describe("Full name"),
|
|
283
286
|
email: z.string().email().describe("Email address"),
|
|
284
|
-
createdAt: z.date().describe("Creation date")
|
|
287
|
+
createdAt: z.date().describe("Creation date"),
|
|
285
288
|
});
|
|
286
289
|
|
|
287
290
|
/**
|
|
@@ -326,14 +329,14 @@ If no type/schema is provided for path parameters, a default schema will be gene
|
|
|
326
329
|
|
|
327
330
|
The library generates intelligent examples for parameters based on their name:
|
|
328
331
|
|
|
329
|
-
| Parameter name | Example
|
|
330
|
-
|
|
331
|
-
| `id`, `*Id`
|
|
332
|
-
| `slug`
|
|
333
|
-
| `uuid`
|
|
334
|
-
| `email`
|
|
335
|
-
| `name`
|
|
336
|
-
| `date`
|
|
332
|
+
| Parameter name | Example |
|
|
333
|
+
| -------------- | ---------------------------------------- |
|
|
334
|
+
| `id`, `*Id` | `"123"` or `123` |
|
|
335
|
+
| `slug` | `"example-slug"` |
|
|
336
|
+
| `uuid` | `"123e4567-e89b-12d3-a456-426614174000"` |
|
|
337
|
+
| `email` | `"user@example.com"` |
|
|
338
|
+
| `name` | `"example-name"` |
|
|
339
|
+
| `date` | `"2023-01-01"` |
|
|
337
340
|
|
|
338
341
|
## Advanced Zod Features
|
|
339
342
|
|
|
@@ -343,7 +346,12 @@ The library supports advanced Zod features such as:
|
|
|
343
346
|
|
|
344
347
|
```typescript
|
|
345
348
|
// Zod validation chains are properly converted to OpenAPI schemas
|
|
346
|
-
const EmailSchema = z
|
|
349
|
+
const EmailSchema = z
|
|
350
|
+
.string()
|
|
351
|
+
.email()
|
|
352
|
+
.min(5)
|
|
353
|
+
.max(100)
|
|
354
|
+
.describe("Email address");
|
|
347
355
|
|
|
348
356
|
// Converts to OpenAPI with email format, minLength and maxLength
|
|
349
357
|
```
|
|
@@ -352,11 +360,11 @@ const EmailSchema = z.string().email().min(5).max(100).describe("Email address")
|
|
|
352
360
|
|
|
353
361
|
```typescript
|
|
354
362
|
// You can use TypeScript with Zod types
|
|
355
|
-
import { z } from
|
|
363
|
+
import { z } from "zod";
|
|
356
364
|
|
|
357
365
|
const UserSchema = z.object({
|
|
358
366
|
id: z.string().uuid(),
|
|
359
|
-
name: z.string().min(2)
|
|
367
|
+
name: z.string().min(2),
|
|
360
368
|
});
|
|
361
369
|
|
|
362
370
|
// Use z.infer to create a TypeScript type
|
|
@@ -109,7 +109,7 @@ export class RouteProcessor {
|
|
|
109
109
|
const routePath = this.getRoutePath(filePath);
|
|
110
110
|
const rootPath = capitalize(routePath.split("/")[1]);
|
|
111
111
|
const operationId = getOperationId(routePath, method);
|
|
112
|
-
const { summary, description, auth, isOpenApi } = dataTypes;
|
|
112
|
+
const { tag, summary, description, auth, isOpenApi } = dataTypes;
|
|
113
113
|
if (this.config.includeOpenApiRoutes && !isOpenApi) {
|
|
114
114
|
// If flag is enabled and there is no @openapi tag, then skip path
|
|
115
115
|
return;
|
|
@@ -122,7 +122,7 @@ export class RouteProcessor {
|
|
|
122
122
|
operationId: operationId,
|
|
123
123
|
summary: summary,
|
|
124
124
|
description: description,
|
|
125
|
-
tags: [rootPath],
|
|
125
|
+
tags: [tag || rootPath],
|
|
126
126
|
parameters: [],
|
|
127
127
|
};
|
|
128
128
|
// Add auth
|
|
@@ -571,7 +571,7 @@ export class SchemaProcessor {
|
|
|
571
571
|
},
|
|
572
572
|
};
|
|
573
573
|
}
|
|
574
|
-
getSchemaContent({ paramsType, pathParamsType, bodyType, responseType, }) {
|
|
574
|
+
getSchemaContent({ tag, paramsType, pathParamsType, bodyType, responseType, }) {
|
|
575
575
|
let params = paramsType ? this.openapiDefinitions[paramsType] : {};
|
|
576
576
|
let pathParams = pathParamsType
|
|
577
577
|
? this.openapiDefinitions[pathParamsType]
|
|
@@ -608,6 +608,7 @@ export class SchemaProcessor {
|
|
|
608
608
|
});
|
|
609
609
|
}
|
|
610
610
|
return {
|
|
611
|
+
tag,
|
|
611
612
|
params,
|
|
612
613
|
pathParams,
|
|
613
614
|
body,
|
package/dist/lib/utils.js
CHANGED
|
@@ -16,6 +16,7 @@ export function extractPathParameters(routePath) {
|
|
|
16
16
|
}
|
|
17
17
|
export function extractJSDocComments(path) {
|
|
18
18
|
const comments = path.node.leadingComments;
|
|
19
|
+
let tag = "";
|
|
19
20
|
let summary = "";
|
|
20
21
|
let description = "";
|
|
21
22
|
let paramsType = "";
|
|
@@ -50,6 +51,13 @@ export function extractJSDocComments(path) {
|
|
|
50
51
|
const regex = /@desc\s*(.*)/;
|
|
51
52
|
description = commentValue.match(regex)[1].trim();
|
|
52
53
|
}
|
|
54
|
+
if (commentValue.includes("@tag")) {
|
|
55
|
+
const regex = /@tag\s*(.*)/;
|
|
56
|
+
const match = commentValue.match(regex);
|
|
57
|
+
if (match && match[1]) {
|
|
58
|
+
tag = match[1].trim();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
53
61
|
if (commentValue.includes("@params")) {
|
|
54
62
|
paramsType = extractTypeFromComment(commentValue, "@params");
|
|
55
63
|
}
|
|
@@ -65,6 +73,7 @@ export function extractJSDocComments(path) {
|
|
|
65
73
|
});
|
|
66
74
|
}
|
|
67
75
|
return {
|
|
76
|
+
tag,
|
|
68
77
|
auth,
|
|
69
78
|
summary,
|
|
70
79
|
description,
|
package/package.json
CHANGED