UNPKG

json-to-ts

Version:

Convert json object to typescript interfaces

108 lines 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var util_1 = require("./util"); function isKeyNameValid(keyName) { var regex = /^[a-zA-Z_][a-zA-Z\d_]*$/; return regex.test(keyName); } function parseKeyMetaData(key) { var isOptional = key.endsWith("--?"); if (isOptional) { return { isOptional: isOptional, keyValue: key.slice(0, -3), }; } else { return { isOptional: isOptional, keyValue: key, }; } } function findNameById(id, names) { return names.find(function (_) { return _.id === id; }).name; } function removeUndefinedFromUnion(unionTypeName) { var typeNames = unionTypeName.split(" | "); var undefinedIndex = typeNames.indexOf("undefined"); typeNames.splice(undefinedIndex, 1); return typeNames.join(" | "); } function replaceTypeObjIdsWithNames(typeObj, names) { return (Object.entries(typeObj) // quote key if is invalid and question mark if optional from array merging .map(function (_a) { var key = _a[0], type = _a[1]; var _b = parseKeyMetaData(key), isOptional = _b.isOptional, keyValue = _b.keyValue; var isValid = isKeyNameValid(keyValue); var validName = isValid ? keyValue : "'" + keyValue + "'"; return isOptional ? [validName + "?", type, isOptional] : [validName, type, isOptional]; }) // replace hashes with names referencing the hashes .map(function (_a) { var key = _a[0], type = _a[1], isOptional = _a[2]; if (!util_1.isHash(type)) { return [key, type, isOptional]; } var newType = findNameById(type, names); return [key, newType, isOptional]; }) // if union has undefined, remove undefined and make type optional .map(function (_a) { var key = _a[0], type = _a[1], isOptional = _a[2]; if (!(util_1.isNonArrayUnion(type) && type.includes("undefined"))) { return [key, type, isOptional]; } var newType = removeUndefinedFromUnion(type); var newKey = isOptional ? key : key + "?"; // if already optional dont add question mark return [newKey, newType, isOptional]; }) // make undefined optional and set type as any .map(function (_a) { var key = _a[0], type = _a[1], isOptional = _a[2]; if (type !== "undefined") { return [key, type, isOptional]; } var newType = "any"; var newKey = isOptional ? key : key + "?"; // if already optional dont add question mark return [newKey, newType, isOptional]; }) .reduce(function (agg, _a) { var key = _a[0], value = _a[1]; agg[key] = value; return agg; }, {})); } function getInterfaceStringFromDescription(_a) { var name = _a.name, typeMap = _a.typeMap, useTypeAlias = _a.useTypeAlias; var stringTypeMap = Object.entries(typeMap) .map(function (_a) { var key = _a[0], name = _a[1]; return " " + key + ": " + name + ";\n"; }) .reduce(function (a, b) { return (a += b); }, ""); var declarationKeyWord = useTypeAlias ? "type" : "interface"; var interfaceString = declarationKeyWord + " " + name + (useTypeAlias ? " =" : "") + " {\n"; interfaceString += stringTypeMap; interfaceString += "}"; return interfaceString; } exports.getInterfaceStringFromDescription = getInterfaceStringFromDescription; function getInterfaceDescriptions(typeStructure, names) { return names .map(function (_a) { var id = _a.id, name = _a.name; var typeDescription = util_1.findTypeById(id, typeStructure.types); if (typeDescription.typeObj) { var typeMap = replaceTypeObjIdsWithNames(typeDescription.typeObj, names); return { name: name, typeMap: typeMap }; } else { return null; } }) .filter(function (_) { return _ !== null; }); } exports.getInterfaceDescriptions = getInterfaceDescriptions; //# sourceMappingURL=get-interfaces.js.map