UNPKG

infinity-forge

Version:
76 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateInterfacesOpenApi = generateInterfacesOpenApi; var mapOpenApiTypeToTS = function (prop, array) { var _a, _b, _c, _d; if (!prop) { return 'any'; } if (prop === null || prop === void 0 ? void 0 : prop.$ref) { return prop.$ref.replace('#/components/schemas/', ''); } if (Array.isArray(prop.enum) && prop.enum.length > 0) { var union = prop.enum .map(function (v) { return (typeof v === 'string' ? "'".concat(v, "'") : String(v)); }) .join(' | '); return union; } if (!prop.type) { 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'; } switch (prop.type) { case 'string': return 'string'; case 'integer': if (prop.format === 'int32') { return 'number'; } return 'number'; case 'number': return 'number'; case 'boolean': return 'boolean'; case 'array': if (prop.items) { return "".concat(mapOpenApiTypeToTS(prop.items, true), "[]"); } return 'any[]'; case 'object': if (prop.additionalProperties) { var valueType = mapOpenApiTypeToTS(prop.additionalProperties, false); return "Record<string, ".concat(valueType, ">"); } return 'Record<string, any>'; default: if ((_d = (_c = prop === null || prop === void 0 ? void 0 : prop.allOf) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d['$ref']) { var name_2 = prop.allOf[0]['$ref'].replace('#/components/schemas/', ''); return name_2; } 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