next-openapi-gen 0.5.2 → 0.5.3
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/dist/lib/zod-converter.js +99 -0
- package/package.json +1 -1
|
@@ -138,6 +138,12 @@ export class ZodSchemaConverter {
|
|
|
138
138
|
if (!content.includes(schemaName)) {
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
|
+
// Pre-process all schemas in file
|
|
142
|
+
this.preprocessAllSchemasInFile(filePath);
|
|
143
|
+
// Return it, if the schema has already been processed during pre-processing
|
|
144
|
+
if (this.zodSchemas[schemaName]) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
141
147
|
// Parse the file
|
|
142
148
|
const ast = parse(content, {
|
|
143
149
|
sourceType: "module",
|
|
@@ -593,6 +599,44 @@ export class ZodSchemaConverter {
|
|
|
593
599
|
console.log(`Skipping property ${index} - unsupported key type`);
|
|
594
600
|
return; // Skip if key is not identifier or string literal
|
|
595
601
|
}
|
|
602
|
+
if (t.isCallExpression(prop.value) &&
|
|
603
|
+
t.isMemberExpression(prop.value.callee) &&
|
|
604
|
+
t.isIdentifier(prop.value.callee.object)) {
|
|
605
|
+
const schemaName = prop.value.callee.object.name;
|
|
606
|
+
// @ts-ignore
|
|
607
|
+
const methodName = prop.value.callee.property.name;
|
|
608
|
+
// Process base schema first
|
|
609
|
+
if (!this.zodSchemas[schemaName]) {
|
|
610
|
+
this.convertZodSchemaToOpenApi(schemaName);
|
|
611
|
+
}
|
|
612
|
+
// For describe method, use reference with description
|
|
613
|
+
if (methodName === "describe" && this.zodSchemas[schemaName]) {
|
|
614
|
+
if (prop.value.arguments.length > 0 &&
|
|
615
|
+
t.isStringLiteral(prop.value.arguments[0])) {
|
|
616
|
+
properties[propName] = {
|
|
617
|
+
allOf: [{ $ref: `#/components/schemas/${schemaName}` }],
|
|
618
|
+
description: prop.value.arguments[0].value,
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
properties[propName] = {
|
|
623
|
+
$ref: `#/components/schemas/${schemaName}`,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
required.push(propName);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
// For other methods, process normally
|
|
630
|
+
const processedSchema = this.processZodNode(prop.value);
|
|
631
|
+
if (processedSchema) {
|
|
632
|
+
properties[propName] = processedSchema;
|
|
633
|
+
const isOptional = this.isOptional(prop.value) || this.hasOptionalMethod(prop.value);
|
|
634
|
+
if (!isOptional) {
|
|
635
|
+
required.push(propName);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
596
640
|
// Check if the property value is an identifier (reference to another schema)
|
|
597
641
|
if (t.isIdentifier(prop.value)) {
|
|
598
642
|
const referencedSchemaName = prop.value.name;
|
|
@@ -1181,4 +1225,59 @@ export class ZodSchemaConverter {
|
|
|
1181
1225
|
console.error(`Error scanning directory ${dir} for type mappings:`, error);
|
|
1182
1226
|
}
|
|
1183
1227
|
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Pre-process all Zod schemas in a file
|
|
1230
|
+
*/
|
|
1231
|
+
preprocessAllSchemasInFile(filePath) {
|
|
1232
|
+
try {
|
|
1233
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
1234
|
+
const ast = parse(content, {
|
|
1235
|
+
sourceType: "module",
|
|
1236
|
+
plugins: ["typescript", "decorators-legacy"],
|
|
1237
|
+
});
|
|
1238
|
+
// Collect all exported Zod schemas
|
|
1239
|
+
traverse.default(ast, {
|
|
1240
|
+
ExportNamedDeclaration: (path) => {
|
|
1241
|
+
if (t.isVariableDeclaration(path.node.declaration)) {
|
|
1242
|
+
path.node.declaration.declarations.forEach((declaration) => {
|
|
1243
|
+
if (t.isIdentifier(declaration.id) && declaration.init) {
|
|
1244
|
+
const schemaName = declaration.id.name;
|
|
1245
|
+
// Check if is Zos schema
|
|
1246
|
+
if (this.isZodSchema(declaration.init) &&
|
|
1247
|
+
!this.zodSchemas[schemaName]) {
|
|
1248
|
+
console.log(`Pre-processing Zod schema: ${schemaName}`);
|
|
1249
|
+
this.processingSchemas.add(schemaName);
|
|
1250
|
+
const schema = this.processZodNode(declaration.init);
|
|
1251
|
+
if (schema) {
|
|
1252
|
+
this.zodSchemas[schemaName] = schema;
|
|
1253
|
+
}
|
|
1254
|
+
this.processingSchemas.delete(schemaName);
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
},
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
catch (error) {
|
|
1263
|
+
console.error(`Error pre-processing file ${filePath}:`, error);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* Check if node is Zod schema
|
|
1268
|
+
*/
|
|
1269
|
+
isZodSchema(node) {
|
|
1270
|
+
if (t.isCallExpression(node)) {
|
|
1271
|
+
if (t.isMemberExpression(node.callee) &&
|
|
1272
|
+
t.isIdentifier(node.callee.object) &&
|
|
1273
|
+
node.callee.object.name === "z") {
|
|
1274
|
+
return true;
|
|
1275
|
+
}
|
|
1276
|
+
if (t.isMemberExpression(node.callee) &&
|
|
1277
|
+
t.isCallExpression(node.callee.object)) {
|
|
1278
|
+
return this.isZodSchema(node.callee.object);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
return false;
|
|
1282
|
+
}
|
|
1184
1283
|
}
|
package/package.json
CHANGED