UNPKG

@vkontakte/api-schema-typescript-generator

Version:

VK API TypeScript generator

145 lines (144 loc) 6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeCodeBlock = exports.TypeScriptCodeTypes = void 0; const BaseCodeBlock_1 = require("./BaseCodeBlock"); const constants_1 = require("../constants"); const helpers_1 = require("../helpers"); const log_1 = require("../log"); const utils_1 = require("../utils"); var TypeScriptCodeTypes; (function (TypeScriptCodeTypes) { TypeScriptCodeTypes["Interface"] = "interface"; TypeScriptCodeTypes["Enum"] = "enum"; TypeScriptCodeTypes["ConstantObject"] = "constant_object"; TypeScriptCodeTypes["Type"] = "type"; TypeScriptCodeTypes["Const"] = "const"; })(TypeScriptCodeTypes || (exports.TypeScriptCodeTypes = TypeScriptCodeTypes = {})); class TypeCodeBlock extends BaseCodeBlock_1.BaseCodeBlock { constructor(options) { super(); this.options = options; this.type = options.type; this.refName = options.refName; this.interfaceName = options.interfaceName; this.extendsInterfaces = options.extendsInterfaces; this.description = options.description; this.properties = options.properties; this.value = options.value; this.needExport = options.needExport; } options; type; refName; interfaceName; extendsInterfaces; description; properties; value; needExport; addProperty(property) { this.properties.push(property); } getPropertiesCode() { const quoteChar = this.properties.some((property) => (0, helpers_1.areQuotesNeededForProperty)(property.name)) ? "'" : ''; return this.properties .map((property) => { let divider = ''; let lineEnd = ''; switch (this.type) { case TypeScriptCodeTypes.Interface: divider = property.isRequired ? ':' : '?:'; lineEnd = ';'; break; case TypeScriptCodeTypes.ConstantObject: divider = ':'; lineEnd = ','; break; case TypeScriptCodeTypes.Enum: divider = ' ='; lineEnd = ','; break; } let value = property.wrapValue ? (0, utils_1.quoteJavaScriptValue)(property.value) : property.value; let propertyCode = [ ` ${quoteChar}${property.name}${quoteChar}${divider} ${value}${lineEnd}`, ]; if (property.description) { const commentLines = (0, helpers_1.joinCommentLines)(2, property.description); if (commentLines.length) { propertyCode.unshift(commentLines.join(constants_1.newLineChar)); } } return propertyCode.join(constants_1.newLineChar); }) .join(constants_1.newLineChar); } toString() { const hasProperties = this.properties.length > 0; const exportKeyword = this.needExport ? 'export' : ''; let propertiesCode = this.getPropertiesCode(); let before = []; let code = ''; if (this.refName) { before.push(`// ${this.refName}`); } if (this.description) { before = [...before, ...(0, helpers_1.joinCommentLines)(0, this.description)]; } switch (this.type) { case TypeScriptCodeTypes.Interface: { if (!hasProperties) { if (this.options.allowEmptyInterface) { propertiesCode = ''; } else { propertiesCode = [' // empty interface', ' [key: string]: any;'].join(constants_1.newLineChar); } } const extendsInterfaces = Array.isArray(this.extendsInterfaces) && this.extendsInterfaces.length ? this.extendsInterfaces.join(', ') : ''; code = [ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} interface ${this.interfaceName} ${extendsInterfaces} {`), propertiesCode, '}', ].join(propertiesCode.length ? constants_1.newLineChar : ''); break; } case TypeScriptCodeTypes.Enum: code = [ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} enum ${this.interfaceName} {`), propertiesCode, '}', ].join(constants_1.newLineChar); break; case TypeScriptCodeTypes.ConstantObject: code = [ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} const ${this.interfaceName} = {`), propertiesCode, '} as const;', ].join(constants_1.newLineChar); break; case TypeScriptCodeTypes.Type: if (!this.value) { (0, log_1.consoleLogErrorAndExit)(`"${this.interfaceName}" type has empty value`); } code = [ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} type ${this.interfaceName} = ${this.value};`), ].join(constants_1.newLineChar); break; case TypeScriptCodeTypes.Const: if (!this.value) { (0, log_1.consoleLogErrorAndExit)(`"${this.interfaceName}" type has empty value`); } code = [ (0, utils_1.trimStringDoubleSpaces)(`${exportKeyword} const ${this.interfaceName} = ${this.value};`), ].join(constants_1.newLineChar); break; } return [before.join(constants_1.newLineChar), code].join(constants_1.newLineChar).trim(); } } exports.TypeCodeBlock = TypeCodeBlock;