mcp-swagger-parser
Version:
Enterprise-grade OpenAPI/Swagger specification parser for Model Context Protocol (MCP) projects
102 lines • 3.98 kB
JavaScript
;
/**
* Base parser class for OpenAPI specifications
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseParser = void 0;
const index_1 = require("../errors/index");
class BaseParser {
/**
* Detect format of the content
*/
detectFormat(content) {
const trimmed = content.trim();
// Try to parse as JSON first
try {
JSON.parse(trimmed);
return 'json';
}
catch {
// If JSON parsing fails, assume it's YAML
return 'yaml';
}
}
/**
* Parse JSON content
*/
parseJson(content) {
try {
return JSON.parse(content);
}
catch (error) {
throw new index_1.OpenAPIParseError(`Invalid JSON format: ${error instanceof Error ? error.message : 'Unknown error'}`, index_1.ERROR_CODES.INVALID_FORMAT, undefined, error instanceof Error ? error : undefined);
}
}
/**
* Parse YAML content
*/
async parseYaml(content) {
try {
const { load } = await Promise.resolve().then(() => __importStar(require('js-yaml')));
return load(content);
}
catch (error) {
throw new index_1.OpenAPIParseError(`Invalid YAML format: ${error instanceof Error ? error.message : 'Unknown error'}`, index_1.ERROR_CODES.INVALID_FORMAT, undefined, error instanceof Error ? error : undefined);
}
}
/**
* Validate basic OpenAPI structure (supports both OpenAPI 3.x and Swagger 2.0)
*/
validateBasicStructure(spec) {
if (!spec || typeof spec !== 'object') {
throw new index_1.OpenAPIParseError('Invalid OpenAPI specification: must be an object', index_1.ERROR_CODES.VALIDATION_ERROR);
}
// Check for OpenAPI 3.x or Swagger 2.0
const hasOpenAPI = spec.openapi && typeof spec.openapi === 'string';
const hasSwagger = spec.swagger && typeof spec.swagger === 'string';
if (!hasOpenAPI && !hasSwagger) {
throw new index_1.OpenAPIParseError('Missing required field: openapi or swagger', index_1.ERROR_CODES.VALIDATION_ERROR, 'openapi/swagger');
}
if (!spec.info) {
throw new index_1.OpenAPIParseError('Missing required field: info', index_1.ERROR_CODES.VALIDATION_ERROR, 'info');
}
if (!spec.paths) {
throw new index_1.OpenAPIParseError('Missing required field: paths', index_1.ERROR_CODES.VALIDATION_ERROR, 'paths');
}
}
}
exports.BaseParser = BaseParser;
//# sourceMappingURL=base-parser.js.map