export interface StorageBackend { getItem ( key ) : string | null ; setItem ( key , value ) : void ; removeItem ( key ) : void ; } export class InMemoryStorageBackend implements StorageBackend { private _data : Record < string , string | undefined > ; constructor ( ) { this . _data = { } ; } getItem ( key ) { return this . _data [ key ] || null ; } setItem ( key , value ) { this . _data [ key ] = value ; } removeItem ( key ) { delete this . _data [ key ] ; } } let defaultBackend ; try { defaultBackend = localStorage ; } catch ( e ) { defaultBackend = new InMemoryStorageBackend ( ) ; } export class CredentialStorage { private _jwtKey : string ; private _backend : StorageBackend ; constructor ( jwtKey , backend = defaultBackend ) { this . _jwtKey = jwtKey ; this . _backend = backend ; } get backend ( ) : StorageBackend { return this . _backend ; } getJWT ( ) { return this . _backend . getItem ( this . _jwtKey ) || undefined ; } setJWT ( value ) { if ( value ) { this . _backend . setItem ( this . _jwtKey , value ) ; } else { this . _backend . removeItem ( this . _jwtKey ) ; } } }