class TokenInfo { constructor(public accessToken: string, public expiresAt: Date) {} } export class TokenCache { private tokenMap: Map constructor() { this.tokenMap = new Map() } set({ scopes, clientId, expiresIn }: { scopes: string[]; clientId: string; expiresIn: number }, token: string) { const date = new Date() date.setSeconds(date.getSeconds() + expiresIn) const tokenInfo = new TokenInfo(token, date) const key = this._getTokenCacheKey({ scopes, clientId }) this.tokenMap.set(key, tokenInfo) } get({ scopes, clientId }: { scopes: string[]; clientId: string }): string | undefined { const key = this._getTokenCacheKey({ scopes, clientId }) const token = this.tokenMap.get(key) if (!token || token.expiresAt <= new Date()) return return token.accessToken } private _getTokenCacheKey({ scopes, clientId }: { scopes: string[]; clientId: string }) { const scopesKey = scopes.join('-') return `${scopesKey}:${clientId}` } }