export type UserToken = { expiresIn : number ; identity : string ; token : string ; } ; let cachedUserToken = undefined ; /**
 * This is implemented by contoso and passed to ChatProvider to use.
 */ export const getToken = async ( ) : Promise < UserToken > => { if ( ! cachedUserToken ) { try { const getTokenRequestOptions = { method : 'POST' } ; const getTokenResponse = await fetch ( '/token' , getTokenRequestOptions ) ; cachedUserToken = ( await getTokenResponse . json ( ) . then ( ( _responseJson ) => { return { expiresOn : _responseJson . expiresOn , identity : _responseJson . user . communicationUserId , token : _responseJson . token } ; } ) ) as UserToken ; } catch ( error ) { console . error ( 'Failed at getting token, Error: ' , error ) ; } } if ( ! cachedUserToken ) { throw new Error ( 'user token undefined' ) ; } return cachedUserToken ; } ; export function clearCachedUserToken ( ) { cachedUserToken = undefined ; }