next-openapi-gen 0.10.1 → 0.10.2

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 CHANGED
@@ -72,7 +72,7 @@ During initialization (`npx next-openapi-gen init`), a configuration file `next.
72
72
  ],
73
73
  "apiDir": "src/app/api", // or "pages/api" for Pages Router
74
74
  "routerType": "app", // "app" (default) or "pages" for legacy Pages Router
75
- "schemaDir": "src/types", // or "src/schemas" for Zod schemas
75
+ "schemaDir": "src/types", // or ["src/types", "src/schemas"] for multiple directories
76
76
  "schemaType": "zod", // or "typescript", or ["zod", "typescript"] for multiple
77
77
  "schemaFiles": [], // Optional: ["./schemas/models.yaml", "./schemas/api.json"]
78
78
  "outputFile": "openapi.json",
@@ -90,7 +90,7 @@ During initialization (`npx next-openapi-gen init`), a configuration file `next.
90
90
  | ---------------------- | ----------------------------------------------------------------------------- |
91
91
  | `apiDir` | Path to the API directory |
92
92
  | `routerType` | Router type: `"app"` (default) or `"pages"` for legacy Pages Router |
93
- | `schemaDir` | Path to the types/schemas directory |
93
+ | `schemaDir` | Path to types/schemas directory, or array of paths for multiple directories |
94
94
  | `schemaType` | Schema type: `"zod"`, `"typescript"`, or `["zod", "typescript"]` for multiple |
95
95
  | `schemaFiles` | Optional: Array of custom OpenAPI schema files (YAML/JSON) to include |
96
96
  | `outputFile` | Name of the OpenAPI output file |
@@ -14,8 +14,14 @@ import { logger } from "./logger.js";
14
14
  function normalizeSchemaTypes(schemaType) {
15
15
  return Array.isArray(schemaType) ? schemaType : [schemaType];
16
16
  }
17
+ /**
18
+ * Normalize schemaDir to array
19
+ */
20
+ function normalizeSchemaDirs(schemaDir) {
21
+ return Array.isArray(schemaDir) ? schemaDir : [schemaDir];
22
+ }
17
23
  export class SchemaProcessor {
18
- schemaDir;
24
+ schemaDirs;
19
25
  typeDefinitions = {};
20
26
  openapiDefinitions = {};
21
27
  contentType = "";
@@ -31,7 +37,7 @@ export class SchemaProcessor {
31
37
  importMap = {}; // { filePath: { importName: importPath } }
32
38
  currentFilePath = ""; // Track the file being processed
33
39
  constructor(schemaDir, schemaType = "typescript", schemaFiles) {
34
- this.schemaDir = path.resolve(schemaDir);
40
+ this.schemaDirs = normalizeSchemaDirs(schemaDir).map((d) => path.resolve(d));
35
41
  this.schemaTypes = normalizeSchemaTypes(schemaType);
36
42
  // Initialize Zod converter if Zod is enabled
37
43
  if (this.schemaTypes.includes("zod")) {
@@ -140,9 +146,18 @@ export class SchemaProcessor {
140
146
  logger.debug(`No Zod schema found for ${schemaName}, trying TypeScript fallback`);
141
147
  }
142
148
  // Fall back to TypeScript types
143
- this.scanSchemaDir(this.schemaDir, schemaName);
149
+ this.scanAllSchemaDirs(schemaName);
144
150
  return this.openapiDefinitions[schemaName] || {};
145
151
  }
152
+ scanAllSchemaDirs(schemaName) {
153
+ for (const dir of this.schemaDirs) {
154
+ if (!fs.existsSync(dir)) {
155
+ logger.warn(`Schema directory not found: ${dir}`);
156
+ continue;
157
+ }
158
+ this.scanSchemaDir(dir, schemaName);
159
+ }
160
+ }
146
161
  scanSchemaDir(dir, schemaName) {
147
162
  let files = this.directoryCache[dir];
148
163
  if (typeof files === "undefined") {
@@ -1229,7 +1244,7 @@ export class SchemaProcessor {
1229
1244
  }
1230
1245
  const { baseTypeName, typeArguments } = parsed;
1231
1246
  // Find the base generic type definition
1232
- this.scanSchemaDir(this.schemaDir, baseTypeName);
1247
+ this.scanAllSchemaDirs(baseTypeName);
1233
1248
  const genericDefEntry = this.typeDefinitions[baseTypeName];
1234
1249
  const genericTypeDefinition = genericDefEntry?.node || genericDefEntry;
1235
1250
  if (!genericTypeDefinition) {
@@ -1241,7 +1256,7 @@ export class SchemaProcessor {
1241
1256
  // If it's a simple type reference (not another generic), find its definition
1242
1257
  if (!argTypeName.includes("<") &&
1243
1258
  !this.isGenericTypeParameter(argTypeName)) {
1244
- this.scanSchemaDir(this.schemaDir, argTypeName);
1259
+ this.scanAllSchemaDirs(argTypeName);
1245
1260
  }
1246
1261
  });
1247
1262
  // Create AST nodes for the type arguments by parsing them
@@ -11,7 +11,7 @@ import { DrizzleZodProcessor } from "./drizzle-zod-processor.js";
11
11
  * Class for converting Zod schemas to OpenAPI specifications
12
12
  */
13
13
  export class ZodSchemaConverter {
14
- schemaDir;
14
+ schemaDirs;
15
15
  zodSchemas = {};
16
16
  processingSchemas = new Set();
17
17
  processedModules = new Set();
@@ -26,7 +26,8 @@ export class ZodSchemaConverter {
26
26
  currentAST;
27
27
  currentImports;
28
28
  constructor(schemaDir) {
29
- this.schemaDir = path.resolve(schemaDir);
29
+ const dirs = Array.isArray(schemaDir) ? schemaDir : [schemaDir];
30
+ this.schemaDirs = dirs.map((d) => path.resolve(d));
30
31
  }
31
32
  /**
32
33
  * Find a Zod schema by name and convert it to OpenAPI spec
@@ -63,8 +64,12 @@ export class ZodSchemaConverter {
63
64
  return this.zodSchemas[schemaName];
64
65
  }
65
66
  }
66
- // Scan schema directory
67
- this.scanDirectoryForZodSchema(this.schemaDir, schemaName);
67
+ // Scan schema directories
68
+ for (const dir of this.schemaDirs) {
69
+ this.scanDirectoryForZodSchema(dir, schemaName);
70
+ if (this.zodSchemas[schemaName])
71
+ break;
72
+ }
68
73
  // Return the schema if found, or null if not
69
74
  if (this.zodSchemas[schemaName]) {
70
75
  logger.debug(`Found and processed Zod schema: ${schemaName}`);
@@ -1554,8 +1559,10 @@ export class ZodSchemaConverter {
1554
1559
  for (const routeFile of routeFiles) {
1555
1560
  this.scanFileForTypeMappings(routeFile);
1556
1561
  }
1557
- // Scan schema directory
1558
- this.scanDirectoryForTypeMappings(this.schemaDir);
1562
+ // Scan schema directories
1563
+ for (const dir of this.schemaDirs) {
1564
+ this.scanDirectoryForTypeMappings(dir);
1565
+ }
1559
1566
  }
1560
1567
  /**
1561
1568
  * Scan a single file for type mappings
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-openapi-gen",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "Automatically generate OpenAPI 3.0 documentation from Next.js projects, with support for Zod schemas and TypeScript types.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",