import { getLevenshteinClosest } from '@sap-cloud-sdk/generator-common/internal' ; import { codeBlock , pascalCase } from '@sap-cloud-sdk/util' ; import { OpenApiApi , OpenApiOperation } from '../openapi-types' ; internal export function getMainApi ( serviceName , apis ) { return ( getLevenshteinClosest ( serviceName , apis , x => x . name ) || getApiWithMaxOperations ( apis ) ) ; } internal export function getApiWithMaxOperations ( apis ) { const sortedByOperationsLength = apis . sort ( ( a , b ) => a . operations . length > b . operations . length ? - 1 : 1 ) ; const withGetAll = sortedByOperationsLength . find ( api => api . operations . find ( operation => operation . operationId . toLocaleLowerCase ( 'getall' ) ) ) ; if ( withGetAll ) { return withGetAll ; } return sortedByOperationsLength [ 0 ] ; } internal export function getMainOperation ( api ) { if ( api . operations . length === 1 ) { return api . operations [ 0 ] ; } return ( getLevenshteinClosest ( api . name , api . operations , op => op . operationId ) || getGetAllOperation ( api . operations ) || getGetOperation ( api . operations ) || getAnyOperationWithoutParams ( api . operations ) || api . operations [ 0 ] ) ; } internal export function getGetAllOperation ( operations ) { return operations . find ( operation => operation . operationId . toLowerCase ( ) === 'getall' ) ; } internal export function getGetOperation ( operations ) { const getOperations = operations . filter ( operation => operation . method === 'get' ) ; if ( getOperations . length > 0 ) { const getOperationsWithoutParams = getOperations . filter ( getOperation => getOperation . pathParameters . length === 0 && getOperation . queryParameters . length === 0 ) ; if ( getOperationsWithoutParams . length > 0 ) { return getOperationsWithoutParams [ 0 ] ; } return getOperations [ 0 ] ; } return undefined ; } internal export function getAnyOperationWithoutParams ( operations ) { return operations . find ( operation => operation . pathParameters . length === 0 && operation . queryParameters . length === 0 ) ; } internal export function getOperationParamCode ( operation ) { const paramSignature = [ ] ; if ( operation . pathParameters ?. length ) { paramSignature . push ( operation . pathParameters . map ( pathParam => pathParam . name ) . join ( ', ' ) ) ; } if ( operation . requestBody ) { let reqBody = 'undefined' ; if ( operation . requestBody . required ) { reqBody = 'myRequestBody' ; } paramSignature . push ( reqBody ) ; } if ( operation . queryParameters ) { const queryParams = operation . queryParameters . filter ( queryParam => queryParam . required ) . map ( queryParam => `${ queryParam . name }: 'my${ pascalCase ( queryParam . name ) }'` ) ; if ( queryParams . length > 0 ) { paramSignature . push ( `{ ${ queryParams . join ( ', ' ) } }` ) ; } } return codeBlock `${ paramSignature . join ( ', ' ) }` ; }