@autobe/agent
Version:
AI backend server code generator
99 lines • 4.87 kB
JavaScript
;
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 openapi_1 = require("@samchon/openapi");
const pluralize_1 = __importDefault(require("pluralize"));
const tstl_1 = require("tstl");
const NamingConvention_1 = require("typia/lib/utils/NamingConvention");
const complementPreliminaryCollection = (props) => {
//----
// Complement interface operations with prerequisites
//----
if (props.kinds.includes("interfaceOperations") === true) {
const dict = new tstl_1.HashMap(props.all.interfaceOperations.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,
});
for (const pre of (_a = op.prerequisites) !== null && _a !== void 0 ? _a : []) {
insert(dict.get(pre.endpoint));
}
};
for (const op of props.local.interfaceOperations)
insert(op);
props.local.interfaceOperations.splice(0, props.local.interfaceOperations.length);
props.local.interfaceOperations.push(...Array.from(endpoints).map((ep) => dict.get(ep)));
// Add DTO schemas used in operations
if (props.kinds.includes("interfaceSchemas") === true) {
const typeNames = new Set();
for (const op of props.local.interfaceOperations) {
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.interfaceSchemas[key] === undefined)
props.local.interfaceSchemas[key] = props.all.interfaceSchemas[key];
}
}
//----
// Complement DTO schemas with iterative references
//----
if (props.kinds.includes("interfaceSchemas") === true) {
const unique = new Set(Object.keys(props.local.interfaceSchemas));
for (const dto of Object.values(props.local.interfaceSchemas))
openapi_1.OpenApiTypeChecker.visit({
components: {
schemas: props.all.interfaceSchemas,
},
schema: dto,
closure: (next) => {
if (openapi_1.OpenApiTypeChecker.isReference(next))
unique.add(next.$ref.split("/").pop());
},
});
for (const key of unique)
if (props.local.interfaceSchemas[key] === undefined)
props.local.interfaceSchemas[key] = props.all.interfaceSchemas[key];
if (props.kinds.includes("prismaSchemas") === true) {
const prisma = new Set();
for (const [key, value] of Object.entries(props.local.interfaceSchemas)) {
openapi_1.OpenApiTypeChecker.visit({
components: {
schemas: props.all.interfaceSchemas,
},
schema: value,
closure: (next) => {
if (openapi_1.OpenApiTypeChecker.isObject(next) === false)
return;
const name = next["x-autobe-prisma-schema"];
if (name !== null &&
name !== undefined &&
props.all.prismaSchemas.find((m) => m.name === name) !== undefined)
prisma.add(name);
},
});
const candidate = (0, pluralize_1.default)(NamingConvention_1.NamingConvention.snake(key));
if (props.all.prismaSchemas.find((m) => m.name === candidate) !==
undefined)
prisma.add(candidate);
}
for (const name of prisma) {
if (props.local.prismaSchemas.find((m) => m.name === name) === undefined)
props.local.prismaSchemas.push(props.all.prismaSchemas.find((m) => m.name === name));
}
}
}
};
exports.complementPreliminaryCollection = complementPreliminaryCollection;
//# sourceMappingURL=complementPreliminaryCollection.js.map