@nestia/sdk
Version:
Nestia SDK and Swagger generator
183 lines • 16.1 kB
JavaScript
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 SdkHttpSimulationProgrammer_1 = require("./SdkHttpSimulationProgrammer");
const SdkImportWizard_1 = require("./SdkImportWizard");
var SdkHttpNamespaceProgrammer;
(function (SdkHttpNamespaceProgrammer) {
SdkHttpNamespaceProgrammer.write = (project) => (importer) => (route, props) => {
const types = write_types(project)(importer)(route, props);
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()] : []),
write_metadata(project)(importer)(route, props),
FilePrinter_1.FilePrinter.enter(),
write_path(project)(importer)(route, props.query),
...(project.config.simulate
? [
SdkHttpSimulationProgrammer_1.SdkHttpSimulationProgrammer.random(project)(importer)(route),
SdkHttpSimulationProgrammer_1.SdkHttpSimulationProgrammer.simulate(project)(importer)(route, props),
]
: []),
...(project.config.json &&
props.input !== undefined &&
(props.input.contentType === "application/json" ||
props.input.encrypted === true)
? [write_stringify(project)(importer)]
: []),
]), typescript_1.default.NodeFlags.Namespace);
};
const write_types = (project) => (importer) => (route, props) => {
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 (props.headers !== undefined)
declare("Headers", SdkAliasCollection_1.SdkAliasCollection.headers(project)(importer)(props.headers));
if (props.query !== undefined)
declare("Query", SdkAliasCollection_1.SdkAliasCollection.query(project)(importer)(props.query));
if (props.input !== undefined)
declare("Input", SdkAliasCollection_1.SdkAliasCollection.input(project)(importer)(props.input));
if (project.config.propagate === true ||
route.success.metadata.size() !== 0)
declare("Output", SdkAliasCollection_1.SdkAliasCollection.output(project)(importer)(route));
return array;
};
const write_metadata = (project) => (importer) => (route, props) => 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", props.input
? LiteralFactory_1.LiteralFactory.write(props.input !== undefined
? {
type: props.input.contentType,
encrypted: !!props.input.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 write_path = (project) => (importer) => (route, query) => {
const g = {
total: [
...route.parameters.filter((param) => param.category === "param" || param.category === "query"),
],
query: route.parameters
.filter((param) => param.category === "query")
.filter((param) => param.field !== null),
path: route.parameters.filter((param) => param.category === "param"),
};
const out = (body) => constant("path")(typescript_1.default.factory.createArrowFunction([], [], g.total.map((p) => IdentifierFactory_1.IdentifierFactory.parameter(p.name, p === query
? p.metadata.isRequired() === false
? typescript_1.default.factory.createUnionTypeNode([
typescript_1.default.factory.createTypeReferenceNode(`${route.name}.Query`),
typescript_1.default.factory.createTypeReferenceNode("undefined"),
])
: typescript_1.default.factory.createTypeReferenceNode(`${route.name}.Query`)
: project.config.clone === true
? SdkAliasCollection_1.SdkAliasCollection.from(project)(importer)(p.metadata)
: SdkAliasCollection_1.SdkAliasCollection.name(p))), undefined, undefined, body));
if (g.total.length === 0)
return out(typescript_1.default.factory.createStringLiteral(route.path));
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(g.path.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 (query === undefined && g.query.length === 0)
return out(template());
const block = (expr) => {
const computeName = (str) => g.total.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 (query !== undefined && g.query.length === 0)
return out(block(query.metadata.isRequired() === false
? typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createIdentifier(query.name), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionQuestionToken), typescript_1.default.factory.createObjectLiteralExpression([], false))
: typescript_1.default.factory.createIdentifier(query.name)));
return out(block(typescript_1.default.factory.createObjectLiteralExpression([
...(query
? [
typescript_1.default.factory.createSpreadAssignment(typescript_1.default.factory.createIdentifier(query.name)),
]
: []),
...g.query.map((q) => q.name === q.field
? typescript_1.default.factory.createShorthandPropertyAssignment(q.name)
: 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(q.name))),
], true)));
};
const write_stringify = (project) => (importer) => constant("stringify")(typescript_1.default.factory.createArrowFunction([], undefined, [
IdentifierFactory_1.IdentifierFactory.parameter("input", typescript_1.default.factory.createTypeReferenceNode("Input")),
], 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
;