UNPKG

openapi-minifier

Version:

A CLI tool by Treblle tp minify OpenAPI V3 Specs by removing redundant information not relevant to AI Agents and LLMs.

59 lines 1.82 kB
import yaml from 'js-yaml'; export function parseOpenAPI(content, format) { try { let spec; if (format === 'yaml') { spec = yaml.load(content, { schema: yaml.JSON_SCHEMA, onWarning: (warning) => { console.warn('YAML parsing warning:', warning); } }); } else { spec = JSON.parse(content); } if (!spec || typeof spec !== 'object') { throw new Error('Invalid OpenAPI specification: not an object'); } const openApiSpec = spec; if (!openApiSpec.openapi) { throw new Error('Missing "openapi" field'); } if (!openApiSpec.info) { throw new Error('Missing "info" field'); } if (!openApiSpec.paths) { throw new Error('Missing "paths" field'); } return openApiSpec; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to parse OpenAPI specification: ${error.message}`); } throw new Error('Failed to parse OpenAPI specification: Unknown error'); } } export function serializeOpenAPI(spec, format) { try { if (format === 'yaml') { return yaml.dump(spec, { indent: 2, lineWidth: -1, noRefs: true, sortKeys: false, }); } else { return JSON.stringify(spec); } } catch (error) { if (error instanceof Error) { throw new Error(`Failed to serialize OpenAPI specification: ${error.message}`); } throw new Error('Failed to serialize OpenAPI specification: Unknown error'); } } //# sourceMappingURL=parser.js.map