import { assoc } from '@sap-cloud-sdk/util' ; import { DestinationFetchOptions , isDestinationFetchOptions } from './destination-accessor-types' ; import { AuthenticationType , Destination , DestinationAuthToken , DestinationCertificate } from './destination-service-types' ; param destination returns export function sanitizeDestination ( destination ) { validateDestinationInput ( destination ) ; const destAuthToken = parseAuthTokens ( destination ) ; let parsedDestination = parseCertificates ( destAuthToken ) as Destination ; parsedDestination = setDefaultAuthenticationFallback ( parsedDestination ) ; parsedDestination = setTrustAll ( parsedDestination ) ; parsedDestination = setOriginalProperties ( parsedDestination ) ; return parsedDestination ; } param destinationJson returns export function parseDestination ( destinationJson ) { const destinationConfig = getDestinationConfig ( destinationJson ) ; validateDestinationConfig ( destinationConfig ) ; const destination = Object . entries ( destinationConfig ) . reduce ( ( dest , [ originalKey , value ] ) => { if ( originalKey in configMapping ) { dest [ configMapping [ originalKey ] ] = value ; } return dest ; } , { originalProperties : destinationJson , authTokens : destinationJson [ 'authTokens' ] || [ ] , certificates : destinationJson [ 'certificates' ] || [ ] } ) ; const additionalHeadersAndQueryParameters = getAdditionalHeadersAndQueryParameters ( destinationConfig ) ; return sanitizeDestination ( { ... destination , ... additionalHeadersAndQueryParameters } ) ; } param destinationConfig param originalKeyPrefix returns function getAdditionalProperties ( destinationConfig , originalKeyPrefix ) { const relevantConfigEntries = Object . entries ( destinationConfig ) . filter ( ( [ key ] ) => key . startsWith ( originalKeyPrefix ) ) ; return relevantConfigEntries . reduce ( ( additionalProperties , [ originalKey , value ] ) => { const headerKey = originalKey . replace ( originalKeyPrefix , '' ) ; additionalProperties [ headerKey ] = value ; return additionalProperties ; } , { } ) ; } param destinationConfig returns function getAdditionalHeadersAndQueryParameters ( destinationConfig ) { const additionalHeaders = getAdditionalProperties ( destinationConfig , 'URL.headers.' ) ; const additionalQueryParameters = getAdditionalProperties ( destinationConfig , 'URL.queries.' ) ; const additionalProperties = { } ; if ( Object . keys ( additionalHeaders ) . length ) { additionalProperties [ 'headers' ] = additionalHeaders ; } if ( Object . keys ( additionalQueryParameters ) . length ) { additionalProperties [ 'queryParameters' ] = additionalQueryParameters ; } return additionalProperties ; } function getDestinationConfig ( destinationJson ) { return isDestinationJson ( destinationJson ) ? destinationJson . destinationConfiguration : destinationJson ; } function validateDestinationConfig ( destinationConfig ) { if ( isHttpDestination ( destinationConfig ) && typeof destinationConfig . URL === 'undefined' ) { throw Error ( "Property 'URL' of destination configuration must not be undefined." ) ; } } function validateDestinationInput ( destinationInput ) { if ( isHttpDestination ( destinationInput ) && typeof destinationInput . url === 'undefined' ) { throw Error ( "Property 'url' of destination input must not be undefined." ) ; } } function isHttpDestination ( destinationInput ) { return ( destinationInput . Type === 'HTTP' || destinationInput . type === 'HTTP' || ( typeof destinationInput . type === 'undefined' && typeof destinationInput . Type === 'undefined' ) ) ; } param destination returns export function toDestinationNameUrl ( destination ) { return isDestinationFetchOptions ( destination ) ? `name: ${ destination . destinationName }` : `name: ${ destination . name }, url: ${ destination . url }` ; } function setOriginalProperties ( destination ) { const originalProperties = destination . originalProperties ? destination . originalProperties : destination ; return assoc ( 'originalProperties' , originalProperties , destination ) ; } function setDefaultAuthenticationFallback ( destination ) { return destination . authentication ? destination : assoc ( 'authentication' , getAuthenticationType ( destination ) , destination ) ; } function parseCertificate ( certificate ) { return { name : certificate . Name || certificate . name , content : certificate . Content || certificate . content , type : certificate . Type || certificate . type } ; } function parseCertificates ( destination ) { const certificates = destination . certificates ? destination . certificates . map ( parseCertificate ) : [ ] ; return assoc ( 'certificates' , certificates , destination ) ; } function parseAuthToken ( authToken ) { return { type : authToken . type , value : authToken . value , expiresIn : authToken . expires_in , error : 'error' in authToken ? authToken . error : null , http_header : authToken . http_header } ; } function parseAuthTokens ( destination ) { const authTokens = destination . authTokens ? destination . authTokens . map ( parseAuthToken ) : [ ] ; return assoc ( 'authTokens' , authTokens , destination ) ; } function setTrustAll ( destination ) { return assoc ( 'isTrustingAllCertificates' , parseTrustAll ( destination . isTrustingAllCertificates ) , destination ) ; } function parseTrustAll ( isTrustingAllCertificates ? ) { if ( typeof isTrustingAllCertificates === 'string' ) { return isTrustingAllCertificates . toLowerCase ( ) === 'true' ; } return ! ! isTrustingAllCertificates ; } function getAuthenticationType ( destination ) { return destination . authentication || ( destination . username && destination . password ) ? 'BasicAuthentication' : 'NoAuthentication' ; } internal export interface DestinationJson { [ key ] : any ; destinationConfiguration : DestinationConfiguration ; authTokens ? : Record < string , string > [ ] ; certificates ? : Record < string , string > [ ] ; } internal export interface DestinationConfiguration { [ key ] : any ; URL : string ; Name ? : string ; ProxyType ? : string ; 'sap-client' ? : string ; User ? : string ; Password ? : string ; Authentication ? : AuthenticationType ; TrustAll ? : string ; tokenServiceURL ? : string ; tokenServiceURLType ? : 'Common' | 'Dedicated;' ; tokenServiceUsername ? : string ; tokenServicePass ? : string ; clientId ? : string ; clientSecret ? : string ; SystemUser ? : string ; Type ? : 'HTTP' | 'LDAP' | 'MAIL' | 'RFC' ; } internal export function isDestinationConfiguration ( destination ) { return destination . URL !== undefined ; } internal export function isDestinationJson ( destination ) { return Object . keys ( destination ) . includes ( 'destinationConfiguration' ) ; } const configMapping = { URL : 'url' , Name : 'name' , User : 'username' , Password : 'password' , ProxyType : 'proxyType' , 'sap-client' : 'sapClient' , Authentication : 'authentication' , TrustAll : 'isTrustingAllCertificates' , Type : 'type' , tokenServiceURL : 'tokenServiceUrl' , clientId : 'clientId' , clientSecret : 'clientSecret' , tokenServiceUser : 'tokenServiceUser' , tokenServicePassword : 'tokenServicePassword' , CloudConnectorLocationId : 'cloudConnectorLocationId' , certificates : 'certificates' , KeyStoreLocation : 'keyStoreName' , KeyStorePassword : 'keyStorePassword' , SystemUser : 'systemUser' } ; export function noDestinationErrorMessage ( destination ) { return isDestinationFetchOptions ( destination ) ? `Could not find a destination with name "${ destination . destinationName }"! Unable to execute request.` : 'Could not find a destination to execute request against and no destination name has been provided (this should never happen)!' ; }