@thoughtspot/visual-embed-sdk
Version:
ThoughtSpot Embed SDK
151 lines • 5.2 kB
JavaScript
import { getEmbedConfig } from '../embed/embedConfig';
import { fetchSessionInfoService, fetchPreauthInfoService } from './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: *
*/
export 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;
}
};
/**
* 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.
*/
export async function getPreauthInfo(allowCache = true) {
if (!allowCache || !preauthInfo) {
try {
const host = getEmbedConfig().thoughtSpotHost;
const sessionResponse = await fetchPreauthInfoService(host);
const processedPreauthInfo = await formatPreauthInfo(sessionResponse);
preauthInfo = processedPreauthInfo;
}
catch (error) {
return null;
}
}
return preauthInfo;
}
/**
* 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.
*/
export async function getSessionInfo() {
if (!sessionInfo) {
const host = getEmbedConfig().thoughtSpotHost;
const sessionResponse = await fetchSessionInfoService(host);
const processedSessionInfo = getSessionDetails(sessionResponse);
sessionInfo = processedSessionInfo;
}
return sessionInfo;
}
/**
* 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: *
*/
export function getCachedSessionInfo() {
return sessionInfo;
}
/**
* 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: *
*/
export 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,
};
};
/**
* 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}
*/
export function resetCachedSessionInfo() {
sessionInfo = null;
}
/**
* 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}
*/
export function resetCachedPreauthInfo() {
preauthInfo = null;
}
//# sourceMappingURL=sessionInfoService.js.map