@autobe/agent
Version:
AI backend server code generator
137 lines (131 loc) • 7.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeInterfaceSchemaRefineProgrammer = void 0;
const utils_1 = require("@autobe/utils");
const utils_2 = require("@typia/utils");
const typia_1 = __importDefault(require("typia"));
const AutoBeJsonSchemaFactory_1 = require("../utils/AutoBeJsonSchemaFactory");
const AutoBeInterfaceSchemaProgrammer_1 = require("./AutoBeInterfaceSchemaProgrammer");
const AutoBeInterfaceSchemaPropertyReviseProgrammer_1 = require("./AutoBeInterfaceSchemaPropertyReviseProgrammer");
var AutoBeInterfaceSchemaRefineProgrammer;
(function (AutoBeInterfaceSchemaRefineProgrammer) {
AutoBeInterfaceSchemaRefineProgrammer.fixApplication = (props) => {
AutoBeInterfaceSchemaProgrammer_1.AutoBeInterfaceSchemaProgrammer.fixApplication({
application: props.application,
everyModels: props.everyModels,
});
const $defs = props.application.functions[0].parameters.$defs;
const fix = (next) => {
if (next === undefined)
return;
else if (utils_2.LlmTypeChecker.isObject(next) === false)
return;
const key = next.properties.key;
if (key === undefined || utils_2.LlmTypeChecker.isString(key) === false)
return;
key.enum = Object.keys(props.schema.properties);
};
fix($defs["AutoBeInterfaceSchemaPropertyDepict"]);
fix($defs["AutoBeInterfaceSchemaPropertyUpdate"]);
fix($defs["AutoBeInterfaceSchemaPropertyErase"]);
};
AutoBeInterfaceSchemaRefineProgrammer.validate = (props) => {
var _a;
// validate database schema existence
if (props.databaseSchema !== null &&
props.everyModels.find((m) => m.name === props.databaseSchema) ===
undefined)
props.errors.push({
path: `${props.path}.databaseSchema`,
expected: props.everyModels
.map((m) => JSON.stringify(m.name))
.join(" | "),
value: props.databaseSchema,
description: utils_1.StringUtil.trim `
You've referenced a non-existing database schema name
${JSON.stringify(props.databaseSchema)} in "databaseSchema"
property. Make sure that the referenced database schema name
exists in your database schema files.
Never assume non-existing models. This is not recommendation,
but an instruction you must follow. Never repeat the same
value again. You have to choose one of below:
**Option 1: Reference an existing database schema**
${props.everyModels.map((m) => `- ${m.name}`).join("\n")}
**Option 2: Set to null (for DTOs with no database reference)**
If this DTO represents pure computed/statistical data or logic-only
structures that have no direct relationship to any database table,
set "databaseSchema" to null. In this case, all properties in the
object type must also have
"AutoBeInterfaceSchemaPropertyRefine.databaseSchemaProperty"
(except "AutoBeInterfaceSchemaPropertyErase" type) set to null.
`,
});
// validate refines detaily
AutoBeInterfaceSchemaPropertyReviseProgrammer_1.AutoBeInterfaceSchemaPropertyReviseProgrammer.validate({
// config
path: props.path,
errors: props.errors,
unionTypeName: "AutoBeInterfaceSchemaPropertyRefine",
noModelDescription: utils_1.StringUtil.trim `
You have defined "databaseSchemaProperty" property referencing
a database schema property, but its parent schema (object type)
does not reference any database schema.
To reference a database schema property, you have to configure
"IAutoBeInterfaceSchemaRefineApplication.IComplete.databaseSchema"
property with a valid database schema name.
If not, set this "databaseSchemaProperty" property to null value
at the next time, and then depict what this property is for
in the "specification" property.
Note that, this is not a recommendation,
but an instruction you must obey.
`,
// database
everyModels: props.everyModels,
model: props.databaseSchema !== null
? ((_a = props.everyModels.find((m) => m.name === props.databaseSchema)) !== null && _a !== void 0 ? _a : null)
: null,
// interface
typeName: props.typeName,
schema: props.schema,
revises: props.revises,
excludes: props.excludes,
});
};
AutoBeInterfaceSchemaRefineProgrammer.execute = (props) => {
var _a;
const result = Object.assign(Object.assign({}, props.schema), { "x-autobe-database-schema": props.databaseSchema, "x-autobe-specification": props.specification, description: props.description, properties: {}, required: [] });
const setRequired = (key) => {
if (result.required.includes(key) === false)
result.required.push(key);
};
for (const refine of props.revises)
if (refine.type === "depict") {
// Add documentation to existing property
result.properties[refine.key] = Object.assign(Object.assign({}, props.schema.properties[refine.key]), { description: refine.description, "x-autobe-database-schema-property": refine.databaseSchemaProperty, "x-autobe-specification": refine.specification });
if (props.schema.required.includes(refine.key))
setRequired(refine.key);
}
else if (refine.type === "create") {
// Create new property with documentation
result.properties[refine.key] = Object.assign(Object.assign({}, AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.fixSchema(refine.schema)), { description: refine.description, "x-autobe-specification": refine.specification, "x-autobe-database-schema-property": refine.databaseSchemaProperty });
if (refine.required)
setRequired(refine.key);
}
else if (refine.type === "update") {
// Update existing property type and documentation
const newKey = (_a = refine.newKey) !== null && _a !== void 0 ? _a : refine.key;
result.properties[newKey] = Object.assign(Object.assign({}, AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.fixSchema(refine.schema)), { description: refine.description, "x-autobe-specification": refine.specification, "x-autobe-database-schema-property": refine.databaseSchemaProperty });
if (refine.required)
setRequired(newKey);
}
else if (refine.type === "erase")
continue;
else
refine;
return result;
};
})(AutoBeInterfaceSchemaRefineProgrammer || (exports.AutoBeInterfaceSchemaRefineProgrammer = AutoBeInterfaceSchemaRefineProgrammer = {}));
//# sourceMappingURL=AutoBeInterfaceSchemaRefineProgrammer.js.map