import { partition , pascalCase , camelCase } from '@sap-cloud-sdk/util' ; import { Method } from '../openapi-types' ; import { operationNameExtension } from '../extensions' ; import { OperationInfo } from './parsing-info' ; param operations returns internal export function nameOperations ( operations ) { const [ operationsWithExtensions , operationsWithoutExtensions ] = partition ( operations , ( { operation } ) => ! ! operation [ operationNameExtension ] ) ; validateUniqueExtensions ( operationsWithExtensions ) ; const renamedOperationsWithExtensions = renameOperations ( operationsWithExtensions , renameOperationWithExtension ) ; const [ namedOperations , unnamedOperations ] = partition ( operationsWithoutExtensions , ( { operation } ) => ! ! operation . operationId ) ; const renamedUnnamedOperations = renameOperations ( unnamedOperations , ( { pathPattern , method } ) => getOperationNameFromPatternAndMethod ( pathPattern , method ) ) ; return [ ... renamedOperationsWithExtensions , ... namedOperations , ... renamedUnnamedOperations ] ; } function validateUniqueExtensions ( operations ) { operations . reduce ( ( uniqueOperationNames , operationInfo ) => { const operationName = renameOperationWithExtension ( operationInfo ) ; if ( uniqueOperationNames . includes ( operationName ) ) { throw new Error ( `Operation name '${ operationInfo . operation [ operationNameExtension ] }' provided for '${ operationNameExtension }' resolves to '${ operationName }' and is not unique.` ) ; } return [ ... uniqueOperationNames , operationName ] ; } , [ ] ) ; } function renameOperationWithExtension ( { operation } ) { const originalOperationName = operation [ operationNameExtension ] ; if ( ! originalOperationName ) { throw new Error ( 'Could not rename operation based on extension. Extension is not defined.' ) ; } return camelCase ( originalOperationName ) ; } function renameOperations ( operations , renameFn ) { return operations . map ( operationInfo => { operationInfo . operation . operationId = renameFn ( operationInfo ) ; return operationInfo ; } ) ; } internal export function getOperationNameFromPatternAndMethod ( pattern , method ) { const [ placeholders , pathParts ] = partition ( pattern . split ( '/' ) , part => /^\{.+\}$/ . test ( part ) ) ; const prefix = getSpeakingNameForMethod ( method ) . toLowerCase ( ) ; const base = pathParts . map ( part => pascalCase ( part ) ) . join ( '' ) ; const suffix = placeholders . length ? 'By' + placeholders . map ( part => pascalCase ( part ) ) . join ( 'And' ) : '' ; return prefix + base + suffix ; } function getSpeakingNameForMethod ( method ) { return nameMapping [ method ] ; } const nameMapping = { get : 'get' , post : 'create' , patch : 'update' , put : 'update' , delete : 'delete' , head : 'getHeadersFor' , options : 'getOptionsFor' , trace : 'trace' } as const ;