next-openapi-gen 0.4.6 → 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 +25 -6
- package/dist/lib/utils.js +9 -0
- package/dist/lib/zod-converter.js +175 -30
- 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
|
|
@@ -26,6 +26,14 @@ export class SchemaProcessor {
|
|
|
26
26
|
* Get all defined schemas (for components.schemas section)
|
|
27
27
|
*/
|
|
28
28
|
getDefinedSchemas() {
|
|
29
|
+
// If using Zod, also include all processed Zod schemas
|
|
30
|
+
if (this.schemaType === "zod" && this.zodSchemaConverter) {
|
|
31
|
+
const zodSchemas = this.zodSchemaConverter.getProcessedSchemas();
|
|
32
|
+
return {
|
|
33
|
+
...this.openapiDefinitions,
|
|
34
|
+
...zodSchemas,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
29
37
|
return this.openapiDefinitions;
|
|
30
38
|
}
|
|
31
39
|
findSchemaDefinition(schemaName, contentType) {
|
|
@@ -441,10 +449,7 @@ export class SchemaProcessor {
|
|
|
441
449
|
if (description) {
|
|
442
450
|
options.description = description;
|
|
443
451
|
}
|
|
444
|
-
if (this.contentType === "
|
|
445
|
-
options.required = !isOptional;
|
|
446
|
-
}
|
|
447
|
-
else if (this.contentType === "body") {
|
|
452
|
+
if (this.contentType === "body") {
|
|
448
453
|
options.nullable = isOptional;
|
|
449
454
|
}
|
|
450
455
|
return options;
|
|
@@ -526,7 +531,7 @@ export class SchemaProcessor {
|
|
|
526
531
|
schema: {
|
|
527
532
|
type: value.type,
|
|
528
533
|
},
|
|
529
|
-
required: isPathParam ? true : value.required, // Path parameters are always required
|
|
534
|
+
required: isPathParam ? true : !!value.required, // Path parameters are always required
|
|
530
535
|
};
|
|
531
536
|
if (value.enum) {
|
|
532
537
|
param.schema.enum = value.enum;
|
|
@@ -566,7 +571,7 @@ export class SchemaProcessor {
|
|
|
566
571
|
},
|
|
567
572
|
};
|
|
568
573
|
}
|
|
569
|
-
getSchemaContent({ paramsType, pathParamsType, bodyType, responseType, }) {
|
|
574
|
+
getSchemaContent({ tag, paramsType, pathParamsType, bodyType, responseType, }) {
|
|
570
575
|
let params = paramsType ? this.openapiDefinitions[paramsType] : {};
|
|
571
576
|
let pathParams = pathParamsType
|
|
572
577
|
? this.openapiDefinitions[pathParamsType]
|
|
@@ -589,7 +594,21 @@ export class SchemaProcessor {
|
|
|
589
594
|
this.findSchemaDefinition(responseType, "response");
|
|
590
595
|
responses = this.openapiDefinitions[responseType] || {};
|
|
591
596
|
}
|
|
597
|
+
if (this.schemaType === "zod") {
|
|
598
|
+
const schemasToProcess = [
|
|
599
|
+
paramsType,
|
|
600
|
+
pathParamsType,
|
|
601
|
+
bodyType,
|
|
602
|
+
responseType,
|
|
603
|
+
].filter(Boolean);
|
|
604
|
+
schemasToProcess.forEach((schemaName) => {
|
|
605
|
+
if (!this.openapiDefinitions[schemaName]) {
|
|
606
|
+
this.findSchemaDefinition(schemaName, "");
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
592
610
|
return {
|
|
611
|
+
tag,
|
|
593
612
|
params,
|
|
594
613
|
pathParams,
|
|
595
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,
|
|
@@ -9,7 +9,6 @@ import * as t from "@babel/types";
|
|
|
9
9
|
export class ZodSchemaConverter {
|
|
10
10
|
schemaDir;
|
|
11
11
|
zodSchemas = {};
|
|
12
|
-
processedFiles = {};
|
|
13
12
|
processingSchemas = new Set();
|
|
14
13
|
processedModules = new Set();
|
|
15
14
|
constructor(schemaDir) {
|
|
@@ -111,10 +110,6 @@ export class ZodSchemaConverter {
|
|
|
111
110
|
}
|
|
112
111
|
else if (file.endsWith(".ts") || file.endsWith(".tsx")) {
|
|
113
112
|
this.processFileForZodSchema(filePath, schemaName);
|
|
114
|
-
// Stop searching if we found the schema
|
|
115
|
-
if (this.zodSchemas[schemaName]) {
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
113
|
}
|
|
119
114
|
}
|
|
120
115
|
}
|
|
@@ -126,12 +121,12 @@ export class ZodSchemaConverter {
|
|
|
126
121
|
* Process a file to find Zod schema definitions
|
|
127
122
|
*/
|
|
128
123
|
processFileForZodSchema(filePath, schemaName) {
|
|
129
|
-
// Skip if already processed
|
|
130
|
-
if (this.processedFiles[filePath])
|
|
131
|
-
return;
|
|
132
|
-
this.processedFiles[filePath] = true;
|
|
133
124
|
try {
|
|
134
125
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
126
|
+
// Check if file contains schema we are looking for
|
|
127
|
+
if (!content.includes(schemaName)) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
135
130
|
// Parse the file
|
|
136
131
|
const ast = parse(content, {
|
|
137
132
|
sourceType: "module",
|
|
@@ -161,9 +156,25 @@ export class ZodSchemaConverter {
|
|
|
161
156
|
if (t.isIdentifier(declaration.id) &&
|
|
162
157
|
declaration.id.name === schemaName &&
|
|
163
158
|
declaration.init) {
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
159
|
+
// Check if this is a call expression with .extend()
|
|
160
|
+
if (t.isCallExpression(declaration.init) &&
|
|
161
|
+
t.isMemberExpression(declaration.init.callee) &&
|
|
162
|
+
t.isIdentifier(declaration.init.callee.property) &&
|
|
163
|
+
declaration.init.callee.property.name === "extend") {
|
|
164
|
+
const schema = this.processZodNode(declaration.init);
|
|
165
|
+
if (schema) {
|
|
166
|
+
this.zodSchemas[schemaName] = schema;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Existing code for z.object({...})
|
|
170
|
+
else if (t.isCallExpression(declaration.init) &&
|
|
171
|
+
t.isMemberExpression(declaration.init.callee) &&
|
|
172
|
+
t.isIdentifier(declaration.init.callee.object) &&
|
|
173
|
+
declaration.init.callee.object.name === "z") {
|
|
174
|
+
const schema = this.processZodNode(declaration.init);
|
|
175
|
+
if (schema) {
|
|
176
|
+
this.zodSchemas[schemaName] = schema;
|
|
177
|
+
}
|
|
167
178
|
}
|
|
168
179
|
}
|
|
169
180
|
});
|
|
@@ -208,9 +219,22 @@ export class ZodSchemaConverter {
|
|
|
208
219
|
if (t.isIdentifier(path.node.id) &&
|
|
209
220
|
path.node.id.name === schemaName &&
|
|
210
221
|
path.node.init) {
|
|
211
|
-
|
|
212
|
-
if (
|
|
213
|
-
|
|
222
|
+
// Check if it is .extend()
|
|
223
|
+
if (t.isCallExpression(path.node.init) &&
|
|
224
|
+
t.isMemberExpression(path.node.init.callee) &&
|
|
225
|
+
t.isIdentifier(path.node.init.callee.property) &&
|
|
226
|
+
path.node.init.callee.property.name === "extend") {
|
|
227
|
+
const schema = this.processZodNode(path.node.init);
|
|
228
|
+
if (schema) {
|
|
229
|
+
this.zodSchemas[schemaName] = schema;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// Existing code
|
|
233
|
+
else {
|
|
234
|
+
const schema = this.processZodNode(path.node.init);
|
|
235
|
+
if (schema) {
|
|
236
|
+
this.zodSchemas[schemaName] = schema;
|
|
237
|
+
}
|
|
214
238
|
}
|
|
215
239
|
}
|
|
216
240
|
},
|
|
@@ -243,10 +267,64 @@ export class ZodSchemaConverter {
|
|
|
243
267
|
console.error(`Error processing file ${filePath} for schema ${schemaName}: ${error}`);
|
|
244
268
|
}
|
|
245
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Process all exported schemas in a file, not just the one we're looking for
|
|
272
|
+
*/
|
|
273
|
+
processAllSchemasInFile(filePath) {
|
|
274
|
+
try {
|
|
275
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
276
|
+
const ast = parse(content, {
|
|
277
|
+
sourceType: "module",
|
|
278
|
+
plugins: ["typescript", "decorators-legacy"],
|
|
279
|
+
});
|
|
280
|
+
traverse.default(ast, {
|
|
281
|
+
ExportNamedDeclaration: (path) => {
|
|
282
|
+
if (t.isVariableDeclaration(path.node.declaration)) {
|
|
283
|
+
path.node.declaration.declarations.forEach((declaration) => {
|
|
284
|
+
if (t.isIdentifier(declaration.id) &&
|
|
285
|
+
declaration.init &&
|
|
286
|
+
t.isCallExpression(declaration.init) &&
|
|
287
|
+
t.isMemberExpression(declaration.init.callee) &&
|
|
288
|
+
t.isIdentifier(declaration.init.callee.object) &&
|
|
289
|
+
declaration.init.callee.object.name === "z") {
|
|
290
|
+
const schemaName = declaration.id.name;
|
|
291
|
+
if (!this.zodSchemas[schemaName] &&
|
|
292
|
+
!this.processingSchemas.has(schemaName)) {
|
|
293
|
+
this.processingSchemas.add(schemaName);
|
|
294
|
+
const schema = this.processZodNode(declaration.init);
|
|
295
|
+
if (schema) {
|
|
296
|
+
this.zodSchemas[schemaName] = schema;
|
|
297
|
+
}
|
|
298
|
+
this.processingSchemas.delete(schemaName);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
console.error(`Error processing all schemas in file ${filePath}: ${error}`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
246
310
|
/**
|
|
247
311
|
* Process a Zod node and convert it to OpenAPI schema
|
|
248
312
|
*/
|
|
249
313
|
processZodNode(node) {
|
|
314
|
+
// Handle reference to another schema (e.g. UserBaseSchema.extend)
|
|
315
|
+
if (t.isCallExpression(node) &&
|
|
316
|
+
t.isMemberExpression(node.callee) &&
|
|
317
|
+
t.isIdentifier(node.callee.object) &&
|
|
318
|
+
t.isIdentifier(node.callee.property) &&
|
|
319
|
+
node.callee.property.name === "extend") {
|
|
320
|
+
const baseSchemaName = node.callee.object.name;
|
|
321
|
+
// Check if the base schema already exists
|
|
322
|
+
if (!this.zodSchemas[baseSchemaName]) {
|
|
323
|
+
// Try to find the basic pattern
|
|
324
|
+
this.convertZodSchemaToOpenApi(baseSchemaName);
|
|
325
|
+
}
|
|
326
|
+
return this.processZodChain(node);
|
|
327
|
+
}
|
|
250
328
|
// Handle z.object({...})
|
|
251
329
|
if (t.isCallExpression(node) &&
|
|
252
330
|
t.isMemberExpression(node.callee) &&
|
|
@@ -461,7 +539,7 @@ export class ZodSchemaConverter {
|
|
|
461
539
|
const objectExpression = node.arguments[0];
|
|
462
540
|
const properties = {};
|
|
463
541
|
const required = [];
|
|
464
|
-
objectExpression.properties.forEach((prop) => {
|
|
542
|
+
objectExpression.properties.forEach((prop, index) => {
|
|
465
543
|
if (t.isObjectProperty(prop)) {
|
|
466
544
|
let propName;
|
|
467
545
|
// Handle both identifier and string literal keys
|
|
@@ -472,15 +550,56 @@ export class ZodSchemaConverter {
|
|
|
472
550
|
propName = prop.key.value;
|
|
473
551
|
}
|
|
474
552
|
else {
|
|
553
|
+
console.log(`Skipping property ${index} - unsupported key type`);
|
|
475
554
|
return; // Skip if key is not identifier or string literal
|
|
476
555
|
}
|
|
556
|
+
// Check if the property value is an identifier (reference to another schema)
|
|
557
|
+
if (t.isIdentifier(prop.value)) {
|
|
558
|
+
const referencedSchemaName = prop.value.name;
|
|
559
|
+
// Try to find and convert the referenced schema
|
|
560
|
+
if (!this.zodSchemas[referencedSchemaName]) {
|
|
561
|
+
this.convertZodSchemaToOpenApi(referencedSchemaName);
|
|
562
|
+
}
|
|
563
|
+
// Create a reference
|
|
564
|
+
properties[propName] = {
|
|
565
|
+
$ref: `#/components/schemas/${referencedSchemaName}`,
|
|
566
|
+
};
|
|
567
|
+
required.push(propName); // Assuming it's required unless marked optional
|
|
568
|
+
}
|
|
569
|
+
// For array of schemas (like z.array(PaymentMethodSchema))
|
|
570
|
+
if (t.isCallExpression(prop.value) &&
|
|
571
|
+
t.isMemberExpression(prop.value.callee) &&
|
|
572
|
+
t.isIdentifier(prop.value.callee.object) &&
|
|
573
|
+
prop.value.callee.object.name === "z" &&
|
|
574
|
+
t.isIdentifier(prop.value.callee.property) &&
|
|
575
|
+
prop.value.callee.property.name === "array" &&
|
|
576
|
+
prop.value.arguments.length > 0 &&
|
|
577
|
+
t.isIdentifier(prop.value.arguments[0])) {
|
|
578
|
+
const itemSchemaName = prop.value.arguments[0].name;
|
|
579
|
+
// Try to find and convert the referenced schema
|
|
580
|
+
if (!this.zodSchemas[itemSchemaName]) {
|
|
581
|
+
this.convertZodSchemaToOpenApi(itemSchemaName);
|
|
582
|
+
}
|
|
583
|
+
// Process as array with reference
|
|
584
|
+
const arraySchema = this.processZodNode(prop.value);
|
|
585
|
+
arraySchema.items = {
|
|
586
|
+
$ref: `#/components/schemas/${itemSchemaName}`,
|
|
587
|
+
};
|
|
588
|
+
properties[propName] = arraySchema;
|
|
589
|
+
const isOptional = this.isOptional(prop.value) || this.hasOptionalMethod(prop.value);
|
|
590
|
+
if (!isOptional) {
|
|
591
|
+
required.push(propName);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
477
594
|
// Process property value (a Zod schema)
|
|
478
595
|
const propSchema = this.processZodNode(prop.value);
|
|
479
596
|
if (propSchema) {
|
|
480
597
|
properties[propName] = propSchema;
|
|
481
598
|
// If the property is not marked as optional, add it to required list
|
|
599
|
+
const isOptional =
|
|
482
600
|
// @ts-ignore
|
|
483
|
-
|
|
601
|
+
this.isOptional(prop.value) || this.hasOptionalMethod(prop.value);
|
|
602
|
+
if (!isOptional) {
|
|
484
603
|
required.push(propName);
|
|
485
604
|
}
|
|
486
605
|
}
|
|
@@ -534,7 +653,20 @@ export class ZodSchemaConverter {
|
|
|
534
653
|
case "array":
|
|
535
654
|
let itemsType = { type: "string" };
|
|
536
655
|
if (node.arguments.length > 0) {
|
|
537
|
-
|
|
656
|
+
// Check if argument is an identifier (schema reference)
|
|
657
|
+
if (t.isIdentifier(node.arguments[0])) {
|
|
658
|
+
const schemaName = node.arguments[0].name;
|
|
659
|
+
// Try to find and convert the referenced schema
|
|
660
|
+
if (!this.zodSchemas[schemaName]) {
|
|
661
|
+
this.convertZodSchemaToOpenApi(schemaName);
|
|
662
|
+
}
|
|
663
|
+
// @ts-ignore
|
|
664
|
+
itemsType = { $ref: `#/components/schemas/${schemaName}` };
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
// @ts-ignore
|
|
668
|
+
itemsType = this.processZodNode(node.arguments[0]);
|
|
669
|
+
}
|
|
538
670
|
}
|
|
539
671
|
schema = { type: "array", items: itemsType };
|
|
540
672
|
break;
|
|
@@ -806,7 +938,10 @@ export class ZodSchemaConverter {
|
|
|
806
938
|
case "extend":
|
|
807
939
|
if (node.arguments.length > 0 &&
|
|
808
940
|
t.isObjectExpression(node.arguments[0])) {
|
|
809
|
-
|
|
941
|
+
// Get the base schema by processing the object that extend is called on
|
|
942
|
+
const baseSchema = this.processZodNode(node.callee.object);
|
|
943
|
+
// Process the extension object
|
|
944
|
+
const extendNode = {
|
|
810
945
|
type: "CallExpression",
|
|
811
946
|
callee: {
|
|
812
947
|
type: "MemberExpression",
|
|
@@ -816,18 +951,28 @@ export class ZodSchemaConverter {
|
|
|
816
951
|
optional: false,
|
|
817
952
|
},
|
|
818
953
|
arguments: [node.arguments[0]],
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
954
|
+
};
|
|
955
|
+
const extendedProps = this.processZodObject(extendNode);
|
|
956
|
+
// Merge base schema and extensions
|
|
957
|
+
if (baseSchema && baseSchema.properties) {
|
|
958
|
+
schema = {
|
|
959
|
+
type: "object",
|
|
960
|
+
properties: {
|
|
961
|
+
...baseSchema.properties,
|
|
962
|
+
...(extendedProps?.properties || {}),
|
|
963
|
+
},
|
|
964
|
+
required: [
|
|
965
|
+
...(baseSchema.required || []),
|
|
966
|
+
...(extendedProps?.required || []),
|
|
967
|
+
].filter((item, index, arr) => arr.indexOf(item) === index), // Remove duplicates
|
|
824
968
|
};
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
969
|
+
// Copy other properties from base schema
|
|
970
|
+
if (baseSchema.description)
|
|
971
|
+
schema.description = baseSchema.description;
|
|
972
|
+
}
|
|
973
|
+
else {
|
|
974
|
+
console.log(`Warning: Could not resolve base schema for extend`);
|
|
975
|
+
schema = extendedProps || { type: "object" };
|
|
831
976
|
}
|
|
832
977
|
}
|
|
833
978
|
break;
|
package/package.json
CHANGED