import { StorageInterface , AbstractStorage } from '@cloudbase/adapter-interface' ; import { Adapter } from '../adapters' ; import { Config , ACCESS_TOKEN , ACCESS_TOKEN_Expire , REFRESH_TOKEN , ANONYMOUS_UUID , LOGIN_TYPE_KEY , USER_INFO_KEY , KV } from '../types' ; import { isUndefined , isNull } from './util' ; constant const alwaysLocalKeys = [ 'anonymousUuidKey' ] ; class TcbObject extends AbstractStorage { constructor ( ) { super ( ) ; if ( ! Adapter . adapter . root [ 'tcbObject' ] ) { Adapter . adapter . root [ 'tcbObject' ] = { } ; } } setItem ( key , value ) { Adapter . adapter . root [ 'tcbObject' ] [ key ] = value ; } getItem ( key ) { return Adapter . adapter . root [ 'tcbObject' ] [ key ] ; } removeItem ( key ) { delete Adapter . adapter . root [ 'tcbObject' ] [ key ] ; } clear ( ) { delete Adapter . adapter . root [ 'tcbObject' ] ; } } function createStorage ( persistence , adapter ) { switch ( persistence ) { case 'local' : return adapter . localStorage || new TcbObject ( ) ; case 'none' : return new TcbObject ( ) ; default : return adapter . sessionStorage || new TcbObject ( ) ; } } export class ICache { public keys : { accessTokenKey : string ; accessTokenExpireKey : string ; refreshTokenKey : string ; anonymousUuidKey : string ; loginTypeKey : string ; userInfoKey : string ; } ; private _persistence : string ; private _storage : StorageInterface ; constructor ( config ) { if ( ! this . _storage ) { this . _persistence = Adapter . adapter . primaryStorage || config . persistence ; this . _storage = createStorage ( this . _persistence , Adapter . adapter ) ; const accessTokenKey = `${ ACCESS_TOKEN }_${ config . env }` ; const accessTokenExpireKey = `${ ACCESS_TOKEN_Expire }_${ config . env }` ; const refreshTokenKey = `${ REFRESH_TOKEN }_${ config . env }` ; const anonymousUuidKey = `${ ANONYMOUS_UUID }_${ config . env }` ; const loginTypeKey = `${ LOGIN_TYPE_KEY }_${ config . env }` ; const userInfoKey = `${ USER_INFO_KEY }_${ config . env }` ; this . keys = { accessTokenKey , accessTokenExpireKey , refreshTokenKey , anonymousUuidKey , loginTypeKey , userInfoKey } ; } } public updatePersistence ( persistence ) { if ( persistence === this . _persistence ) { return ; } const isCurrentLocal = this . _persistence === 'local' ; this . _persistence = persistence ; const storage = createStorage ( persistence , Adapter . adapter ) ; for ( const key in this . keys ) { const name = this . keys [ key ] ; if ( isCurrentLocal && alwaysLocalKeys . includes ( key ) ) { continue ; } const val = this . _storage . getItem ( name ) ; if ( ! isUndefined ( val ) && ! isNull ( val ) ) { storage . setItem ( name , val ) ; this . _storage . removeItem ( name ) ; } } this . _storage = storage ; } public setStore ( key , value , version ? ) { if ( ! this . _storage ) { return ; } const d = { version : version || 'localCachev1' , content : value } ; const content = JSON . stringify ( d ) ; try { this . _storage . setItem ( key , content ) ; } catch ( e ) { throw e ; } return ; } getStore ( key , version ? ) { try { if ( typeof process !== 'undefined' && process . env && process . env . tcb_token ) { return process . env . tcb_token ; } if ( ! this . _storage ) { return ; } } catch ( e ) { return '' ; } version = version || 'localCachev1' ; const content = this . _storage . getItem ( key ) ; if ( ! content ) { return '' ; } if ( content . indexOf ( version ) >= 0 ) { const d = JSON . parse ( content ) ; return d . content ; } else { return '' ; } } removeStore ( key ) { this . _storage . removeItem ( key ) ; } } const cacheMap = { } ; const localCacheMap = { } ; function initCache ( config ) { const { env } = config ; cacheMap [ env ] = new ICache ( config ) ; localCacheMap [ env ] = new ICache ( { ... config , persistence : 'local' } ) ; } function getCache ( env ) { return cacheMap [ env ] ; } function getLocalCache ( env ) { return localCacheMap [ env ] ; } export { getCache , initCache , getLocalCache } ;