UNPKG

mcp-swagger-parser

Version:

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

81 lines 3.23 kB
"use strict"; /** * URL parser for OpenAPI specifications */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UrlParser = void 0; const axios_1 = __importDefault(require("axios")); const index_1 = require("../errors/index"); const base_parser_1 = require("./base-parser"); class UrlParser extends base_parser_1.BaseParser { /** * Parse OpenAPI specification from URL */ async parse(url, options = {}) { // Validate URL format if (!this.isValidUrl(url)) { throw new index_1.OpenAPIParseError(`Invalid URL format: ${url}`, index_1.ERROR_CODES.INVALID_FORMAT, url); } try { const response = await axios_1.default.get(url, { timeout: options.timeout || 120000, headers: { 'Accept': 'application/json, application/yaml, text/yaml, text/plain', ...options.headers }, httpsAgent: options.validateCertificate === false ? { rejectUnauthorized: false } : undefined }); // Determine content type and parse accordingly const contentType = response.headers['content-type'] || ''; let spec; if (contentType.includes('yaml') || contentType.includes('yml')) { spec = await this.parseYaml(response.data); } else if (contentType.includes('json')) { spec = typeof response.data === 'string' ? this.parseJson(response.data) : response.data; } else { // Auto-detect format const content = typeof response.data === 'string' ? response.data : JSON.stringify(response.data); const format = this.detectFormat(content); if (format === 'json') { spec = this.parseJson(content); } else { spec = await this.parseYaml(content); } } // Validate basic structure this.validateBasicStructure(spec); return spec; } catch (error) { if (axios_1.default.isAxiosError(error)) { throw new index_1.OpenAPINetworkError(`Failed to fetch OpenAPI spec from URL: ${error.message}`, url, error.response?.status, error); } if (error instanceof index_1.OpenAPIParseError) { throw error; } throw new index_1.OpenAPIParseError(`Failed to parse OpenAPI spec from URL: ${error instanceof Error ? error.message : String(error)}`, index_1.ERROR_CODES.PARSE_ERROR, url, error instanceof Error ? error : undefined); } } /** * Validate URL format */ isValidUrl(url) { try { const parsed = new URL(url); return ['http:', 'https:'].includes(parsed.protocol); } catch { return false; } } } exports.UrlParser = UrlParser; //# sourceMappingURL=url-parser.js.map