@autobe/agent
Version:
AI backend server code generator
112 lines (110 loc) • 5.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeInterfaceEndpointProgrammer = void 0;
const utils_1 = require("@autobe/utils");
const utils_2 = require("@typia/utils");
const pluralize_1 = require("pluralize");
const typia_1 = __importDefault(require("typia"));
var AutoBeInterfaceEndpointProgrammer;
(function (AutoBeInterfaceEndpointProgrammer) {
/**
* Filter endpoint designs:
*
* - For base: Remove guest actors and login/join/refresh/session
* authorizationType
* - For action: Remove all non-null authorizationType (action endpoints must be
* null only)
*/
AutoBeInterfaceEndpointProgrammer.filter = (props) => {
// Action endpoints: only allow authorizationType: null
if (props.kind === "action")
return props.design.authorizationType === null;
else if (props.design.endpoint.path === "/auth" ||
props.design.endpoint.path.startsWith("/auth/"))
return false;
// remove specific auth types
if (props.design.authorizationType === "login" ||
props.design.authorizationType === "join" ||
props.design.authorizationType === "refresh" ||
props.design.authorizationType === "withdraw")
return false;
else if (props.design.authorizationType === "session") {
props.design.authorizationActors =
props.design.authorizationActors.filter((name) => {
const actor = props.actors.find((a) => a.name === name);
if (actor === undefined)
return false;
return actor.kind !== "guest";
});
if (props.design.authorizationActors.length === 0)
return false;
else if (props.design.endpoint.method !== "get" &&
props.design.endpoint.method !== "patch")
return false;
return true;
}
return true;
};
AutoBeInterfaceEndpointProgrammer.fixDesign = (props) => {
props.design.endpoint.path = AutoBeInterfaceEndpointProgrammer.fixPath(props.design.endpoint.path);
if (props.design.authorizationActors.length === 1) {
const actor = props.actors.find((a) => a.name === props.design.authorizationActors[0]);
if (actor !== undefined && actor.kind !== "admin") {
const param = utils_2.NamingConvention.camel((0, pluralize_1.singular)(actor.name)) + "Id";
const bracket = `{${param}}`;
if (props.design.endpoint.path.includes(bracket) === true)
props.design.endpoint.path = props.design.endpoint.path.replace(bracket, "");
}
}
else if (props.design.authorizationActors.length > 1 &&
props.design.endpoint.path.includes("{actorId}")) {
props.design.endpoint.path = props.design.endpoint.path.replace("{actorId}", "");
}
return props.design;
};
AutoBeInterfaceEndpointProgrammer.fixPath = (path) => {
return path
.split("/")
.map((s) => s.startsWith("{") && s.endsWith("}")
? `{${utils_2.NamingConvention.camel(s.slice(1, -1).replace(/-/g, "_"))}}`
: s)
.join("/");
};
AutoBeInterfaceEndpointProgrammer.fixApplication = (props) => {
const $defs = props.application.functions[0].parameters.$defs;
const design = $defs["AutoBeInterfaceEndpointDesign"];
if (design === undefined)
throw new Error("AutoBeInterfaceEndpointDesign is undefined");
else if (utils_2.LlmTypeChecker.isObject(design) === false)
throw new Error("AutoBeInterfaceEndpointDesign is not object type");
const property = design.properties["authorizationActors"];
if (property === undefined)
throw new Error("AutoBeInterfaceEndpointDesign.authorizationActors is undefined");
else if (utils_2.LlmTypeChecker.isArray(property) === false)
throw new Error("AutoBeInterfaceEndpointDesign.authorizationActors is not array type");
property.items = {
type: "string",
enum: props.actors.map((actor) => actor.name),
};
};
AutoBeInterfaceEndpointProgrammer.validateDesign = (props) => {
// if (props.actors.length === 0) props.design.authorizationActors = [];
props.design.authorizationActors.forEach((actorName, i) => {
if (props.actors.find((actor) => actor.name === actorName) === undefined)
props.errors.push({
path: `${props.path}.authorizationActors[${i}]`,
expected: props.actors.map((a) => JSON.stringify(a.name)).join(" | "),
value: actorName,
description: utils_1.StringUtil.trim `
Actor "${actorName}" is not defined in the roles list.
Please select one of them below, or do not define (\`null\`):
${props.actors.map((actor) => `- ${actor.name}`).join("\n")}
`,
});
});
};
})(AutoBeInterfaceEndpointProgrammer || (exports.AutoBeInterfaceEndpointProgrammer = AutoBeInterfaceEndpointProgrammer = {}));
//# sourceMappingURL=AutoBeInterfaceEndpointProgrammer.js.map