UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

134 lines 10.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SdkHttpFunctionProgrammer = void 0; const typescript_1 = __importDefault(require("typescript")); const IdentifierFactory_1 = require("typia/lib/factories/IdentifierFactory"); const TypeFactory_1 = require("typia/lib/factories/TypeFactory"); const StringUtil_1 = require("../../utils/StringUtil"); const SdkAliasCollection_1 = require("./SdkAliasCollection"); const SdkImportWizard_1 = require("./SdkImportWizard"); var SdkHttpFunctionProgrammer; (function (SdkHttpFunctionProgrammer) { SdkHttpFunctionProgrammer.write = (project) => (importer) => (route, props) => typescript_1.default.factory.createFunctionDeclaration([ typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword), typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.AsyncKeyword), ], undefined, route.name, undefined, [ IdentifierFactory_1.IdentifierFactory.parameter("connection", typescript_1.default.factory.createTypeReferenceNode(SdkImportWizard_1.SdkImportWizard.IConnection(importer), props.headers ? [typescript_1.default.factory.createTypeReferenceNode(`${route.name}.Headers`)] : undefined)), ...route.parameters .filter((p) => p.category !== "headers") .map((p) => typescript_1.default.factory.createParameterDeclaration([], undefined, p.name, p.metadata.optional === true ? typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionToken) : undefined, project.config.primitive !== false && (p === props.query || p === props.input) ? typescript_1.default.factory.createTypeReferenceNode(`${route.name}.${p === props.query ? "Query" : "Input"}`) : project.config.clone === true ? SdkAliasCollection_1.SdkAliasCollection.from(project)(importer)(p.metadata) : SdkAliasCollection_1.SdkAliasCollection.name(p))), ], typescript_1.default.factory.createTypeReferenceNode("Promise", [ project.config.propagate === true || route.success.metadata.size() !== 0 ? typescript_1.default.factory.createTypeReferenceNode(`${route.name}.Output`) : typescript_1.default.factory.createTypeReferenceNode("void"), ]), typescript_1.default.factory.createBlock(write_body(project)(importer)(route, props), true)); const write_body = (project) => (importer) => (route, props) => { const caller = () => { var _a, _b, _c; return typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.Fetcher(!!((_a = props.input) === null || _a === void 0 ? void 0 : _a.encrypted) || route.success.encrypted)(importer)), project.config.propagate ? "propagate" : "fetch"), project.config.propagate ? route.method.toLowerCase() === "get" || route.method.toLowerCase() === "head" ? [TypeFactory_1.TypeFactory.keyword("any")] : [TypeFactory_1.TypeFactory.keyword("any"), TypeFactory_1.TypeFactory.keyword("any")] : undefined, [ props.input && props.input.contentType !== "multipart/form-data" ? 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.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("Content-Type"), typescript_1.default.factory.createStringLiteral((_c = (_b = props.input) === null || _b === void 0 ? void 0 : _b.contentType) !== null && _c !== void 0 ? _c : "application/json")), ], true)), ], true) : typescript_1.default.factory.createIdentifier("connection"), typescript_1.default.factory.createObjectLiteralExpression([ typescript_1.default.factory.createSpreadAssignment(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(route.name), "METADATA")), typescript_1.default.factory.createPropertyAssignment("template", IdentifierFactory_1.IdentifierFactory.access(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(route.name), "METADATA"), "path")), typescript_1.default.factory.createPropertyAssignment("path", typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(route.name), "path"), undefined, route.parameters .filter((p) => p.category === "param" || p.category === "query") .map((p) => typescript_1.default.factory.createIdentifier(p.name)))), ], true), ...(props.input ? [typescript_1.default.factory.createIdentifier(props.input.name)] : []), ...(project.config.json && props.input !== undefined && (props.input.contentType === "application/json" || props.input.encrypted === true) ? [typescript_1.default.factory.createIdentifier(`${route.name}.stringify`)] : []), ]); }; const output = (awaiter) => project.config.simulate ? typescript_1.default.factory.createConditionalExpression(typescript_1.default.factory.createIdentifier("!!connection.simulate"), undefined, typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(`${route.name}.simulate`), [], [ typescript_1.default.factory.createIdentifier("connection"), ...route.parameters .filter((p) => p.category !== "headers") .map((p) => typescript_1.default.factory.createIdentifier(p.name)), ]), undefined, awaiter ? typescript_1.default.factory.createAwaitExpression(caller()) : caller()) : awaiter ? typescript_1.default.factory.createAwaitExpression(caller()) : caller(); return [ ...(project.config.assert ? route.parameters .filter((p) => p.category !== "headers") .map((p) => typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.typia(importer)), "assert"), [ typescript_1.default.factory.createTypeQueryNode(typescript_1.default.factory.createIdentifier(p.name)), ], [typescript_1.default.factory.createIdentifier(p.name)]))) : []), ...(route.success.setHeaders.length === 0 ? [typescript_1.default.factory.createReturnStatement(output(false))] : write_set_headers(project)(importer)(route)(output(true))), ]; }; const write_set_headers = (project) => (importer) => (route) => (condition) => { const accessor = (x) => (y) => x[0] === "[" ? `${x}${y}` : `${x}.${y}`; const output = StringUtil_1.StringUtil.escapeDuplicate([ "connection", ...route.parameters.map((p) => p.name), ])("output"); const headers = accessor("connection")("headers"); const data = project.config.propagate ? accessor(output)("data") : output; const assigners = [ typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createIdentifier(headers), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionQuestionEqualsToken), typescript_1.default.factory.createObjectLiteralExpression([])), ...route.success.setHeaders.map((tuple) => { var _a; return tuple.type === "assigner" ? typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("Object.assign"), [], [ typescript_1.default.factory.createIdentifier(headers), typescript_1.default.factory.createIdentifier(accessor(data)(tuple.source)), ]) : typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createIdentifier(accessor(headers)((_a = tuple.target) !== null && _a !== void 0 ? _a : tuple.source)), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsToken), typescript_1.default.factory.createIdentifier(accessor(data)(tuple.source))); }), ].map(typescript_1.default.factory.createExpressionStatement); return [ typescript_1.default.factory.createVariableStatement([], typescript_1.default.factory.createVariableDeclarationList([ typescript_1.default.factory.createVariableDeclaration(output, undefined, SdkAliasCollection_1.SdkAliasCollection.output(project)(importer)(route), condition), ], typescript_1.default.NodeFlags.Const)), ...(project.config.propagate ? [ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createIdentifier(accessor(output)("success")), assigners.length === 1 ? assigners[0] : typescript_1.default.factory.createBlock(assigners, true), undefined), ] : assigners), typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createIdentifier(output)), ]; }; })(SdkHttpFunctionProgrammer || (exports.SdkHttpFunctionProgrammer = SdkHttpFunctionProgrammer = {})); //# sourceMappingURL=SdkHttpFunctionProgrammer.js.map