import { createLogger , first } from '@sap-cloud-sdk/util' ; import { Cache , IsolationStrategy } from '../cache' ; import { tenantId } from '../tenant' ; import { userId } from '../user' ; import { Destination } from './destination-service-types' ; import { DestinationsByType } from './destination-accessor-types' ; const logger = createLogger ( { package : 'connectivity' , messageContext : 'destination-cache' } ) ; const DestinationCache = ( cache ) => ( { retrieveDestinationFromCache : ( decodedJwt , name , isolation ) : Destination | undefined => cache . get ( getDestinationCacheKey ( decodedJwt , name , isolation ) ) , cacheRetrievedDestination : ( decodedJwt , destination , isolation ) : void => { cacheRetrievedDestination ( decodedJwt , destination , isolation , cache ) ; } , cacheRetrievedDestinations : ( decodedJwt , retrievedDestinations , isolation ) : void => { retrievedDestinations . subaccount . forEach ( dest => cacheRetrievedDestination ( decodedJwt , dest , isolation , cache ) ) ; retrievedDestinations . instance . forEach ( dest => cacheRetrievedDestination ( decodedJwt , dest , isolation , cache ) ) ; } , clear : ( ) => { cache . clear ( ) ; } , getCacheInstance : ( ) => cache } ) ; param decodedJwt param destinationName param isolationStrategy returns internal export function getDestinationCacheKey ( decodedJwt , destinationName , isolationStrategy = IsolationStrategy . Tenant_User ) { const tenant = tenantId ( decodedJwt ) ; const user = userId ( decodedJwt ) ; switch ( isolationStrategy ) { case IsolationStrategy . No_Isolation : return `::${ destinationName }` ; case IsolationStrategy . Tenant : if ( tenant ) { return `${ tenant }::${ destinationName }` ; } logger . warn ( `Cannot get cache key. Isolation strategy ${ isolationStrategy } is used, but tenant id is undefined.` ) ; return ; case IsolationStrategy . User : if ( user ) { return `:${ user }:${ destinationName }` ; } logger . warn ( `Cannot get cache key. Isolation strategy ${ isolationStrategy } is used, but user id is undefined.` ) ; return ; case IsolationStrategy . Tenant_User : if ( tenant && user ) { return `${ user }:${ tenant }:${ destinationName }` ; } logger . warn ( `Cannot get cache key. Isolation strategy ${ isolationStrategy } is used, but tenant id or user id is undefined.` ) ; return ; default : logger . warn ( `Cannot get cache key. Isolation strategy ${ isolationStrategy } is not supported.` ) ; return ; } } function cacheRetrievedDestination ( decodedJwt , destination , isolation , cache ) { if ( ! destination . name ) { throw new Error ( 'The destination name is undefined.' ) ; } const key = getDestinationCacheKey ( decodedJwt , destination . name , isolation ) ; const expiresIn = first ( destination . authTokens || [ ] ) ?. expiresIn ; const expirationTime = expiresIn ? Date . now ( ) + parseInt ( expiresIn ) * 1000 : undefined ; cache . set ( key , destination , expirationTime ) ; } internal export const destinationCache = DestinationCache ( new Cache < Destination > ( { hours : 0 , minutes : 5 , seconds : 0 } ) ) ;