UNPKG

@autobe/agent

Version:

AI backend server code generator

182 lines 9.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.complementPreliminaryCollection = void 0; const utils_1 = require("@autobe/utils"); const utils_2 = require("@typia/utils"); const pluralize_1 = __importDefault(require("pluralize")); const tstl_1 = require("tstl"); const AutoBeRealizeCollectorProgrammer_1 = require("../../realize/programmers/AutoBeRealizeCollectorProgrammer"); const AutoBeRealizeTransformerProgrammer_1 = require("../../realize/programmers/AutoBeRealizeTransformerProgrammer"); const complementPreliminaryCollection = (props) => { // Realize modularizations if (props.kinds.includes("realizeCollectors") === true) complementRealizeCollectors(props); if (props.kinds.includes("realizeTransformers") === true) complementRealizeTransformers(props); // Complement interface operations with prerequisites if (props.kinds.includes("interfaceOperations") === true) complementInterfaceOperations(Object.assign(Object.assign({}, props), { previous: false })); if (props.kinds.includes("previousInterfaceOperations") === true) complementInterfaceOperations(Object.assign(Object.assign({}, props), { previous: true })); // Complement DTO schemas with iterative references if (props.kinds.includes("interfaceSchemas") === true) complementInterfaceSchemas(Object.assign(Object.assign({}, props), { previous: false })); if (props.kinds.includes("previousInterfaceSchemas") === true) complementInterfaceSchemas(Object.assign(Object.assign({}, props), { previous: true })); // Complement analysis section if (props.kinds.includes("analysisSections") === true) complementAnalysisIndex(Object.assign(Object.assign({}, props), { previous: false })); if (props.kinds.includes("previousAnalysisSections") === true) complementAnalysisIndex(Object.assign(Object.assign({}, props), { previous: true })); }; exports.complementPreliminaryCollection = complementPreliminaryCollection; const complementRealizeCollectors = (props) => complementRealizeModularizations(props, props.local.realizeCollectors); const complementRealizeTransformers = (props) => complementRealizeModularizations(props, props.local.realizeTransformers); const complementRealizeModularizations = (props, metadata) => { var _a; var _b, _c; for (const { plan } of metadata) { if (props.kinds.includes("databaseSchemas")) { const model = props.all.databaseSchemas.find((m) => m.name === plan.databaseSchemaName); if (model !== undefined && props.local.databaseSchemas.find((m) => m.name === model.name) === undefined) props.local.databaseSchemas.push(model); } if (props.kinds.includes("interfaceSchemas")) { const type = props.all.interfaceSchemas[plan.dtoTypeName]; if (type !== undefined) (_a = (_b = props.local.interfaceSchemas)[_c = plan.dtoTypeName]) !== null && _a !== void 0 ? _a : (_b[_c] = type); } } }; const complementInterfaceOperations = (props) => { // collect endpoints and operations const kind = props.previous ? "previousInterfaceOperations" : "interfaceOperations"; const schemaKind = props.previous ? "previousInterfaceSchemas" : "interfaceSchemas"; const dict = new tstl_1.HashMap(props.all[kind].map((op) => new tstl_1.Pair({ method: op.method, path: op.path }, op)), utils_1.AutoBeOpenApiEndpointComparator.hashCode, utils_1.AutoBeOpenApiEndpointComparator.equals); const endpoints = new tstl_1.HashSet(utils_1.AutoBeOpenApiEndpointComparator.hashCode, utils_1.AutoBeOpenApiEndpointComparator.equals); const insert = (op) => { var _a; if (endpoints.has(op) === true) return; endpoints.insert({ method: op.method, path: op.path, }); if (props.prerequisite === true) for (const pre of (_a = op.prerequisites) !== null && _a !== void 0 ? _a : []) insert(dict.get(pre.endpoint)); }; for (const op of props.local[kind]) insert(op); // remake local operations props.local[kind].splice(0, props.local[kind].length); props.local[kind].push(...Array.from(endpoints).map((ep) => dict.get(ep))); // add DTO schemas used in operations if (props.kinds.includes(schemaKind) === true) { const typeNames = new Set(); for (const op of props.local[kind]) { if (op.requestBody !== null) typeNames.add(op.requestBody.typeName); if (op.responseBody !== null) typeNames.add(op.responseBody.typeName); } for (const key of typeNames) if (props.local[schemaKind][key] === undefined && props.all[schemaKind][key] !== undefined) props.local[schemaKind][key] = props.all[schemaKind][key]; } }; const complementInterfaceSchemas = (props) => { // link modularizations if (props.previous === false && props.kinds.includes("realizeCollectors") === true) { const creators = Object.keys(props.local.interfaceSchemas).filter(AutoBeRealizeCollectorProgrammer_1.AutoBeRealizeCollectorProgrammer.filter); for (const key of creators) { const found = props.all.realizeCollectors.find((t) => t.plan.dtoTypeName === key); if (found !== undefined) props.local.realizeCollectors.push(found); } } if (props.previous === false && props.kinds.includes("realizeTransformers") === true) { const unique = new Set(); for (const key of Object.keys(props.local.interfaceSchemas)) { if (key.startsWith("IPage") && key.startsWith("IPage.") === false) unique.add(key.replace("IPage", "")); else if (key.endsWith(".IAuthorized")) unique.add(key.replace(".IAuthorized", "")); else if (AutoBeRealizeTransformerProgrammer_1.AutoBeRealizeTransformerProgrammer.filter({ schemas: props.all.interfaceSchemas, key, }) === true) unique.add(key); } for (const key of unique) { const found = props.all.realizeTransformers.find((t) => t.plan.dtoTypeName === key); if (found !== undefined) props.local.realizeTransformers.push(found); } } // link dependencies const kind = props.previous === true ? "previousInterfaceSchemas" : "interfaceSchemas"; const prismaKind = props.previous === true ? "previousDatabaseSchemas" : "databaseSchemas"; const unique = new Set(Object.keys(props.local[kind])); for (const dto of Object.values(props.local[kind])) utils_2.OpenApiTypeChecker.visit({ components: { schemas: props.all[kind], }, schema: dto, closure: (next) => { if (utils_2.OpenApiTypeChecker.isReference(next)) unique.add(next.$ref.split("/").pop()); }, }); for (const key of unique) if (props.local[kind][key] === undefined && props.all[kind][key] !== undefined) props.local[kind][key] = props.all[kind][key]; // load related database schemas if (props.kinds.includes(prismaKind) === true) { const prisma = new Set(); for (const [key, value] of Object.entries(props.local[kind])) { utils_2.OpenApiTypeChecker.visit({ components: { schemas: props.all[kind], }, schema: value, closure: (next) => { if (utils_2.OpenApiTypeChecker.isObject(next) === false) return; const name = next["x-autobe-database-schema"]; if (name !== null && name !== undefined && props.all[prismaKind].find((m) => m.name === name) !== undefined) prisma.add(name); }, }); const candidate = (0, pluralize_1.default)(utils_2.NamingConvention.snake(key)); if (props.all[prismaKind].find((m) => m.name === candidate) !== undefined) prisma.add(candidate); } for (const name of prisma) { if (props.local[prismaKind].find((m) => m.name === name) === undefined) props.local[prismaKind].push(props.all[prismaKind].find((m) => m.name === name)); } } }; const complementAnalysisIndex = (props) => { const kind = props.previous === true ? "previousAnalysisSections" : "analysisSections"; const all = props.all[kind]; if (all.length === 0) return; const first = all[0]; if (props.local[kind].includes(first) === false) props.local[kind].unshift(first); }; //# sourceMappingURL=complementPreliminaryCollection.js.map