import { flatten , createLogger } from '@sap-cloud-sdk/util' ; import { getVcapService } from '../environment-accessor' ; import { Destination } from './destination-service-types' ; import { addProxyConfigurationInternet , ProxyStrategy , proxyStrategy } from './proxy-util' ; const logger = createLogger ( { package : 'connectivity' , messageContext : 'destination-accessor-vcap' } ) ; param serviceInstanceName param options returns internal export function destinationForServiceBinding ( serviceInstanceName , options = { } ) { const serviceBindings = loadServiceBindings ( ) ; const selected = findServiceByName ( serviceBindings , serviceInstanceName ) ; const destination = options . transformationFn ? options . transformationFn ( selected ) : transform ( selected ) ; return destination && proxyStrategy ( destination ) === ProxyStrategy . INTERNET_PROXY ? addProxyConfigurationInternet ( destination ) : destination ; } internal export interface DestinationForServiceBindingsOptions { /**
     * Custom transformation function to control how a [[Destination]] is built from the given [[ServiceBinding]].
     */ transformationFn ? : ( serviceBinding : ServiceBinding ) => Destination ; } internal export interface ServiceBinding { [ key ] : any ; name : string ; type : string ; } function loadServiceBindings ( ) { const vcapServices = getVcapService ( ) ; if ( ! vcapServices ) { throw noVcapServicesError ( ) ; } return transformServiceBindings ( vcapServices ) as ServiceBinding [ ] ; } const transformServiceBindings = ( vcapService ) => { const serviceTypes = inlineServiceTypes ( vcapService ) ; const flattened = flattenServiceBindings ( serviceTypes ) ; return flattened ; } ; function flattenServiceBindings ( vcapServices ) { return flatten ( Object . values ( vcapServices ) ) ; } function inlineServiceTypes ( vcapServices ) { return Object . entries ( vcapServices ) . reduce ( ( vcap , [ serviceType , bindings ] ) => ( { ... vcap , [ serviceType ] : bindings . map ( b => ( { ... b , type : serviceType } ) ) } ) , { } ) ; } function findServiceByName ( serviceBindings , serviceInstanceName ) { const found = serviceBindings . find ( s => s . name === serviceInstanceName ) ; if ( ! found ) { throw noServiceBindingFoundError ( serviceBindings , serviceInstanceName ) ; } return found ; } const serviceToDestinationTransformers = { 'business-logging' : businessLoggingBindingToDestination , 's4-hana-cloud' : xfS4hanaCloudBindingToDestination } ; function transform ( serviceBinding ) { if ( ! serviceToDestinationTransformers [ serviceBinding . type ] ) { throw serviceTypeNotSupportedError ( serviceBinding . type ) ; } return serviceToDestinationTransformers [ serviceBinding . type ] ( serviceBinding ) ; } function noVcapServicesError ( ) { return Error ( 'No services are bound to the application (environment variable VCAP_SERVICES is not defined)!' ) ; } function serviceTypeNotSupportedError ( serviceType ) { return Error ( `Service of type ${ serviceType } is not supported! Consider providing your own transformation function when calling destinationForServiceBinding, like this:
  destinationServiceForBinding(yourServiceName, { serviceBindingToDestination: yourTransformationFunction });` ) ; } function noServiceBindingFoundError ( serviceBindings , serviceInstanceName ) { return Error ( `Unable to find a service binding for given name "${ serviceInstanceName }"! Found the following bindings: ${ serviceBindings . map ( s => s . name ) . join ( ', ' ) }.
      ` ) ; } function businessLoggingBindingToDestination ( serviceBinding ) { return { url : serviceBinding . credentials . writeUrl , authentication : 'OAuth2ClientCredentials' , username : serviceBinding . credentials . uaa . clientid , password : serviceBinding . credentials . uaa . clientsecret } ; } function xfS4hanaCloudBindingToDestination ( serviceBinding ) { return { url : serviceBinding . credentials . URL , authentication : 'BasicAuthentication' , username : serviceBinding . credentials . User , password : serviceBinding . credentials . Password } ; } export function searchServiceBindingForDestination ( name ) { logger . info ( 'Attempting to retrieve destination from service binding.' ) ; try { const destination = destinationForServiceBinding ( name ) ; logger . info ( 'Successfully retrieved destination from service binding.' ) ; return destination ; } catch ( error ) { logger . info ( `Could not retrieve destination from service binding. If you are not using SAP Extension Factory, this information probably does not concern you. ${ error . message }` ) ; } }