UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

176 lines 15.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.SdkHttpNamespaceProgrammer = void 0; const typescript_1 = __importDefault(require("typescript")); const ExpressionFactory_1 = require("typia/lib/factories/ExpressionFactory"); const IdentifierFactory_1 = require("typia/lib/factories/IdentifierFactory"); const LiteralFactory_1 = require("typia/lib/factories/LiteralFactory"); const TypeFactory_1 = require("typia/lib/factories/TypeFactory"); const Escaper_1 = require("typia/lib/utils/Escaper"); const FilePrinter_1 = require("./FilePrinter"); const SdkAliasCollection_1 = require("./SdkAliasCollection"); const SdkHttpParameterProgrammer_1 = require("./SdkHttpParameterProgrammer"); const SdkHttpSimulationProgrammer_1 = require("./SdkHttpSimulationProgrammer"); const SdkImportWizard_1 = require("./SdkImportWizard"); var SdkHttpNamespaceProgrammer; (function (SdkHttpNamespaceProgrammer) { SdkHttpNamespaceProgrammer.write = (project) => (importer) => (route) => { const types = writeTypes(project)(importer)(route); return typescript_1.default.factory.createModuleDeclaration([typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.ExportKeyword)], typescript_1.default.factory.createIdentifier(route.name), typescript_1.default.factory.createModuleBlock([ ...types, ...(types.length ? [FilePrinter_1.FilePrinter.enter()] : []), writeMetadata(project)(importer)(route), FilePrinter_1.FilePrinter.enter(), writePath(project)(importer)(route), ...(project.config.simulate ? [ SdkHttpSimulationProgrammer_1.SdkHttpSimulationProgrammer.random(project)(importer)(route), SdkHttpSimulationProgrammer_1.SdkHttpSimulationProgrammer.simulate(project)(importer)(route), ] : []), ...(project.config.json && route.body && (route.body.contentType === "application/json" || route.body.encrypted === true) ? [writeStringify(project)(importer)] : []), ]), typescript_1.default.NodeFlags.Namespace); }; const writeTypes = (project) => (importer) => (route) => { const array = []; const declare = (name, type) => array.push(typescript_1.default.factory.createTypeAliasDeclaration([typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], name, undefined, type)); if (project.config.keyword === true && SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getSignificant(route, true).length !== 0) declare("Props", SdkAliasCollection_1.SdkAliasCollection.httpProps(project)(importer)(route)); if (route.headerObject) declare("Headers", SdkAliasCollection_1.SdkAliasCollection.headers(project)(importer)(route.headerObject)); if (route.queryObject) declare("Query", SdkAliasCollection_1.SdkAliasCollection.query(project)(importer)(route.queryObject)); if (route.body) declare("Body", SdkAliasCollection_1.SdkAliasCollection.body(project)(importer)(route.body)); if (project.config.propagate === true || route.success.metadata.size() !== 0) declare("Output", SdkAliasCollection_1.SdkAliasCollection.response(project)(importer)(route)); return array; }; const writeMetadata = (project) => (importer) => (route) => constant("METADATA")(typescript_1.default.factory.createAsExpression(typescript_1.default.factory.createObjectLiteralExpression([ typescript_1.default.factory.createPropertyAssignment("method", typescript_1.default.factory.createStringLiteral(route.method)), typescript_1.default.factory.createPropertyAssignment("path", typescript_1.default.factory.createStringLiteral(route.path)), typescript_1.default.factory.createPropertyAssignment("request", route.body ? LiteralFactory_1.LiteralFactory.write(route.body !== undefined ? { type: route.body.contentType, encrypted: !!route.body.encrypted, } : { type: "application/json", encrypted: false, }) : typescript_1.default.factory.createNull()), typescript_1.default.factory.createPropertyAssignment("response", route.method !== "HEAD" ? LiteralFactory_1.LiteralFactory.write({ type: route.success.contentType, encrypted: !!route.success.encrypted, }) : typescript_1.default.factory.createNull()), typescript_1.default.factory.createPropertyAssignment("status", route.success.status !== null ? ExpressionFactory_1.ExpressionFactory.number(route.success.status) : typescript_1.default.factory.createNull()), ...(route.success.contentType === "application/x-www-form-urlencoded" ? [ typescript_1.default.factory.createPropertyAssignment("parseQuery", typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(`${SdkImportWizard_1.SdkImportWizard.typia(importer)}.http.createAssertQuery`), [ project.config.clone === true ? SdkAliasCollection_1.SdkAliasCollection.from(project)(importer)(route.success.metadata) : SdkAliasCollection_1.SdkAliasCollection.name(route.success), ], undefined)), ] : []), ], true), typescript_1.default.factory.createTypeReferenceNode(typescript_1.default.factory.createIdentifier("const")))); const writePath = (project) => (importer) => (route) => { const out = (body) => constant("path")(typescript_1.default.factory.createArrowFunction([], [], SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getParameterDeclarations({ project, importer, route, body: false, prefix: false, }), undefined, undefined, body)); const parameters = SdkHttpParameterProgrammer_1.SdkHttpParameterProgrammer.getSignificant(route, false); if (parameters.length === 0) return out(typescript_1.default.factory.createStringLiteral(route.path)); const access = (name) => project.config.keyword === true ? `props.${name}` : name; const template = () => { const split = route.path.split(":"); if (split.length === 1) return typescript_1.default.factory.createStringLiteral(route.path); return typescript_1.default.factory.createTemplateExpression(typescript_1.default.factory.createTemplateHead(split[0]), split.slice(1).map((s, i, arr) => { const name = s.split("/")[0]; return typescript_1.default.factory.createTemplateSpan(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("encodeURIComponent"), undefined, [ typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createCallChain(typescript_1.default.factory.createPropertyAccessChain(typescript_1.default.factory.createIdentifier(access(route.pathParameters.find((p) => p.field === name) .name)), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionDotToken), "toString"), undefined, undefined, []), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionQuestionToken), typescript_1.default.factory.createStringLiteral("null")), ]), (i !== arr.length - 1 ? typescript_1.default.factory.createTemplateMiddle : typescript_1.default.factory.createTemplateTail)(s.substring(name.length))); })); }; if (route.queryObject === null && route.queryParameters.length === 0) return out(template()); const block = (expr) => { const computeName = (str) => parameters.find((p) => p.name === str) !== undefined ? computeName("_" + str) : str; const variables = computeName("variables"); return typescript_1.default.factory.createBlock([ local(variables)("URLSearchParams")(typescript_1.default.factory.createNewExpression(typescript_1.default.factory.createIdentifier("URLSearchParams"), [], [])), typescript_1.default.factory.createForOfStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ typescript_1.default.factory.createVariableDeclaration(typescript_1.default.factory.createArrayBindingPattern([ typescript_1.default.factory.createBindingElement(undefined, undefined, typescript_1.default.factory.createIdentifier("key"), undefined), typescript_1.default.factory.createBindingElement(undefined, undefined, typescript_1.default.factory.createIdentifier("value"), undefined), ]), undefined, undefined, undefined), ], typescript_1.default.NodeFlags.Const), typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("Object.entries"), undefined, [ typescript_1.default.factory.createAsExpression(expr, TypeFactory_1.TypeFactory.keyword("any")), ]), typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createIdentifier("undefined"), typescript_1.default.factory.createIdentifier("value")), typescript_1.default.factory.createContinueStatement(), typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("Array.isArray"), undefined, [typescript_1.default.factory.createIdentifier("value")]), typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("value"), typescript_1.default.factory.createIdentifier("forEach")), undefined, [ typescript_1.default.factory.createArrowFunction(undefined, undefined, [IdentifierFactory_1.IdentifierFactory.parameter("elem")], undefined, undefined, typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(variables), "append"), undefined, [ typescript_1.default.factory.createIdentifier("key"), typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("String"), undefined, [typescript_1.default.factory.createIdentifier("elem")]), ])), ])), typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(variables), "set"), undefined, [ typescript_1.default.factory.createIdentifier("key"), typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("String"), undefined, [typescript_1.default.factory.createIdentifier("value")]), ]))))), local("location")("string")(template()), typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createConditionalExpression(typescript_1.default.factory.createStrictEquality(ExpressionFactory_1.ExpressionFactory.number(0), IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(variables), "size")), undefined, typescript_1.default.factory.createIdentifier("location"), undefined, typescript_1.default.factory.createTemplateExpression(typescript_1.default.factory.createTemplateHead(""), [ typescript_1.default.factory.createTemplateSpan(typescript_1.default.factory.createIdentifier("location"), typescript_1.default.factory.createTemplateMiddle("?")), typescript_1.default.factory.createTemplateSpan(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(variables), "toString"), undefined, undefined), typescript_1.default.factory.createTemplateTail("")), ]))), ], true); }; if (route.queryObject !== null && route.queryParameters.length === 0) return out(block(route.queryObject.metadata.isRequired() === false ? typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createIdentifier(route.queryObject.name), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionQuestionToken), typescript_1.default.factory.createObjectLiteralExpression([], false)) : typescript_1.default.factory.createIdentifier(access(route.queryObject.name)))); return out(block(typescript_1.default.factory.createObjectLiteralExpression([ ...(route.queryObject ? [ typescript_1.default.factory.createSpreadAssignment(typescript_1.default.factory.createIdentifier(access(route.queryObject.name))), ] : []), ...route.queryParameters.map((q) => typescript_1.default.factory.createPropertyAssignment(Escaper_1.Escaper.variable(q.field) ? q.field : typescript_1.default.factory.createStringLiteral(q.field), typescript_1.default.factory.createIdentifier(access(q.name)))), ], true))); }; const writeStringify = (project) => (importer) => constant("stringify")(typescript_1.default.factory.createArrowFunction([], undefined, [ IdentifierFactory_1.IdentifierFactory.parameter("input", typescript_1.default.factory.createTypeReferenceNode("Body")), ], undefined, undefined, typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier(SdkImportWizard_1.SdkImportWizard.typia(importer)), "json"), project.config.assert ? "stringify" : "assertStringify"), undefined, [typescript_1.default.factory.createIdentifier("input")]))); })(SdkHttpNamespaceProgrammer || (exports.SdkHttpNamespaceProgrammer = SdkHttpNamespaceProgrammer = {})); const local = (name) => (type) => (expression) => typescript_1.default.factory.createVariableStatement([], typescript_1.default.factory.createVariableDeclarationList([ typescript_1.default.factory.createVariableDeclaration(name, undefined, typescript_1.default.factory.createTypeReferenceNode(type), expression), ], typescript_1.default.NodeFlags.Const)); const constant = (name) => (expression) => 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(name, undefined, undefined, expression), ], typescript_1.default.NodeFlags.Const)); //# sourceMappingURL=SdkHttpNamespaceProgrammer.js.map