Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCAL-244609: Move all store to window instead of file #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/authToken.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { ERROR_MESSAGE } from './errors';
import { EmbedConfig } from './types';
import { getValueFromWindow, storeValueInWindow } from './utils';
import { fetchAuthTokenService, verifyTokenService } from './utils/authService/authService';
import { logger } from './utils/logger';

let cachedAuthToken: string | null = null;
const cacheAuthTokenKey = 'cachedAuthToken';

const getCacheAuthToken = (): string | null => getValueFromWindow(cacheAuthTokenKey);
const storeAuthTokenInCache = (token: string): void => {
storeValueInWindow(cacheAuthTokenKey, token);
};

// This method can be used to get the authToken using the embedConfig
/**
*
* @param embedConfig
*/
export async function getAuthenticationToken(embedConfig: EmbedConfig): Promise<string> {
const cachedAuthToken = getCacheAuthToken();
// Since we don't have token validation enabled , we cannot tell if the
// cached token is valid or not. So we will always fetch a new token.
if (cachedAuthToken && !embedConfig.disableTokenVerification) {
Expand Down Expand Up @@ -42,7 +49,7 @@ export async function getAuthenticationToken(embedConfig: EmbedConfig): Promise<
throw e;
}

cachedAuthToken = authToken;
storeAuthTokenInCache(authToken);
return authToken;
}

Expand All @@ -51,6 +58,7 @@ const validateAuthToken = async (
authToken: string,
suppressAlert?: boolean,
): Promise<boolean> => {
const cachedAuthToken = getCacheAuthToken();
if (embedConfig.disableTokenVerification) {
logger.info('Token verification is disabled. Assuming token is valid.');
return true;
Expand Down Expand Up @@ -83,5 +91,5 @@ const validateAuthToken = async (
* @group Authentication / Init
*/
export const resetCachedAuthToken = (): void => {
cachedAuthToken = null;
storeAuthTokenInCache(null);
};
9 changes: 5 additions & 4 deletions src/embed/embedConfig.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { getValueFromWindow, storeValueInWindow } from '../utils';
import { EmbedConfig } from '../types';

let config = {} as EmbedConfig;
const configKey = 'embedConfig';

/**
* Gets the configuration embed was initialized with.
* @returns {@link EmbedConfig} The configuration embed was initialized with.
* @version SDK: 1.19.0 | ThoughtSpot: *
* @group Global methods
*/
export const getEmbedConfig = (): EmbedConfig => config;
export const getEmbedConfig = (): EmbedConfig => getValueFromWindow(configKey) || {};

/**
* Sets the configuration embed was initialized with.
Expand All @@ -18,6 +19,6 @@ export const getEmbedConfig = (): EmbedConfig => config;
* @group Global methods
*/
export const setEmbedConfig = (newConfig: EmbedConfig) => {
config = newConfig;
return newConfig;
storeValueInWindow(configKey, newConfig);
return getValueFromWindow(configKey);
};
21 changes: 21 additions & 0 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
removeStyleProperties,
setStyleProperties,
isUndefined,
storeValueInWindow,
getValueFromWindow,
} from './utils';
import { RuntimeFilterOp } from './types';

Expand Down Expand Up @@ -272,4 +274,23 @@ describe('unit test for utils', () => {
expect(isUndefined(undefined)).toBe(true);
expect(isUndefined({})).toBe(false);
});

describe('getValueFromWindow and storeValueInWindow', () => {
test('Store and retrieve', () => {
storeValueInWindow('test', 'testValue');
expect(getValueFromWindow('test')).toBe('testValue');
});

test('Object should be set if not', () => {
// eslint-disable-next-line no-underscore-dangle
(window as any)._tsEmbedSDK = null;

storeValueInWindow('test', 'testValue');
expect(getValueFromWindow('test')).toBe('testValue');
});

test('Return undefined if key is not found', () => {
expect(getValueFromWindow('notFound')).toBe(undefined);
});
});
});
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,24 @@ export const getTypeFromValue = (value: any): [string, string] => {
}
return ['', ''];
};

const sdkWindowKey = '_tsEmbedSDK' as any;

/**
* Stores a value in the global `window` object under the `_tsEmbedSDK` namespace.
* @param key - The key under which the value will be stored.
* @param value - The value to store.
*/
export const storeValueInWindow = (key: string, value: any) => {
if (!window[sdkWindowKey]) {
(window as any)[sdkWindowKey] = {};
}
window[sdkWindowKey][key as any] = value;
};

/**
* Retrieves a stored value from the global `window` object under the `_tsEmbedSDK` namespace.
* @param key - The key whose value needs to be retrieved.
* @returns The stored value or `undefined` if the key is not found.
*/
export const getValueFromWindow = (key: string): any => window?.[sdkWindowKey]?.[key as any];
Loading