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