UNPKG

mcp-swagger-parser

Version:

Enterprise-grade OpenAPI/Swagger specification parser for Model Context Protocol (MCP) projects

82 lines 2.42 kB
"use strict"; /** * Version detection utilities for OpenAPI specifications */ Object.defineProperty(exports, "__esModule", { value: true }); exports.VersionDetector = void 0; /** * Version detector for OpenAPI/Swagger specifications */ class VersionDetector { /** * Detect the version of an OpenAPI/Swagger specification */ static detect(spec) { if (!spec || typeof spec !== 'object') { return 'unknown'; } // Check for Swagger 2.0 if (spec.swagger && typeof spec.swagger === 'string' && spec.swagger.startsWith('2.')) { return 'swagger2'; } // Check for OpenAPI 3.x if (spec.openapi && typeof spec.openapi === 'string' && spec.openapi.startsWith('3.')) { return 'openapi3'; } return 'unknown'; } /** * Detect version with detailed information */ static detectDetailed(spec) { const version = this.detect(spec); let detectedVersion = 'unknown'; if (version === 'swagger2' && spec.swagger) { detectedVersion = spec.swagger; } else if (version === 'openapi3' && spec.openapi) { detectedVersion = spec.openapi; } return { version, detectedVersion, isSwagger2: version === 'swagger2', isOpenAPI3: version === 'openapi3', isSupported: version !== 'unknown' }; } /** * Check if a specification is Swagger 2.0 */ static isSwagger2(spec) { return this.detect(spec) === 'swagger2'; } /** * Check if a specification is OpenAPI 3.x */ static isOpenAPI3(spec) { return this.detect(spec) === 'openapi3'; } /** * Check if a specification is supported */ static isSupported(spec) { const version = this.detect(spec); return version === 'swagger2' || version === 'openapi3'; } /** * Get the version string from a specification */ static getVersionString(spec) { const version = this.detect(spec); if (version === 'swagger2' && spec.swagger) { return spec.swagger; } if (version === 'openapi3' && spec.openapi) { return spec.openapi; } return 'unknown'; } } exports.VersionDetector = VersionDetector; //# sourceMappingURL=version-detector.js.map