mcp-swagger-parser
Version:
Enterprise-grade OpenAPI/Swagger specification parser for Model Context Protocol (MCP) projects
50 lines • 1.88 kB
JavaScript
;
/**
* Text parser for OpenAPI specifications
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextParser = void 0;
const index_1 = require("../errors/index");
const base_parser_1 = require("./base-parser");
class TextParser extends base_parser_1.BaseParser {
/**
* Parse OpenAPI specification from text content
*/
async parse(content, options = {}) {
if (!content || typeof content !== 'string') {
throw new index_1.OpenAPIParseError('Content must be a non-empty string', index_1.ERROR_CODES.INVALID_FORMAT);
}
try {
let spec;
const format = options.format || 'auto';
if (format === 'json') {
spec = this.parseJson(content);
}
else if (format === 'yaml') {
spec = await this.parseYaml(content);
}
else {
// Auto-detect format
const detectedFormat = this.detectFormat(content);
if (detectedFormat === 'json') {
spec = this.parseJson(content);
}
else {
spec = await this.parseYaml(content);
}
}
// Validate basic structure
this.validateBasicStructure(spec);
return spec;
}
catch (error) {
if (error instanceof index_1.OpenAPIParseError) {
throw error;
}
const source = options.filename || 'text content';
throw new index_1.OpenAPIParseError(`Failed to parse ${source}: ${error instanceof Error ? error.message : String(error)}`, index_1.ERROR_CODES.PARSE_ERROR, source, error instanceof Error ? error : undefined);
}
}
}
exports.TextParser = TextParser;
//# sourceMappingURL=text-parser.js.map