import { promises } from 'fs' ; import { parse } from 'path' ; import { OpenAPIV3 } from 'openapi-types' ; import { convert } from 'swagger2openapi' ; import { load } from 'js-yaml' ; import { ErrorWithCause } from '@sap-cloud-sdk/util' ; const { readFile } = promises ; param filePath internal export async function convertOpenApiSpec ( filePath ) { try { const file = await parseFileAsJson ( filePath ) ; return convertDocToOpenApiV3 ( file ) ; } catch ( err ) { throw new ErrorWithCause ( 'Could not convert document to the format needed for parsing and generation.' , err ) ; } } param filePath returns internal export async function parseFileAsJson ( filePath ) { const fileContent = await readFile ( filePath , 'utf8' ) ; const extension = parse ( filePath ) . ext . toLowerCase ( ) ; if ( extension === '.json' ) { return JSON . parse ( fileContent ) ; } if ( [ '.yaml' , '.yml' ] . includes ( extension ) ) { return load ( fileContent ) as Record < string , any > ; } throw new Error ( `Could not parse OpenAPI specification at ${ filePath }. Only JSON and YAML files are allowed.` ) ; } param openApiDocument returns internal export async function convertDocToOpenApiV3 ( openApiDocument ) { try { return ( await convert ( openApiDocument , { } ) ) . openapi ; } catch ( err ) { throw new ErrorWithCause ( `Could not convert OpenAPI specification to OpenAPI version 3. ${ err . message }` , err ) ; } }