UNPKG

@thoughtspot/visual-embed-sdk

Version:
99 lines 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isActiveService = exports.fetchLogoutService = exports.fetchSessionInfoService = exports.fetchPreauthInfoService = void 0; const tokenizedFetch_1 = require("../../tokenizedFetch"); const logger_1 = require("../logger"); const authService_1 = require("./authService"); /** * * @param url * @param options */ function tokenizedFailureLoggedFetch(url, options = {}) { return (0, tokenizedFetch_1.tokenizedFetch)(url, options).then(async (r) => { var _a; if (!r.ok && r.type !== 'opaqueredirect' && r.type !== 'opaque') { logger_1.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(); * ``` */ async function fetchPreauthInfoService(thoughtspotHost) { const sessionInfoPath = `${thoughtspotHost}${authService_1.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; } } exports.fetchPreauthInfoService = fetchPreauthInfoService; /** * Fetches the session info from the ThoughtSpot server. * @param thoughtspotHost * @returns {Promise<any>} * @example * ```js * const response = await sessionInfoService(); * ``` */ async function fetchSessionInfoService(thoughtspotHost) { const sessionInfoPath = `${thoughtspotHost}${authService_1.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; } exports.fetchSessionInfoService = fetchSessionInfoService; /** * * @param thoughtSpotHost */ async function fetchLogoutService(thoughtSpotHost) { return tokenizedFailureLoggedFetch(`${thoughtSpotHost}${authService_1.EndPoints.LOGOUT}`, { credentials: 'include', method: 'POST', headers: { 'x-requested-by': 'ThoughtSpot', }, }); } exports.fetchLogoutService = fetchLogoutService; /** * Is active service to check if the user is logged in. * @param thoughtSpotHost * @version SDK: 1.28.4 | ThoughtSpot: * */ async function isActiveService(thoughtSpotHost) { const isActiveUrl = `${thoughtSpotHost}${authService_1.EndPoints.IS_ACTIVE}`; try { const res = await (0, tokenizedFetch_1.tokenizedFetch)(isActiveUrl, { credentials: 'include', }); return res.ok; } catch (e) { logger_1.logger.warn(`Is Logged In Service failed : ${e.message}`); } return false; } exports.isActiveService = isActiveService; //# sourceMappingURL=tokenizedAuthService.js.map