UNPKG

@autobe/compiler

Version:

AI backend server code generator

80 lines (75 loc) 2.85 kB
import { AutoBeOpenApi, IAutoBeInterfaceCompiler } from "@autobe/interface"; import { invertOpenApiDocument, transformOpenApiDocument } from "@autobe/utils"; import { NestiaMigrateApplication } from "@nestia/migrate"; import { OpenApi } from "@samchon/openapi"; import { ArrayUtil } from "../utils/ArrayUtil"; import { FilePrinter } from "../utils/FilePrinter"; /** * Custom Interface compiler that handles API specification and NestJS * application generation. * * This compiler transforms validated {@link AutoBeOpenApi.IDocument} AST * structures into comprehensive NestJS projects through a sophisticated * multi-stage transformation pipeline. The Interface compiler bridges the gap * between database design and application implementation, ensuring perfect * alignment with business requirements and database schemas. * * The compiler leverages NestiaMigrateApplication for robust NestJS project * generation and HttpMigration for bidirectional conversion between AutoBE AST * and standard OpenAPI formats. All generated TypeScript code is automatically * formatted with Prettier and organized with proper import sorting for * production-ready quality. * * Key capabilities include generating complete NestJS applications with * controllers, DTOs, client SDKs, and E2E test scaffolds, all enhanced with * keyworded parameter optimization for AI consumption and comprehensive * documentation derived from AST descriptions. * * @author Samchon */ export class AutoBeInterfaceCompiler implements IAutoBeInterfaceCompiler { public async write( document: AutoBeOpenApi.IDocument, exclude: string[], ): Promise<Record<string, string>> { const migrate: NestiaMigrateApplication = new NestiaMigrateApplication( transformOpenApiDocument(document), ); const files: Record<string, string> = migrate.nest({ keyword: true, simulate: true, e2e: true, author: { tag: "autobe", value: "Generated by AutoBE - https://github.com/wrtnlabs/autobe", }, }); return Object.fromEntries([ ...(await ArrayUtil.asyncMap( Object.entries(files).filter( ([key, _]) => exclude.includes(key) === false, ), async ([key, value]) => [ key, key.endsWith(".ts") && key.endsWith(".d.ts") === false ? await FilePrinter.beautify(value) : value, ], )), [ "packages/api/swagger.json", JSON.stringify(migrate.getData().document(), null, 2), ], ]); } public async transform( document: AutoBeOpenApi.IDocument, ): Promise<OpenApi.IDocument> { return transformOpenApiDocument(document); } public async invert( document: OpenApi.IDocument, ): Promise<AutoBeOpenApi.IDocument> { return invertOpenApiDocument(document); } }