UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

93 lines 6.69 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.E2eFileProgrammer = void 0; const typescript_1 = __importDefault(require("typescript")); const IdentifierFactory_1 = require("typia/lib/factories/IdentifierFactory"); const FilePrinter_1 = require("./FilePrinter"); const ImportDictionary_1 = require("./ImportDictionary"); const SdkAliasCollection_1 = require("./SdkAliasCollection"); const SdkImportWizard_1 = require("./SdkImportWizard"); var E2eFileProgrammer; (function (E2eFileProgrammer) { E2eFileProgrammer.generate = (project) => (props) => (route) => __awaiter(this, void 0, void 0, function* () { const importer = new ImportDictionary_1.ImportDictionary(`${props.current}/${getFunctionName(route)}.ts`); if (project.config.clone !== true) for (const tuple of route.imports) for (const instance of tuple.instances) importer.internal({ file: tuple.file, type: true, instance, }); importer.internal({ type: false, file: props.api, instance: null, name: "api", }); const functor = generate_function(project)(importer)(route); yield FilePrinter_1.FilePrinter.write({ location: importer.file, statements: [ ...importer.toStatements(props.current), FilePrinter_1.FilePrinter.enter(), functor, ], }); }); const generate_function = (project) => (importer) => (route) => typescript_1.default.factory.createVariableStatement([typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], typescript_1.default.factory.createVariableDeclarationList([ typescript_1.default.factory.createVariableDeclaration(typescript_1.default.factory.createIdentifier(getFunctionName(route)), undefined, undefined, generate_arrow(project)(importer)(route)), ], typescript_1.default.NodeFlags.Const)); const generate_arrow = (project) => (importer) => (route) => { const headers = route.parameters.find((p) => p.category === "headers" && p.field === null); const connection = headers ? typescript_1.default.factory.createObjectLiteralExpression([ typescript_1.default.factory.createSpreadAssignment(typescript_1.default.factory.createIdentifier("connection")), typescript_1.default.factory.createPropertyAssignment("headers", typescript_1.default.factory.createObjectLiteralExpression([ typescript_1.default.factory.createSpreadAssignment(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier("connection"), "headers")), typescript_1.default.factory.createSpreadAssignment(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.typia(importer)), "random"), [ project.config.clone === true ? SdkAliasCollection_1.SdkAliasCollection.from(project)(importer)(headers.metadata) : SdkAliasCollection_1.SdkAliasCollection.name(headers), ], undefined)), ], true)), ], true) : typescript_1.default.factory.createIdentifier("connection"); const caller = typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(["api", "functional", ...route.accessor].join(".")), undefined, [ connection, ...route.parameters .filter((p) => p.category !== "headers") .map((p) => typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.typia(importer)), "random"), [ project.config.clone === true ? SdkAliasCollection_1.SdkAliasCollection.from(project)(importer)(p.metadata) : SdkAliasCollection_1.SdkAliasCollection.name(p), ], undefined)), ]); const assert = typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.typia(importer)), "assert"), undefined, [typescript_1.default.factory.createIdentifier("output")]); return typescript_1.default.factory.createArrowFunction([typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.AsyncKeyword)], undefined, [ IdentifierFactory_1.IdentifierFactory.parameter("connection", typescript_1.default.factory.createTypeReferenceNode("api.IConnection")), ], undefined, undefined, typescript_1.default.factory.createBlock([ typescript_1.default.factory.createVariableStatement([], typescript_1.default.factory.createVariableDeclarationList([ typescript_1.default.factory.createVariableDeclaration("output", undefined, project.config.propagate !== true && route.success.type.name === "void" ? undefined : SdkAliasCollection_1.SdkAliasCollection.output(project)(importer)(route), typescript_1.default.factory.createAwaitExpression(caller)), ], typescript_1.default.NodeFlags.Const)), typescript_1.default.factory.createExpressionStatement(assert), ])); }; })(E2eFileProgrammer || (exports.E2eFileProgrammer = E2eFileProgrammer = {})); const getFunctionName = (route) => ["test", "api", ...route.accessor].join("_"); //# sourceMappingURL=E2eFileProgrammer.js.map