UNPKG

infinity-forge

Version:
50 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateInterfacesOpenApi = generateInterfacesOpenApi; var mapOpenApiTypeToTS = function (prop, array) { var _a, _b; if ((array && prop.$ref) || (prop === null || prop === void 0 ? void 0 : prop.$ref)) { return prop.$ref.replace('#/components/schemas/', ''); } switch (prop.type) { case 'string': return 'string'; case 'integer': return 'number'; case 'number': return 'number'; case 'boolean': return 'boolean'; case 'array': return "".concat(mapOpenApiTypeToTS(prop.items, true), "[]"); case 'object': return 'Record<string, any>'; default: if ((_b = (_a = prop === null || prop === void 0 ? void 0 : prop.allOf) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b['$ref']) { var name_1 = prop.allOf[0]['$ref'].replace('#/components/schemas/', ''); return name_1; } return 'any'; } }; function generateInterfacesOpenApi(openApiDocument) { var _a; var schemas = (_a = openApiDocument === null || openApiDocument === void 0 ? void 0 : openApiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas; return Object.keys(schemas || {}) .map(function (schemaName) { var schema = schemas[schemaName]; var properties = schema.properties || {}; var required = schema.required || []; var propertiesLines = Object.keys(properties) .map(function (propName) { var prop = properties[propName]; var tsType = mapOpenApiTypeToTS(prop, false); var isRequired = required.includes(propName) ? '' : '?'; return " ".concat(propName).concat(isRequired, ": ").concat(tsType, ";"); }) .join('\n'); return "export interface ".concat(schemaName, " {\n").concat(propertiesLines, "\n}"); }) .join('\n\n'); } //# sourceMappingURL=index.js.map