@autobe/agent
Version:
AI backend server code generator
84 lines • 3.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonSchemaNamingConvention = void 0;
const utils_1 = require("@autobe/utils");
const openapi_1 = require("@samchon/openapi");
var JsonSchemaNamingConvention;
(function (JsonSchemaNamingConvention) {
JsonSchemaNamingConvention.operations = (operations) => {
const convention = new Convention();
for (const op of operations) {
if (op.requestBody !== null)
convention.emplace(op.requestBody.typeName, (v) => {
if (op.requestBody !== null)
op.requestBody.typeName = v;
});
if (op.responseBody !== null)
convention.emplace(op.responseBody.typeName, (v) => {
if (op.responseBody !== null)
op.responseBody.typeName = v;
});
}
convention.execute();
};
JsonSchemaNamingConvention.schemas = (operations, ...componentSchemas) => {
const convention = new Convention();
for (const op of operations) {
if (op.requestBody !== null)
convention.emplace(op.requestBody.typeName, (v) => {
if (op.requestBody !== null)
op.requestBody.typeName = v;
});
if (op.responseBody !== null)
convention.emplace(op.responseBody.typeName, (v) => {
if (op.responseBody !== null)
op.responseBody.typeName = v;
});
}
for (const schema of componentSchemas) {
for (const key of Object.keys(schema)) {
convention.emplace(key, (v) => {
if (key === v)
return;
schema[v] = schema[key];
delete schema[key];
});
for (const value of Object.values(schema))
openapi_1.OpenApiTypeChecker.visit({
components: { schemas: schema },
schema: value,
closure: (s) => {
if (openapi_1.OpenApiTypeChecker.isReference(s) === false)
return;
const key = s.$ref.split("/").pop();
convention.emplace(key, (v) => (s.$ref = `#/components/schemas/${v}`));
},
});
}
convention.execute();
}
};
})(JsonSchemaNamingConvention || (exports.JsonSchemaNamingConvention = JsonSchemaNamingConvention = {}));
const countCapitalLetters = (str) => (str.match(/[A-Z]/g) || []).length;
class Convention {
constructor() {
this.dict = new Map();
this.closures = [];
}
emplace(key, setter) {
this.closures.push({ value: key, setter });
const top = key.split(".")[0];
utils_1.MapUtil.take(this.dict, top.toLowerCase(), () => new Set()).add(top);
}
execute() {
const mapping = new Map();
for (const [key, value] of this.dict.entries())
mapping.set(key, Array.from(value).sort((a, b) => countCapitalLetters(b) - countCapitalLetters(a))[0]);
for (const closure of this.closures) {
const elements = closure.value.split(".");
const value = mapping.get(elements[0].toLowerCase());
closure.setter([value, ...elements.slice(1)].join("."));
}
}
}
//# sourceMappingURL=JsonSchemaNamingConvention.js.map