@autobe/agent
Version:
AI backend server code generator
185 lines (183 loc) • 10.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeInterfaceSchemaReviewProgrammer = 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 AutoBeJsonSchemaValidator_1 = require("../utils/AutoBeJsonSchemaValidator");
const AutoBeInterfaceSchemaProgrammer_1 = require("./AutoBeInterfaceSchemaProgrammer");
const AutoBeInterfaceSchemaPropertyReviseProgrammer_1 = require("./AutoBeInterfaceSchemaPropertyReviseProgrammer");
var AutoBeInterfaceSchemaReviewProgrammer;
(function (AutoBeInterfaceSchemaReviewProgrammer) {
AutoBeInterfaceSchemaReviewProgrammer.filter = (key, value) => AutoBeJsonSchemaValidator_1.AutoBeJsonSchemaValidator.isPreset(key) === false &&
utils_1.AutoBeOpenApiTypeChecker.isObject(value);
AutoBeInterfaceSchemaReviewProgrammer.filterSecurity = (props) => {
const symbols = ["IAuthorized", "IJoin", "ILogin"];
return props.typeName.includes(".")
? symbols.some((s) => props.typeName.endsWith(`.${s}`))
: symbols.some((s) => props.document.components.schemas[`${props.typeName}.${s}`] !==
undefined);
};
AutoBeInterfaceSchemaReviewProgrammer.fixApplication = (props) => {
AutoBeInterfaceSchemaProgrammer_1.AutoBeInterfaceSchemaProgrammer.fixApplication({
application: props.application,
everyModels: props.everyModels,
});
const model = props.schema["x-autobe-database-schema"] !== undefined
? props.everyModels.find((m) => m.name === props.schema["x-autobe-database-schema"])
: undefined;
const $defs = props.application.functions[0].parameters.$defs;
const fix = (next, fixKey = true) => {
if (next === undefined)
return;
else if (utils_2.LlmTypeChecker.isObject(next) === false)
return;
if (fixKey === true) {
const key = next.properties.key;
if (key === undefined || utils_2.LlmTypeChecker.isString(key) === false)
return;
key.enum = Object.keys(props.schema.properties);
}
if (model !== undefined) {
const key = next.properties.databaseSchemaProperty;
if (key === undefined || utils_2.LlmTypeChecker.isString(key) === false)
return;
key.enum = AutoBeInterfaceSchemaProgrammer_1.AutoBeInterfaceSchemaProgrammer.getDatabaseSchemaProperties({
everyModels: props.everyModels,
model,
}).map((p) => p.key);
}
};
fix($defs["AutoBeInterfaceSchemaPropertyCreate"], false);
fix($defs["AutoBeInterfaceSchemaPropertyErase"]);
fix($defs["AutoBeInterfaceSchemaPropertyNullish"]);
fix($defs["AutoBeInterfaceSchemaPropertyDepict"]);
fix($defs["AutoBeInterfaceSchemaPropertyUpdate"]);
fix($defs["AutoBeInterfaceSchemaPropertyKeep"]);
fix($defs["AutoBeInterfaceSchemaPropertyExclude"]);
};
AutoBeInterfaceSchemaReviewProgrammer.validate = (props) => {
var _a;
// validate revises detaily
AutoBeInterfaceSchemaPropertyReviseProgrammer_1.AutoBeInterfaceSchemaPropertyReviseProgrammer.validate({
// config
path: props.path,
errors: props.errors,
unionTypeName: "AutoBeInterfaceSchemaPropertyRevise",
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 make it correct, you have to change the "databaseSchemaProperty"
to be \`null\` 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. I repeat that, change the value to be \`null\`.
`,
// database
everyModels: props.everyModels,
model: props.schema["x-autobe-database-schema"]
? ((_a = props.everyModels.find((m) => m.name === props.schema["x-autobe-database-schema"])) !== null && _a !== void 0 ? _a : null)
: null,
// interface
typeName: props.typeName,
schema: props.schema,
excludes: props.excludes,
revises: props.revises,
});
};
AutoBeInterfaceSchemaReviewProgrammer.execute = (props) => {
var _a;
const result = Object.assign(Object.assign({}, props.schema), { properties: {}, required: [] });
const setRequired = (key) => {
if (result.required.includes(key) === false)
result.required.push(key);
};
for (const revise of props.revises)
if (revise.type === "create") {
// create new property
result.properties[revise.key] = Object.assign(Object.assign({}, AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.fixSchema(revise.schema)), { description: revise.description, "x-autobe-specification": revise.specification, "x-autobe-database-schema-property": revise.databaseSchemaProperty });
if (revise.required)
setRequired(revise.key);
}
else if (revise.type === "update") {
// update existing property
const newKey = (_a = revise.newKey) !== null && _a !== void 0 ? _a : revise.key;
result.properties[newKey] = Object.assign(Object.assign({}, AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.fixSchema(revise.schema)), { description: revise.description, "x-autobe-specification": revise.specification, "x-autobe-database-schema-property": revise.databaseSchemaProperty });
if (revise.required === true)
setRequired(newKey);
}
else if (revise.type === "keep") {
// keep original property with updated databaseSchemaProperty
result.properties[revise.key] = Object.assign(Object.assign({}, JSON.parse(JSON.stringify(props.schema.properties[revise.key]))), { "x-autobe-database-schema-property": revise.databaseSchemaProperty });
if (props.schema.required.includes(revise.key))
setRequired(revise.key);
}
else if (revise.type === "nullish") {
// change nullable or required status only
nullish({
schema: result,
property: props.schema.properties[revise.key],
revise: revise,
});
}
else if (revise.type === "depict") {
result.properties[revise.key] = Object.assign(Object.assign({}, props.schema.properties[revise.key]), { description: revise.description, "x-autobe-database-schema-property": revise.databaseSchemaProperty, "x-autobe-specification": revise.specification });
if (props.schema.required.includes(revise.key))
setRequired(revise.key);
}
else if (revise.type === "erase")
continue;
else
revise;
return result;
};
const nullish = (props) => {
let cloned = JSON.parse(JSON.stringify(props.property));
if (props.revise.nullable === true) {
if (utils_1.AutoBeOpenApiTypeChecker.isOneOf(cloned)) {
if (cloned.oneOf.some((item) => utils_1.AutoBeOpenApiTypeChecker.isNull(item)) ===
false)
cloned.oneOf.push({ type: "null" });
}
else if (utils_1.AutoBeOpenApiTypeChecker.isNull(cloned) === false)
cloned = {
"x-autobe-database-schema-property": cloned["x-autobe-database-schema-property"],
"x-autobe-specification": cloned["x-autobe-specification"],
description: cloned.description,
oneOf: [
Object.assign(Object.assign({}, cloned), {
"x-autobe-specification": undefined,
"x-autobe-database-schema-property": undefined,
description: undefined,
}),
{ type: "null" },
],
};
}
else if (props.revise.nullable === false) {
if (utils_1.AutoBeOpenApiTypeChecker.isOneOf(cloned)) {
cloned.oneOf = cloned.oneOf.filter((value) => utils_1.AutoBeOpenApiTypeChecker.isNull(value) === false);
if (cloned.oneOf.length === 1)
cloned = Object.assign(Object.assign({}, cloned.oneOf[0]), { "x-autobe-specification": cloned["x-autobe-specification"], "x-autobe-database-schema-property": cloned["x-autobe-database-schema-property"], description: cloned.description });
}
}
// Update databaseSchemaProperty
cloned["x-autobe-database-schema-property"] =
props.revise.databaseSchemaProperty;
// Update description: null preserves existing, string replaces it
if (props.revise.description !== null)
cloned.description = props.revise.description;
if (props.revise.specification !== null)
cloned["x-autobe-specification"] = props.revise.specification;
// do assign
props.schema.properties[props.revise.key] = cloned;
if (props.revise.required === true)
props.schema.required.push(props.revise.key);
};
})(AutoBeInterfaceSchemaReviewProgrammer || (exports.AutoBeInterfaceSchemaReviewProgrammer = AutoBeInterfaceSchemaReviewProgrammer = {}));
//# sourceMappingURL=AutoBeInterfaceSchemaReviewProgrammer.js.map