import { flat , pascalCase } from '@sap-cloud-sdk/util' ; import { OpenAPIV3 } from 'openapi-types' ; import { methods , OpenApiApi } from '../openapi-types' ; import { apiNameExtension , defaultApiName } from '../extensions' ; import { parseOperation } from './operation' ; import { OperationInfo } from './parsing-info' ; import { nameOperations } from './operation-naming' ; import { ensureUniqueNames } from './unique-naming' ; import { OpenApiDocumentRefs } from './refs' ; import { ParserOptions } from './options' ; param document param refs param options returns internal export function parseApis ( document , refs , options ) { const operationsByApis = getOperationsByApis ( document ) ; return Object . entries ( operationsByApis ) . map ( ( [ name , operations ] ) => { const namedOperations = nameOperations ( operations ) ; const operationNames = namedOperations . map ( ( { operation } ) => operation . operationId ! ) ; const uniqueNames = ensureUniqueNames ( operationNames , options , { separator : '_' } ) ; const uniquelyNamedOperations = namedOperations . map ( ( operationInfo , i ) => { operationInfo . operation . operationId = uniqueNames [ i ] ; return operationInfo ; } ) ; return { name , operations : uniquelyNamedOperations . map ( operationInfo => parseOperation ( operationInfo , refs , options ) ) } ; } ) ; } function getPathItem ( document , pathPattern ) { const pathItem = document . paths [ pathPattern ] ; if ( ! pathItem ) { throw new Error ( `Could not parse APIs. Path pattern '${ pathPattern }' does not exist in the document.` ) ; } return pathItem ; } function getApiNameForOperation ( { operation , pathPattern } , document ) { const originalApiName = operation [ apiNameExtension ] || getPathItem ( document , pathPattern ) [ apiNameExtension ] || document [ apiNameExtension ] || operation . tags ?. [ 0 ] || defaultApiName ; return `${ pascalCase ( originalApiName . replace ( /api$/i , '' ) ) }Api` ; } function getOperationsByApis ( document ) { const allOperations = getAllOperations ( document ) ; if ( ! allOperations . length ) { throw new Error ( 'Could not parse APIs. The document does not contain any operations.' ) ; } return allOperations . reduce ( ( apiMap , operationInfo ) => { const apiName = getApiNameForOperation ( operationInfo , document ) ; if ( ! apiMap [ apiName ] ) { apiMap [ apiName ] = [ ] ; } apiMap [ apiName ] . push ( operationInfo ) ; return apiMap ; } , { } as Record < string , OperationInfo [ ] > ) ; } function getAllOperations ( openApiDocument ) { return flat ( Object . entries ( openApiDocument . paths ) . map ( ( [ pathPattern , pathItem ] ) => methods . filter ( method => pathItem [ method ] ) . map ( method => ( { pathPattern , operation : pathItem [ method ] ! , pathItemParameters : pathItem . parameters || [ ] , method } ) ) ) ) ; }