UNPKG

@thoughtspot/visual-embed-sdk

Version:
161 lines 5.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resetCachedPreauthInfo = exports.resetCachedSessionInfo = exports.getSessionDetails = exports.getCachedSessionInfo = exports.getSessionInfo = exports.getPreauthInfo = exports.formatPreauthInfo = void 0; const embedConfig_1 = require("../embed/embedConfig"); const authService_1 = require("./authService"); let sessionInfo = null; let preauthInfo = null; /** * Processes the session info response and returns the session info object. * @param preauthInfoResp {any} Response from the session info API. * @returns {PreauthInfo} The session info object. * @example ```js * const preauthInfoResp = await fetch(sessionInfoPath); * const sessionInfo = await formatPreauthInfo(preauthInfoResp); * console.log(sessionInfo); * ``` * @version SDK: 1.28.3 | ThoughtSpot: * */ const formatPreauthInfo = async (preauthInfoResp) => { var _a; try { // Convert Headers to a plain object const headers = {}; (_a = preauthInfoResp === null || preauthInfoResp === void 0 ? void 0 : preauthInfoResp.headers) === null || _a === void 0 ? void 0 : _a.forEach((value, key) => { headers[key] = value; }); const data = await preauthInfoResp.json(); return { ...data, status: 200, headers, }; } catch (error) { return null; } }; exports.formatPreauthInfo = formatPreauthInfo; /** * Returns the session info object and caches it for future use. * Once fetched the session info object is cached and returned from the cache on * subsequent calls. * @example ```js * const preauthInfo = await getPreauthInfo(); * console.log(preauthInfo); * ``` * @version SDK: 1.28.3 | ThoughtSpot: * * @returns {Promise<SessionInfo>} The session info object. */ async function getPreauthInfo(allowCache = true) { if (!allowCache || !preauthInfo) { try { const host = (0, embedConfig_1.getEmbedConfig)().thoughtSpotHost; const sessionResponse = await (0, authService_1.fetchPreauthInfoService)(host); const processedPreauthInfo = await (0, exports.formatPreauthInfo)(sessionResponse); preauthInfo = processedPreauthInfo; } catch (error) { return null; } } return preauthInfo; } exports.getPreauthInfo = getPreauthInfo; /** * Returns the cached session info object and caches it for future use. * Once fetched the session info object is cached and returned from the cache on * subsequent calls. * This cache is cleared when inti is called OR resetCachedSessionInfo is called. * @example ```js * const sessionInfo = await getSessionInfo(); * console.log(sessionInfo); * ``` * @version SDK: 1.28.3 | ThoughtSpot: * * @returns {Promise<SessionInfo>} The session info object. */ async function getSessionInfo() { if (!sessionInfo) { const host = (0, embedConfig_1.getEmbedConfig)().thoughtSpotHost; const sessionResponse = await (0, authService_1.fetchSessionInfoService)(host); const processedSessionInfo = (0, exports.getSessionDetails)(sessionResponse); sessionInfo = processedSessionInfo; } return sessionInfo; } exports.getSessionInfo = getSessionInfo; /** * Returns the cached session info object. If the client is not authenticated the * function will return null. * @example ```js * const sessionInfo = getCachedSessionInfo(); * if (sessionInfo) { * console.log(sessionInfo); * } else { * console.log('Not authenticated'); * } * ``` * @returns {SessionInfo | null} The session info object. * @version SDK: 1.28.3 | ThoughtSpot: * */ function getCachedSessionInfo() { return sessionInfo; } exports.getCachedSessionInfo = getCachedSessionInfo; /** * Processes the session info response and returns the session info object. * @param sessionInfoResp {any} Response from the session info API. * @returns {SessionInfo} The session info object. * @example ```js * const sessionInfoResp = await fetch(sessionInfoPath); * const sessionInfo = getSessionDetails(sessionInfoResp); * console.log(sessionInfo); * ``` * @version SDK: 1.28.3 | ThoughtSpot: * */ const getSessionDetails = (sessionInfoResp) => { const devMixpanelToken = sessionInfoResp.configInfo.mixpanelConfig.devSdkKey; const prodMixpanelToken = sessionInfoResp.configInfo.mixpanelConfig.prodSdkKey; const mixpanelToken = sessionInfoResp.configInfo.mixpanelConfig.production ? prodMixpanelToken : devMixpanelToken; return { userGUID: sessionInfoResp.userGUID, mixpanelToken, isPublicUser: sessionInfoResp.configInfo.isPublicUser, releaseVersion: sessionInfoResp.releaseVersion, clusterId: sessionInfoResp.configInfo.selfClusterId, clusterName: sessionInfoResp.configInfo.selfClusterName, ...sessionInfoResp, }; }; exports.getSessionDetails = getSessionDetails; /** * Resets the cached session info object and forces a new fetch on the next call. * @example ```js * resetCachedSessionInfo(); * const sessionInfo = await getSessionInfo(); * console.log(sessionInfo); * ``` * @version SDK: 1.28.3 | ThoughtSpot: * * @returns {void} */ function resetCachedSessionInfo() { sessionInfo = null; } exports.resetCachedSessionInfo = resetCachedSessionInfo; /** * Resets the cached preauth info object and forces a new fetch on the next call. * @example ```js * resetCachedPreauthInfo(); * const preauthInfo = await getPreauthInfo(); * console.log(preauthInfo); * ``` * @version SDK: 1.28.3 | ThoughtSpot: * * @returns {void} */ function resetCachedPreauthInfo() { preauthInfo = null; } exports.resetCachedPreauthInfo = resetCachedPreauthInfo; //# sourceMappingURL=sessionInfoService.js.map