api-core-lib 12.0.69 → 12.0.70
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/cli.cjs +32 -21
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -76,14 +76,14 @@ var import_swagger_parser = __toESM(require("@apidevtools/swagger-parser"), 1);
|
|
|
76
76
|
var import_openapi_types = __toESM(require_dist(), 1);
|
|
77
77
|
var DEBUG_MODE = process.env.DEBUG === "true";
|
|
78
78
|
var debugLog = (title, data) => DEBUG_MODE && console.log(import_chalk.default.yellow(`
|
|
79
|
-
[DEBUG: ${title}]`), import_util.default.inspect(data, { depth, colors: true }));
|
|
79
|
+
[DEBUG: ${title}]`), import_util.default.inspect(data, { depth: 5, colors: true }));
|
|
80
80
|
var toPascalCase = (str) => str.replace(/[^a-zA-Z0-9_]/g, " ").replace(/(?:^\w|[A-Z]|\b\w)/g, (w) => w.toUpperCase()).replace(/\s+/g, "");
|
|
81
81
|
var toCamelCase = (str) => {
|
|
82
82
|
const s = toPascalCase(str);
|
|
83
83
|
return s.charAt(0).toLowerCase() + s.slice(1);
|
|
84
84
|
};
|
|
85
85
|
var getModuleName = (opId) => `${opId.split("_")[0].replace(/Controller$/, "")}Api`;
|
|
86
|
-
var getActionName = (opId) => toCamelCase(opId.split("_")[1] || opId);
|
|
86
|
+
var getActionName = (opId) => toCamelCase(opId.split("_")[1] || opId).replace(/V\d+$/, "");
|
|
87
87
|
var findCommonPath = (paths) => {
|
|
88
88
|
if (!paths || paths.length === 0) return "/";
|
|
89
89
|
const sorted = [...paths].sort();
|
|
@@ -94,16 +94,18 @@ var findCommonPath = (paths) => {
|
|
|
94
94
|
const prefix = first.substring(0, i);
|
|
95
95
|
return prefix.substring(0, prefix.lastIndexOf("/") + 1) || "/";
|
|
96
96
|
};
|
|
97
|
-
var generateFriendlyMessage = (fieldName,
|
|
98
|
-
const friendlyName = fieldName.replace(/([A-Z])/g, " $1").replace(/^./, (str) => str.toUpperCase());
|
|
99
|
-
return `${friendlyName} is required.`;
|
|
100
|
-
};
|
|
97
|
+
var generateFriendlyMessage = (fieldName) => `${fieldName.replace(/([A-Z])/g, " $1").replace(/^./, (str) => str.toUpperCase())} is required.`;
|
|
101
98
|
function parseSchema(name, schema, allEnums) {
|
|
102
99
|
const properties = [];
|
|
103
100
|
const enums = {};
|
|
104
101
|
if (schema.properties) {
|
|
105
102
|
for (const propName in schema.properties) {
|
|
106
103
|
const propSchema = schema.properties[propName];
|
|
104
|
+
let itemSchema;
|
|
105
|
+
if (propSchema.type === "array" && propSchema.items) {
|
|
106
|
+
const itemTypeName = `${toPascalCase(name)}${toPascalCase(propName)}Item`;
|
|
107
|
+
itemSchema = parseSchema(itemTypeName, propSchema.items, allEnums).properties[0];
|
|
108
|
+
}
|
|
107
109
|
if (propSchema.enum) {
|
|
108
110
|
const enumName = `${toPascalCase(name)}${toPascalCase(propName)}Enum`;
|
|
109
111
|
enums[propName] = propSchema.enum;
|
|
@@ -118,7 +120,7 @@ function parseSchema(name, schema, allEnums) {
|
|
|
118
120
|
example: propSchema.example,
|
|
119
121
|
enum: propSchema.enum,
|
|
120
122
|
format: propSchema.format,
|
|
121
|
-
items:
|
|
123
|
+
items: itemSchema,
|
|
122
124
|
properties: propSchema.properties ? parseSchema(`${name}${toPascalCase(propName)}`, propSchema, allEnums).properties : void 0
|
|
123
125
|
});
|
|
124
126
|
}
|
|
@@ -132,7 +134,14 @@ function parseSpecToModules(spec) {
|
|
|
132
134
|
const modulePaths = /* @__PURE__ */ new Map();
|
|
133
135
|
const registerSchema = (schema, baseName) => {
|
|
134
136
|
if (!schema) return "unknown";
|
|
135
|
-
if (schema.type === "array" && schema.items)
|
|
137
|
+
if (schema.type === "array" && schema.items) {
|
|
138
|
+
const itemSchema = schema.items;
|
|
139
|
+
const itemBaseName = `${baseName}Item`;
|
|
140
|
+
if (itemSchema.properties || itemSchema.type === "object") {
|
|
141
|
+
return `${registerSchema(itemSchema, itemBaseName)}[]`;
|
|
142
|
+
}
|
|
143
|
+
return `${itemSchema.type || "unknown"}[]`;
|
|
144
|
+
}
|
|
136
145
|
if (schema.type === "object" || schema.properties || schema.allOf || !schema.type) {
|
|
137
146
|
const typeName = toPascalCase(baseName.replace(/_v\d+(Request|Response)$/, "$1"));
|
|
138
147
|
if (!allSchemas.has(typeName)) allSchemas.set(typeName, parseSchema(typeName, schema, allEnums));
|
|
@@ -186,6 +195,9 @@ async function generateModuleFiles(module2, allSchemas, allEnums, outputDir) {
|
|
|
186
195
|
Generating module: ${import_chalk.default.bold(module2.moduleName)}`));
|
|
187
196
|
const schemasToImport = [...module2.schemas].sort();
|
|
188
197
|
const enumsToImport = [...module2.enums].sort();
|
|
198
|
+
const indexContent = [`// This file is auto-generated.
|
|
199
|
+
|
|
200
|
+
export * from './config';`];
|
|
189
201
|
let configContent = `/* eslint-disable */
|
|
190
202
|
// This file is auto-generated.
|
|
191
203
|
|
|
@@ -210,9 +222,6 @@ ${actionsValue}
|
|
|
210
222
|
`;
|
|
211
223
|
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "config.ts"), configContent.trim());
|
|
212
224
|
console.log(import_chalk.default.gray(` \u2713 config.ts`));
|
|
213
|
-
const indexContent = [`// This file is auto-generated.
|
|
214
|
-
|
|
215
|
-
export * from './config';`];
|
|
216
225
|
if (schemasToImport.length > 0) {
|
|
217
226
|
if (enumsToImport.length > 0) {
|
|
218
227
|
let enumsContent = `// This file is auto-generated.
|
|
@@ -255,8 +264,10 @@ ${prop.example ? ` * @example ${JSON.stringify(prop.example)}
|
|
|
255
264
|
`;
|
|
256
265
|
let propType = prop.type;
|
|
257
266
|
if (prop.enum) propType = `${toPascalCase(typeName)}${toPascalCase(prop.name)}Enum`;
|
|
258
|
-
else if (prop.items) propType = prop.items.
|
|
259
|
-
else if (prop.
|
|
267
|
+
else if (prop.items) propType = prop.items.name ? `${toPascalCase(prop.items.name)}[]` : `${prop.items.type || "unknown"}[]`;
|
|
268
|
+
else if (prop.type === "object") propType = prop.properties && prop.properties.length > 0 ? `{
|
|
269
|
+
${prop.properties.map((p) => ` ${p.name}${p.isRequired ? "" : "?"}: ${p.type};
|
|
270
|
+
`).join("")} }` : `Record<string, unknown>`;
|
|
260
271
|
typesContent += ` ${prop.name}${prop.isRequired ? "" : "?"}: ${propType};
|
|
261
272
|
`;
|
|
262
273
|
}
|
|
@@ -291,7 +302,7 @@ ${zodShape}
|
|
|
291
302
|
let mocksContent = `// This file is auto-generated.
|
|
292
303
|
import type { ${schemasToImport.join(", ")} } from './types';
|
|
293
304
|
`;
|
|
294
|
-
if (enumsToImport.length > 0) mocksContent += `import { ${enumsToImport.
|
|
305
|
+
if (enumsToImport.length > 0) mocksContent += `import { ${enumsToImport.join(", ")} } from './enums';
|
|
295
306
|
|
|
296
307
|
`;
|
|
297
308
|
for (const typeName of schemasToImport) {
|
|
@@ -315,20 +326,20 @@ import type { ${schemasToImport.join(", ")} } from './types';
|
|
|
315
326
|
}
|
|
316
327
|
function _propToZod(prop, parentName) {
|
|
317
328
|
let zodString = "z.any()";
|
|
318
|
-
const
|
|
329
|
+
const errorParams = { required_error: prop.description || generateFriendlyMessage(prop.name) };
|
|
319
330
|
switch (prop.type) {
|
|
320
331
|
case "string":
|
|
321
|
-
zodString = `z.string(${JSON.stringify(
|
|
322
|
-
if (prop.format === "email") zodString += `.email({ message: "
|
|
332
|
+
zodString = `z.string(${JSON.stringify(errorParams)})`;
|
|
333
|
+
if (prop.format === "email") zodString += `.email({ message: "Invalid email address" })`;
|
|
323
334
|
if (prop.format === "uuid") zodString += `.uuid()`;
|
|
324
|
-
if (prop.enum) zodString = `z.enum(${toPascalCase(parentName)}${toPascalCase(prop.name)}Enum)`;
|
|
335
|
+
if (prop.enum) zodString = `z.enum(${toPascalCase(parentName)}${toPascalCase(prop.name)}Enum, ${JSON.stringify(errorParams)})`;
|
|
325
336
|
break;
|
|
326
337
|
case "integer":
|
|
327
338
|
case "number":
|
|
328
|
-
zodString = `z.number(${JSON.stringify(
|
|
339
|
+
zodString = `z.number(${JSON.stringify(errorParams)})`;
|
|
329
340
|
break;
|
|
330
341
|
case "boolean":
|
|
331
|
-
zodString = `z.boolean(${JSON.stringify(
|
|
342
|
+
zodString = `z.boolean(${JSON.stringify(errorParams)})`;
|
|
332
343
|
break;
|
|
333
344
|
case "array":
|
|
334
345
|
zodString = `z.array(${prop.items ? _propToZod(prop.items, `${parentName}${toPascalCase(prop.name)}Item`) : "z.any()"})`;
|
|
@@ -341,7 +352,7 @@ ${prop.properties.map((p) => ` ${p.name}: ${_propToZod(p, `${parentName}${toP
|
|
|
341
352
|
zodString = shape;
|
|
342
353
|
break;
|
|
343
354
|
}
|
|
344
|
-
if (!prop.isRequired) zodString
|
|
355
|
+
if (!prop.isRequired) zodString = zodString.replace(/\(.*\)/, "").trim() + ".optional()";
|
|
345
356
|
if (prop.isNullable) zodString += ".nullable()";
|
|
346
357
|
return zodString;
|
|
347
358
|
}
|