import { createLogger } from '@sap-cloud-sdk/util' ; import { Cache } from './cache' ; import { ClientCredentialsResponse } from './xsuaa-service-types' ; const logger = createLogger ( { package : 'connectivity' , messageContext : 'client-credentials-token-cache' } ) ; const ClientCredentialsTokenCache = ( cache ) => ( { getToken : ( url , clientId ) : ClientCredentialsResponse | undefined => cache . get ( getCacheKey ( url , clientId ) ) , cacheToken : ( url , clientId , token ) : void => { cache . set ( getCacheKey ( url , clientId ) , token , token . expires_in ? Date . now ( ) + token . expires_in * 1000 : undefined ) ; } , clear : ( ) : void => { cache . clear ( ) ; } , getCacheInstance : ( ) => cache } ) ; internal param url param clientId returns export function getCacheKey ( url , clientId ) { if ( ! url ) { logger . warn ( 'Cannot get cache key. The url was undefined.' ) ; return ; } if ( ! clientId ) { logger . warn ( 'Cannot get cache key. The ClientId was undefined.' ) ; return ; } return [ url , clientId ] . join ( ':' ) ; } internal export const clientCredentialsTokenCache = ClientCredentialsTokenCache ( new Cache < ClientCredentialsResponse > ( ) ) ;