@thoughtspot/visual-embed-sdk
Version:
ThoughtSpot Embed SDK
92 lines • 2.75 kB
JavaScript
import { tokenizedFetch } from '../../tokenizedFetch';
import { logger } from '../logger';
import { EndPoints } from './authService';
/**
*
* @param url
* @param options
*/
function tokenizedFailureLoggedFetch(url, options = {}) {
return tokenizedFetch(url, options).then(async (r) => {
var _a;
if (!r.ok && r.type !== 'opaqueredirect' && r.type !== 'opaque') {
logger.error(`Failed to fetch ${url}`, await ((_a = r.text) === null || _a === void 0 ? void 0 : _a.call(r)));
}
return r;
});
}
/**
* Fetches the session info from the ThoughtSpot server.
* @param thoughtspotHost
* @returns {Promise<any>}
* @example
* ```js
* const response = await sessionInfoService();
* ```
*/
export async function fetchPreauthInfoService(thoughtspotHost) {
const sessionInfoPath = `${thoughtspotHost}${EndPoints.PREAUTH_INFO}`;
const handleError = (e) => {
const error = new Error(`Failed to fetch auth info: ${e.message || e.statusText}`);
error.status = e.status; // Attach the status code to the error object
throw error;
};
try {
const response = await tokenizedFailureLoggedFetch(sessionInfoPath);
return response;
}
catch (e) {
handleError(e);
return null;
}
}
/**
* Fetches the session info from the ThoughtSpot server.
* @param thoughtspotHost
* @returns {Promise<any>}
* @example
* ```js
* const response = await sessionInfoService();
* ```
*/
export async function fetchSessionInfoService(thoughtspotHost) {
const sessionInfoPath = `${thoughtspotHost}${EndPoints.SESSION_INFO}`;
const response = await tokenizedFailureLoggedFetch(sessionInfoPath);
if (!response.ok) {
throw new Error(`Failed to fetch session info: ${response.statusText}`);
}
const data = await response.json();
return data;
}
/**
*
* @param thoughtSpotHost
*/
export async function fetchLogoutService(thoughtSpotHost) {
return tokenizedFailureLoggedFetch(`${thoughtSpotHost}${EndPoints.LOGOUT}`, {
credentials: 'include',
method: 'POST',
headers: {
'x-requested-by': 'ThoughtSpot',
},
});
}
/**
* Is active service to check if the user is logged in.
* @param thoughtSpotHost
* @version SDK: 1.28.4 | ThoughtSpot: *
*/
export async function isActiveService(thoughtSpotHost) {
const isActiveUrl = `${thoughtSpotHost}${EndPoints.IS_ACTIVE}`;
try {
const res = await tokenizedFetch(isActiveUrl, {
credentials: 'include',
});
return res.ok;
}
catch (e) {
logger.warn(`Is Logged In Service failed : ${e.message}`);
}
return false;
}
//# sourceMappingURL=tokenizedAuthService.js.map