api-core-lib 12.0.79 → 12.0.81
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 +129 -15
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -213,19 +213,24 @@ function parseSpecToModules(spec) {
|
|
|
213
213
|
const allEnums = /* @__PURE__ */ new Map();
|
|
214
214
|
const modulePaths = /* @__PURE__ */ new Map();
|
|
215
215
|
const registerSchema = (schema, baseName) => {
|
|
216
|
-
if (!schema) return "
|
|
216
|
+
if (!schema) return "Record<string, any>";
|
|
217
217
|
if (schema.type === "array" && schema.items) {
|
|
218
218
|
const itemSchema = schema.items;
|
|
219
219
|
const itemBaseName = `${baseName}Item`;
|
|
220
|
-
if (itemSchema.properties || itemSchema.type === "object")
|
|
221
|
-
|
|
220
|
+
if (itemSchema.properties || itemSchema.type === "object") {
|
|
221
|
+
return `${registerSchema(itemSchema, itemBaseName)}[]`;
|
|
222
|
+
}
|
|
223
|
+
const primitiveType = itemSchema.type === "integer" ? "number" : itemSchema.type || "any";
|
|
224
|
+
return `${primitiveType}[]`;
|
|
222
225
|
}
|
|
223
226
|
if (schema.type === "object" || schema.properties || schema.allOf || !schema.type) {
|
|
224
227
|
const typeName = toPascalCase(baseName.replace(/_v\d+(Request|Response)$/, "$1"));
|
|
225
|
-
if (!allSchemas.has(typeName))
|
|
228
|
+
if (!allSchemas.has(typeName)) {
|
|
229
|
+
allSchemas.set(typeName, parseSchema(typeName, schema, allEnums));
|
|
230
|
+
}
|
|
226
231
|
return typeName;
|
|
227
232
|
}
|
|
228
|
-
return schema.type === "integer" ? "number" : schema.type || "
|
|
233
|
+
return schema.type === "integer" ? "number" : schema.type || "any";
|
|
229
234
|
};
|
|
230
235
|
for (const apiPath in spec.paths) {
|
|
231
236
|
const pathItem = spec.paths[apiPath];
|
|
@@ -278,10 +283,79 @@ async function generateModuleFiles(module2, allSchemas, allEnums, outputDir) {
|
|
|
278
283
|
const indexContent = [`// This file is auto-generated. Do not edit directly.
|
|
279
284
|
|
|
280
285
|
export * from './config';`];
|
|
286
|
+
let configContent = `// This file is auto-generated. Do not edit directly.
|
|
287
|
+
|
|
288
|
+
import type { ApiModuleConfig, ActionConfigModule } from 'api-core-lib';
|
|
289
|
+
`;
|
|
290
|
+
if (schemasToImport.length > 0) configContent += `import type { ${schemasToImport.join(", ")} } from './types';
|
|
291
|
+
`;
|
|
292
|
+
const actionsType = Object.values(module2.actions).map((a) => ` ${a.name}: ActionConfigModule<${a.inputType}, ${a.outputType}>;`).join("\n");
|
|
293
|
+
const actionsValue = Object.values(module2.actions).map((a) => {
|
|
294
|
+
const { inputType, outputType, name, ...c } = a;
|
|
295
|
+
return ` ${name}: ${JSON.stringify(c, null, 2).replace(/\n/g, "\n ")}`;
|
|
296
|
+
}).join(",\n");
|
|
297
|
+
configContent += `
|
|
298
|
+
export const ${module2.moduleName}Module: ApiModuleConfig<{
|
|
299
|
+
${actionsType}
|
|
300
|
+
}> = {
|
|
301
|
+
baseEndpoint: '${module2.baseEndpoint}',
|
|
302
|
+
actions: {
|
|
303
|
+
${actionsValue}
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
`;
|
|
307
|
+
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "config.ts"), configContent.trim());
|
|
308
|
+
console.log(import_chalk.default.gray(` \u2713 config.ts`));
|
|
281
309
|
if (schemasToImport.length > 0) {
|
|
310
|
+
if (enumsToImport.length > 0) {
|
|
311
|
+
let enumsContent = `// This file is auto-generated. Do not edit directly.
|
|
312
|
+
|
|
313
|
+
`;
|
|
314
|
+
for (const enumName of enumsToImport) {
|
|
315
|
+
const enumDef = allEnums.get(enumName);
|
|
316
|
+
if (enumDef) {
|
|
317
|
+
enumsContent += `export const ${enumName} = ${JSON.stringify(enumDef.values)} as const;
|
|
318
|
+
`;
|
|
319
|
+
enumsContent += `export type ${enumName} = typeof ${enumName}[number];
|
|
320
|
+
|
|
321
|
+
`;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "enums.ts"), enumsContent);
|
|
325
|
+
console.log(import_chalk.default.gray(` \u2713 enums.ts`));
|
|
326
|
+
indexContent.push(`export * from './enums';`);
|
|
327
|
+
}
|
|
328
|
+
let typesContent = `// This file is auto-generated. Do not edit directly.
|
|
329
|
+
|
|
330
|
+
`;
|
|
331
|
+
if (enumsToImport.length > 0) typesContent += `import type { ${enumsToImport.join(", ")} } from './enums';
|
|
332
|
+
|
|
333
|
+
`;
|
|
334
|
+
for (const typeName of schemasToImport) {
|
|
335
|
+
const parsedSchema = allSchemas.get(typeName);
|
|
336
|
+
if (parsedSchema) {
|
|
337
|
+
if (parsedSchema.description) typesContent += `/** ${parsedSchema.description} */
|
|
338
|
+
`;
|
|
339
|
+
typesContent += `export interface ${typeName} {
|
|
340
|
+
`;
|
|
341
|
+
for (const prop of parsedSchema.properties) {
|
|
342
|
+
if (prop.description) typesContent += ` /** ${prop.description} */
|
|
343
|
+
`;
|
|
344
|
+
let propType = prop.enumName || (prop.items ? `${prop.items.type || "any"}[]` : prop.type);
|
|
345
|
+
if (propType === "integer") propType = "number";
|
|
346
|
+
if (propType === "object") propType = "Record<string, any>";
|
|
347
|
+
typesContent += ` ${prop.name}${prop.isRequired ? "" : "?"}: ${propType};
|
|
348
|
+
`;
|
|
349
|
+
}
|
|
350
|
+
typesContent += `}
|
|
351
|
+
|
|
352
|
+
`;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "types.ts"), typesContent);
|
|
356
|
+
console.log(import_chalk.default.gray(` \u2713 types.ts`));
|
|
282
357
|
indexContent.push(`export * from './types';`);
|
|
283
|
-
|
|
284
|
-
let validationContent = `// This file is auto-generated by the API generator. Do not edit directly.
|
|
358
|
+
let validationContent = `// This file is auto-generated. Do not edit directly.
|
|
285
359
|
|
|
286
360
|
import { z } from 'zod';
|
|
287
361
|
`;
|
|
@@ -294,14 +368,7 @@ import { z } from 'zod';
|
|
|
294
368
|
if (parsedSchema) {
|
|
295
369
|
const zodShape = parsedSchema.properties.map((p) => ` ${p.name}: ${_propToZod(p)}`).join(",\n");
|
|
296
370
|
validationContent += `
|
|
297
|
-
/**
|
|
298
|
-
* Zod schema for {@link ${typeName}}.
|
|
299
|
-
`;
|
|
300
|
-
if (parsedSchema.description) {
|
|
301
|
-
validationContent += ` * @description ${parsedSchema.description.replace(/\*\//g, "*\\/")}
|
|
302
|
-
`;
|
|
303
|
-
}
|
|
304
|
-
validationContent += ` */
|
|
371
|
+
/** Zod schema for {@link ${typeName}}. */
|
|
305
372
|
`;
|
|
306
373
|
validationContent += `export const ${typeName}Schema = z.object({
|
|
307
374
|
${zodShape}
|
|
@@ -315,10 +382,57 @@ ${zodShape}
|
|
|
315
382
|
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "validation.ts"), validationContent);
|
|
316
383
|
console.log(import_chalk.default.gray(` \u2713 validation.ts`));
|
|
317
384
|
indexContent.push(`export * from './validation';`);
|
|
385
|
+
let mocksContent = `// This file is auto-generated. Do not edit directly.
|
|
386
|
+
import type { ${schemasToImport.join(", ")} } from './types';
|
|
387
|
+
`;
|
|
388
|
+
if (enumsToImport.length > 0) mocksContent += `import { ${enumsToImport.join(", ")} } from './enums';
|
|
389
|
+
|
|
390
|
+
`;
|
|
391
|
+
for (const typeName of schemasToImport) {
|
|
392
|
+
const parsedSchema = allSchemas.get(typeName);
|
|
393
|
+
if (parsedSchema) {
|
|
394
|
+
let mockObject = {};
|
|
395
|
+
parsedSchema.properties.forEach((p) => {
|
|
396
|
+
mockObject[p.name] = _propToMock(p);
|
|
397
|
+
});
|
|
398
|
+
mocksContent += `export const mock${typeName}: ${typeName} = ${JSON.stringify(mockObject, null, 2)};
|
|
399
|
+
|
|
400
|
+
`;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "mocks.ts"), mocksContent);
|
|
404
|
+
console.log(import_chalk.default.gray(` \u2713 mocks.ts`));
|
|
405
|
+
indexContent.push(`export * from './mocks';`);
|
|
318
406
|
}
|
|
319
407
|
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "index.ts"), indexContent.join("\n"));
|
|
320
408
|
console.log(import_chalk.default.gray(` \u2713 index.ts`));
|
|
321
409
|
}
|
|
410
|
+
function _propToMock(prop) {
|
|
411
|
+
if (prop.example) return prop.example;
|
|
412
|
+
if (prop.name.match(/image|avatar|logo|url/i)) return "https://via.placeholder.com/150";
|
|
413
|
+
if (prop.enum) return prop.enum[0];
|
|
414
|
+
switch (prop.type) {
|
|
415
|
+
case "string":
|
|
416
|
+
if (prop.format === "email") return "test@example.com";
|
|
417
|
+
if (prop.format === "uuid") return "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11";
|
|
418
|
+
return `Mock ${toPascalCase(prop.name)}`;
|
|
419
|
+
case "integer":
|
|
420
|
+
case "number":
|
|
421
|
+
return 1;
|
|
422
|
+
case "boolean":
|
|
423
|
+
return true;
|
|
424
|
+
case "array":
|
|
425
|
+
return prop.items ? [_propToMock(prop.items)] : [];
|
|
426
|
+
case "object":
|
|
427
|
+
const mock = {};
|
|
428
|
+
if (prop.properties) prop.properties.forEach((p) => {
|
|
429
|
+
mock[p.name] = _propToMock(p);
|
|
430
|
+
});
|
|
431
|
+
return mock;
|
|
432
|
+
default:
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
322
436
|
async function runGenerator(options) {
|
|
323
437
|
console.log(import_chalk.default.cyan.bold("\u{1F680} Starting API Development Platform Generator (Sapphire Edition)..."));
|
|
324
438
|
import_dotenv.default.config({ path: options.envPath });
|