UNPKG

@autobe/agent

Version:

AI backend server code generator

72 lines 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoBeJsonSchemaNamingConvention = void 0; const utils_1 = require("@autobe/utils"); const pluralize_1 = require("pluralize"); var AutoBeJsonSchemaNamingConvention; (function (AutoBeJsonSchemaNamingConvention) { AutoBeJsonSchemaNamingConvention.normalize = (props) => { const convention = new Convention(); for (const op of props.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 [key, value] of Object.entries(props.collection.schemas)) { convention.emplace(key, (newKey) => { if (key === newKey) return; props.collection.set(newKey, value); props.collection.delete(key); }); utils_1.AutoBeOpenApiTypeChecker.visit({ components: { schemas: props.collection.schemas, authorizations: [], }, schema: value, closure: (s) => { if (utils_1.AutoBeOpenApiTypeChecker.isReference(s) === false) return; const key = s.$ref.split("/").pop(); convention.emplace(key, (v) => (s.$ref = `#/components/schemas/${v}`)); }, }); } convention.execute(); }; })(AutoBeJsonSchemaNamingConvention || (exports.AutoBeJsonSchemaNamingConvention = AutoBeJsonSchemaNamingConvention = {})); const countCapitalLetters = (str) => (str.match(/[A-Z]/g) || []).length; class Convention { constructor() { this.dict = new Map(); this.closures = []; } emplace(key, setter) { const elements = key.split(".").map(pluralize_1.singular); this.closures.push({ value: elements.join("."), setter, }); const head = elements[0]; utils_1.MapUtil.take(this.dict, head.toLowerCase(), () => new Set()).add(head); } 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 head = mapping.get(elements[0].toLowerCase()); closure.setter([head, ...elements.slice(1)].join(".")); } } } //# sourceMappingURL=AutoBeJsonSchemaNamingConvention.js.map