UNPKG

@autobe/compiler

Version:

AI backend server code generator

106 lines 7.24 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeRealizeControllers = void 0; const utils_1 = require("@autobe/utils"); const migrate_1 = require("@nestia/migrate"); const NestiaMigrateNestMethodProgrammer_1 = require("@nestia/migrate/lib/programmers/NestiaMigrateNestMethodProgrammer"); const path_1 = __importDefault(require("path")); const typescript_1 = __importDefault(require("typescript")); const ArrayUtil_1 = require("../utils/ArrayUtil"); const FilePrinter_1 = require("../utils/FilePrinter"); const writeRealizeControllers = (props) => __awaiter(void 0, void 0, void 0, function* () { const app = new migrate_1.NestiaMigrateApplication((0, utils_1.transformOpenApiDocument)(props.document)); const result = app.nest({ simulate: false, e2e: false, programmer: { controllerMethod: (ctx) => { const method = NestiaMigrateNestMethodProgrammer_1.NestiaMigrateNestMethodProgrammer.write(ctx); const operate = props.document.operations.find((o) => o.method === ctx.route.method && o.path === ctx.route.path); const func = props.functions.find((f) => f.endpoint.method === ctx.route.method && f.endpoint.path === ctx.route.path); if (func === undefined || operate === undefined) return method; // unreachable const authorization = operate.authorizationActor ? props.authorizations.find((d) => d.actor.name === operate.authorizationActor) : undefined; if (operate.authorizationActor && authorization === undefined) return method; // unreachable ctx.importer.external({ type: "instance", library: path_1.default .relative(ctx.controller.location, func.location) .replaceAll(path_1.default.sep, "/") .split(".ts")[0], name: func.name, }); const inputArguments = [ ...(operate.authorizationActor ? [operate.authorizationActor] : []), ...ctx.route.parameters.map((p) => p.name), ...(ctx.route.query ? [ctx.route.query.name] : []), ...(ctx.route.body ? [ctx.route.body.name] : []), ]; const call = typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(func.name), undefined, inputArguments.length === 0 ? undefined : [ typescript_1.default.factory.createObjectLiteralExpression(inputArguments.map((name) => typescript_1.default.factory.createShorthandPropertyAssignment(name)), true), ]); const tryCatch = typescript_1.default.factory.createTryStatement(typescript_1.default.factory.createBlock([ typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createAwaitExpression(call)), ], true), typescript_1.default.factory.createCatchClause(typescript_1.default.factory.createVariableDeclaration(typescript_1.default.factory.createIdentifier("error"), undefined, undefined, undefined), typescript_1.default.factory.createBlock([ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("console.log"), undefined, [typescript_1.default.factory.createIdentifier("error")])), typescript_1.default.factory.createThrowStatement(typescript_1.default.factory.createIdentifier("error")), ], true)), undefined); return typescript_1.default.factory.updateMethodDeclaration(method, method.modifiers, method.asteriskToken, method.name, method.questionToken, method.typeParameters, authorization ? [ createAuthorizationParameter(ctx, authorization), ...method.parameters, ] : operate.authorizationType === "login" || operate.authorizationType === "join" ? [createIpParameter(ctx), ...method.parameters] : method.parameters, method.type, typescript_1.default.factory.createBlock([tryCatch])); }, }, }); const entries = yield ArrayUtil_1.ArrayUtil.asyncMap(Object.entries(result).filter(([key]) => key.startsWith("src/controllers/")), (_a) => __awaiter(void 0, [_a], void 0, function* ([key, value]) { return [key, yield FilePrinter_1.FilePrinter.beautify(value)]; })); return Object.fromEntries(entries); }); exports.writeRealizeControllers = writeRealizeControllers; const createAuthorizationParameter = (ctx, authorization) => typescript_1.default.factory.createParameterDeclaration([ typescript_1.default.factory.createDecorator(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(ctx.importer.external({ type: "instance", library: path_1.default .relative(ctx.controller.location, authorization.decorator.location) .replaceAll(path_1.default.sep, "/") .split(".ts")[0], name: authorization.decorator.name, })), undefined, [])), ], undefined, authorization.actor.name, undefined, typescript_1.default.factory.createTypeReferenceNode(ctx.importer.external({ type: "instance", library: path_1.default .relative(ctx.controller.location, authorization.payload.location) .replaceAll(path_1.default.sep, "/") .split(".ts")[0], name: authorization.payload.name, })), undefined); const createIpParameter = (ctx) => typescript_1.default.factory.createParameterDeclaration([ typescript_1.default.factory.createDecorator(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(ctx.importer.external({ type: "instance", library: "@nestjs/common", name: "Ip", })), undefined, [])), ], undefined, "ip", undefined, typescript_1.default.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.StringKeyword), undefined); //# sourceMappingURL=writeRealizeControllers.js.map