import { codeBlock , documentationBlock , unixEOL } from '@sap-cloud-sdk/util' ; import { OpenApiOperation , OpenApiParameter , OpenApiRequestBody } from '../openapi-types' ; import { serializeSchema } from './schema' ; param operation returns internal export function serializeOperation ( operation ) { const requestBuilderParams = [ `'${ operation . method }'` , `'${ operation . pathPattern }'` ] ; const bodyAndQueryParams = serializeParamsForRequestBuilder ( operation ) ; if ( bodyAndQueryParams ) { requestBuilderParams . push ( bodyAndQueryParams ) ; } const responseType = serializeSchema ( operation . response ) ; return codeBlock `
${ operationDocumentation ( operation ) }
${ operation . operationId }: (${ serializeOperationSignature ( operation ) }) => new OpenApiRequestBuilder<${ responseType }>(
  ${ requestBuilderParams . join ( ',\n' ) }
)` ; } function serializeOperationSignature ( operation ) { const pathParams = serializePathParamsForSignature ( operation ) ; const requestBodyParam = serializeRequestBodyParamForSignature ( operation ) ; const queryParams = serializeQueryParamsForSignature ( operation ) ; return [ pathParams , requestBodyParam , queryParams ] . filter ( params => params ) . join ( ', ' ) ; } function serializePathParamsForSignature ( operation ) { if ( operation . pathParameters . length ) { return operation . pathParameters . map ( param => `${ param . name }: string` ) . join ( ', ' ) ; } } function serializeRequestBodyParamForSignature ( operation ) { if ( operation . requestBody ) { return `body: ${ serializeSchema ( operation . requestBody . schema ) }${ operation . requestBody . required ? '' : ' | undefined' }` ; } } function serializeQueryParamsForSignature ( operation ) { if ( operation . queryParameters . length ) { const allOptional = operation . queryParameters . every ( param => ! param . required ) ; const queryParams = operation . queryParameters . map ( param => `'${ param . name }'${ param . required ? '' : '?' }: ${ serializeSchema ( param . schema ) }` ) . join ( ',\n' ) ; return `queryParameters${ allOptional ? '?' : '' }: {${ queryParams }}` ; } } function serializeParamsForRequestBuilder ( operation ) { const params = [ ] ; if ( operation . pathParameters . length ) { const pathParamStr = `pathParameters: { ${ operation . pathParameters . map ( param => param . name ) . join ( ', ' ) } }` ; params . push ( pathParamStr ) ; } if ( operation . requestBody ) { params . push ( 'body' ) ; } if ( operation . queryParameters . length ) { params . push ( 'queryParameters' ) ; } if ( params . length ) { return codeBlock `{
      ${ params . join ( ',\n' ) }
    }` ; } } internal export function operationDocumentation ( operation ) { const signature = [ ] ; if ( operation . pathParameters . length ) { signature . push ( ... getSignatureOfPathParameters ( operation . pathParameters ) ) ; } if ( operation . requestBody ) { signature . push ( getSignatureOfBody ( operation . requestBody ) ) ; } if ( operation . queryParameters . length > 0 ) { signature . push ( `@param queryParameters - Object containing the following keys: ${ operation . queryParameters . map ( param => `${ param . name }` ) . join ( ', ' ) }.` ) ; } signature . push ( '@returns The request builder, use the `execute()` method to trigger the request.' ) ; const lines = [ getOperationDescriptionText ( operation ) , ... signature ] ; return documentationBlock `${ lines . join ( unixEOL ) }` ; } function getSignatureOfPathParameters ( parameters ) { return parameters . map ( parameter => `@param ${ parameter . name } - ${ parameter . description || 'Path parameter.' }` ) ; } function getSignatureOfBody ( body ) { return `@param body - ${ body . description || 'Request body.' }` ; } function getOperationDescriptionText ( operation ) { if ( operation . description ) { return operation . description ; } return `Create a request builder for execution of ${ operation . method } requests to the '${ operation . pathPattern }' endpoint.` ; }