UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

128 lines 9.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SdkHttpFunctionProgrammer = void 0; const factory_1 = require("@ttsc/factory"); const IdentifierFactory_1 = require("../../factories/IdentifierFactory"); const TypeFactory_1 = require("../../factories/TypeFactory"); const legacy_1 = require("../../internal/legacy"); const StringUtil_1 = require("../../utils/StringUtil"); const SdkAliasCollection_1 = require("./SdkAliasCollection"); const SdkHttpParameterProgrammer_1 = require("./SdkHttpParameterProgrammer"); const SdkImportWizard_1 = require("./SdkImportWizard"); var SdkHttpFunctionProgrammer; (function (SdkHttpFunctionProgrammer) { SdkHttpFunctionProgrammer.write = (project) => (importer) => (route) => { return factory_1.factory.createFunctionDeclaration([ factory_1.factory.createModifier(factory_1.SyntaxKind.ExportKeyword), factory_1.factory.createModifier(factory_1.SyntaxKind.AsyncKeyword), ], undefined, route.name, undefined, [ IdentifierFactory_1.IdentifierFactory.parameter("connection", factory_1.factory.createTypeReferenceNode(SdkImportWizard_1.SdkImportWizard.IConnection(importer), route.headerObject !== null ? [factory_1.factory.createTypeReferenceNode(`${route.name}.Headers`)] : undefined)), ...SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getParameterDeclarations({ project, importer, route, body: true, prefix: true, }), ], factory_1.factory.createTypeReferenceNode("Promise", [ project.config.propagate === true || route.success.binary === true || (0, legacy_1.sizeOf)(route.success.metadata) !== 0 ? factory_1.factory.createTypeReferenceNode(`${route.name}.Output`) : factory_1.factory.createTypeReferenceNode("void"), ]), factory_1.factory.createBlock(writeBody(project)(importer)(route), true)); }; const writeBody = (project) => (importer) => (route) => { const access = (name) => project.config.keyword === true ? factory_1.factory.createPropertyAccessExpression(factory_1.factory.createIdentifier("props"), name) : factory_1.factory.createIdentifier(name); const fetch = () => { var _a, _b; var _c; return factory_1.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(factory_1.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.Fetcher(!!((_a = route.body) === 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, [ route.body && route.body.contentType !== "multipart/form-data" ? factory_1.factory.createObjectLiteralExpression([ factory_1.factory.createSpreadAssignment(factory_1.factory.createIdentifier("connection")), factory_1.factory.createPropertyAssignment("headers", factory_1.factory.createObjectLiteralExpression([ factory_1.factory.createSpreadAssignment(IdentifierFactory_1.IdentifierFactory.access(factory_1.factory.createIdentifier("connection"), "headers")), factory_1.factory.createPropertyAssignment(factory_1.factory.createStringLiteral("Content-Type"), factory_1.factory.createStringLiteral((_c = (_b = route.body) === null || _b === void 0 ? void 0 : _b.contentType) !== null && _c !== void 0 ? _c : "application/json")), ], true)), ], true) : factory_1.factory.createIdentifier("connection"), factory_1.factory.createObjectLiteralExpression([ factory_1.factory.createSpreadAssignment(IdentifierFactory_1.IdentifierFactory.access(factory_1.factory.createIdentifier(route.name), "METADATA")), factory_1.factory.createPropertyAssignment("template", IdentifierFactory_1.IdentifierFactory.access(IdentifierFactory_1.IdentifierFactory.access(factory_1.factory.createIdentifier(route.name), "METADATA"), "path")), factory_1.factory.createPropertyAssignment("path", factory_1.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(factory_1.factory.createIdentifier(route.name), "path"), undefined, SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getArguments({ project, route, body: false, }))), ], true), ...(route.body ? [access(route.body.name)] : []), ...(project.config.json && route.body !== null && (route.body.contentType === "application/json" || route.body.encrypted === true) ? [factory_1.factory.createIdentifier(`${route.name}.stringify`)] : []), ]); }; const output = (awaiter) => project.config.simulate ? factory_1.factory.createConditionalExpression(factory_1.factory.createStrictEquality(factory_1.factory.createTrue(), factory_1.factory.createIdentifier("connection.simulate")), undefined, factory_1.factory.createCallExpression(factory_1.factory.createIdentifier(`${route.name}.simulate`), [], [ factory_1.factory.createIdentifier("connection"), ...SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getArguments({ project, route, body: true, }), ]), undefined, awaiter ? factory_1.factory.createAwaitExpression(fetch()) : fetch()) : awaiter ? factory_1.factory.createAwaitExpression(fetch()) : fetch(); return [ ...(project.config.assert ? SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getSignificant(route, true).map((p) => factory_1.factory.createExpressionStatement(factory_1.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(factory_1.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.typia(importer)), "assert"), [factory_1.factory.createTypeQueryNode(access(p.name))], [access(p.name)]))) : []), ...(route.success.setHeaders.length === 0 ? [factory_1.factory.createReturnStatement(output(false))] : writeSetHeaders(project)(importer)(route)(output(true))), ]; }; const writeSetHeaders = (project) => (importer) => (route) => (condition) => { const accessor = (x) => (y) => x[0] === "[" ? `${x}${y}` : `${x}.${y}`; const output = StringUtil_1.StringUtil.escapeDuplicate([ "connection", ...SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getSignificant(route, true).map((p) => p.name), ])("output"); const headers = accessor("connection")("headers"); const data = project.config.propagate ? accessor(output)("data") : output; const assigners = [ factory_1.factory.createBinaryExpression(factory_1.factory.createIdentifier(headers), factory_1.factory.createToken(factory_1.SyntaxKind.QuestionQuestionEqualsToken), factory_1.factory.createObjectLiteralExpression([])), ...route.success.setHeaders.map((tuple) => { var _a; return tuple.type === "assigner" ? factory_1.factory.createCallExpression(factory_1.factory.createIdentifier("Object.assign"), [], [ factory_1.factory.createIdentifier(headers), factory_1.factory.createIdentifier(accessor(data)(tuple.source)), ]) : factory_1.factory.createBinaryExpression(factory_1.factory.createIdentifier(accessor(headers)((_a = tuple.target) !== null && _a !== void 0 ? _a : tuple.source)), factory_1.factory.createToken(factory_1.SyntaxKind.EqualsToken), factory_1.factory.createIdentifier(accessor(data)(tuple.source))); }), ].map(factory_1.factory.createExpressionStatement); return [ factory_1.factory.createVariableStatement([], factory_1.factory.createVariableDeclarationList([ factory_1.factory.createVariableDeclaration(output, undefined, SdkAliasCollection_1.SdkAliasCollection.response(project)(importer)(route), condition), ], factory_1.NodeFlags.Const)), ...(project.config.propagate ? [ factory_1.factory.createIfStatement(factory_1.factory.createIdentifier(accessor(output)("success")), assigners.length === 1 ? assigners[0] : factory_1.factory.createBlock(assigners, true), undefined), ] : assigners), factory_1.factory.createReturnStatement(factory_1.factory.createIdentifier(output)), ]; }; })(SdkHttpFunctionProgrammer || (exports.SdkHttpFunctionProgrammer = SdkHttpFunctionProgrammer = {})); //# sourceMappingURL=SdkHttpFunctionProgrammer.js.map