UNPKG

st-open-api

Version:

Generates API client SDKs from an OpenAPI specification written in OpenAPI version 3.x.x

274 lines 11.6 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInterfaceOrEnumFromSchema = void 0; var enum_property_1 = require("../classes/enum-property"); var fs = require("fs"); var nodePath = require("path"); var path_1 = require("path"); var interface_property_1 = require("../classes/interface-property"); var folder_manager_1 = require("../classes/folder-manager"); var config_1 = require("./config"); var interface_array_property_1 = require("../classes/interface-array-property"); var formatText_1 = require("./formatText"); var primitive_type_property_1 = require("../classes/primitive-type-property"); var getInterfaceOrEnumFromSchema = function (className, originalName, schema, path) { var e_1, _a; var isDebug = config_1.configuration.isDebug(); if (isDebug) { console.log("Get enum or interface ".concat(className)); console.log("Schema ".concat(className), JSON.stringify(schema, null, 2)); } var isArray = false; if (schema.type === 'array') { isArray = true; if (!!schema.items && schema.items["$ref"]) { var ref = config_1.configuration.getReference().getImportAndTypeByRef(schema.items["$ref"], path); return new interface_array_property_1.InterfaceArrayProperty(className, ref.import, ref.className); } schema = schema.items; schema.type = 'object'; } var schemasAllOf = schema.allOf; if (schemasAllOf) { schema = schemasAllOf .map(function (allOfSchema) { var schemaRef = allOfSchema.$ref; if (schemaRef) { return config_1.configuration.getReference().getImportAndTypeByRef(schemaRef, path).schema; } return allOfSchema; }) .filter(function (v) { return !!v; }) .reduce(function (prev, curr) { return (__assign(__assign({}, prev), { required: __spreadArray(__spreadArray([], __read(((prev === null || prev === void 0 ? void 0 : prev.required) || [])), false), __read(((curr === null || curr === void 0 ? void 0 : curr.required) || [])), false), properties: __assign(__assign({}, ((prev === null || prev === void 0 ? void 0 : prev.properties) || {})), ((curr === null || curr === void 0 ? void 0 : curr.properties) || {})) })); }, { type: 'object' }); } if (schema.type === 'object' || schema.properties || schema.additionalProperties) { if (isDebug) { console.log("Object Schema ".concat(className, " (array=").concat(isArray, ")"), JSON.stringify(schema, null, 2)); } if (!!schema.properties || typeof schema.additionalProperties === 'object') { var interfaceProperty = new interface_property_1.InterfaceProperty(className); try { for (var _b = __values(Object.keys(schema.properties || {})), _c = _b.next(); !_c.done; _c = _b.next()) { var propertyName = _c.value; var property = schema.properties[propertyName]; var isRequired = (schema.required || []).indexOf(propertyName) > -1; interfaceProperty.addProperty(getProperty(className, originalName, propertyName, isRequired, property, path)); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } if (typeof schema.additionalProperties === 'object' && !!schema.additionalProperties.type) { var isArray_1 = schema.additionalProperties.type === 'array'; if (isArray_1) { interfaceProperty.addAdditionalProperties(mapType(schema.additionalProperties.items.type), true); } else { interfaceProperty.addAdditionalProperties(mapType(schema.additionalProperties.type)); } } return interfaceProperty; } //getReference(schema); } else if (schema.enum) { var enumClass = new enum_property_1.EnumProperty(className); enumClass.setValues(schema.enum); return enumClass; } else if (isPrimitive(schema.type)) { var primitiveTypeClass = new primitive_type_property_1.PrimitiveTypeProperty(className); primitiveTypeClass.setPrimitiveType(mapType(schema.type)); return primitiveTypeClass; } else { console.log(className, originalName, 'no mapping for schema found'); } }; exports.getInterfaceOrEnumFromSchema = getInterfaceOrEnumFromSchema; var getProperty = function (className, originalName, propertyName, required, schema, path) { var isDebug = config_1.configuration.isDebug(); var ref = config_1.configuration.getReference(); if (isDebug) { console.log("Enter get property ".concat(originalName), JSON.stringify(schema, null, 2)); } var isArray = false; if (schema.type === 'array') { isArray = true; schema = schema.items; if (isPrimitive(schema.type)) { return { propertyName: propertyName, required: (schema.required || []).indexOf(propertyName) > -1 || required, description: '', isArray: isArray, value: mapType(schema.type), import: '' }; } else { schema.type = 'object'; } } var _import; var value = mapType(schema.type); var enumeration = getEnumeration(schema); if (isDebug && !!enumeration) { console.log('Enumeration found ', enumeration); } var reference = getReference(schema); if (isDebug && !!reference) { console.log('Reference found ', JSON.stringify(schema, null, 2)); } if (schema.allOf) { reference = handleAllOfProperty(propertyName, originalName, path, schema); } if (!!enumeration) { var newOriginal = "".concat(propertyName).concat(originalName.substring(0, 1).toUpperCase()).concat(originalName.substring(1)); var nestedPath = getNestedPath(path, 'enumeration'); var enumeration_1 = (0, exports.getInterfaceOrEnumFromSchema)("I".concat((0, formatText_1.formatText)(newOriginal, 'ANY', 'PascalCase')), newOriginal, schema, nestedPath); var rendered = enumeration_1.render(); fs.appendFileSync(nodePath.join(nestedPath, "".concat(rendered.fileName, ".ts")), rendered.render); var refKey = "#/nested/enumeration/".concat(newOriginal); ref.addReference(refKey, { fileName: rendered.fileName, className: rendered.classEnumName, folderPath: nestedPath }); reference = refKey; } if (schema.type === 'object' && !reference) { var newOriginal = "".concat(propertyName).concat(originalName.substring(0, 1).toUpperCase()).concat(originalName.substring(1)); var nestedPath = getNestedPath(path, 'interface'); var object = (0, exports.getInterfaceOrEnumFromSchema)("I".concat((0, formatText_1.formatText)(newOriginal, 'ANY', 'PascalCase')), newOriginal, schema, nestedPath); var rendered = object.render(); fs.appendFileSync(nodePath.join(nestedPath, "".concat(rendered.fileName, ".ts")), rendered.render); var refKey = "#/nested/interface/".concat(newOriginal); ref.addReference(refKey, { fileName: rendered.fileName, className: rendered.classEnumName, folderPath: nestedPath }); reference = refKey; } if (!!reference) { var refResult = ref.getImportAndTypeByRef(reference, path); _import = refResult.import; value = refResult.className; } return { propertyName: propertyName, required: (schema.required || []).indexOf(propertyName) > -1 || required, description: '', isArray: isArray, value: value, import: _import, }; }; var getEnumeration = function (schema) { if (schema.items && schema.items.enum) { return schema.items.enum; } if (schema.enum) { return schema.enum; } }; var getReference = function (schema) { if (schema.items && schema.items.$ref) { return schema.items.$ref; } if (schema.$ref) { return schema.$ref; } }; var mapType = function (type) { switch (type) { case "integer": return "number"; case undefined: return "any"; default: return type; } ; }; var getNestedPath = function (path, type) { return (0, folder_manager_1.mkdir)((0, path_1.join)(path, type)); }; var isPrimitive = function (type) { switch (type) { case "boolean": case "integer": case "number": case "string": case undefined: return true; } return false; }; var handleAllOfProperty = function (propertyName, originalName, path, schema) { var ref = config_1.configuration.getReference(); var referenceName = "".concat(propertyName).concat(originalName.substring(0, 1).toUpperCase()).concat(originalName.substring(1)); var allOfCombination = (0, exports.getInterfaceOrEnumFromSchema)("I".concat((0, formatText_1.formatText)(referenceName, 'ANY', 'PascalCase')), referenceName, schema, path); var rendered = allOfCombination.render(); fs.appendFileSync(nodePath.join(path, "".concat(rendered.fileName, ".ts")), rendered.render); var refKey = "#/nested/".concat(referenceName); ref.addReference(refKey, { fileName: rendered.fileName, className: rendered.classEnumName, folderPath: path }); return refKey; }; //# sourceMappingURL=get-property.js.map