2022-06-28 19:11:15 +00:00
|
|
|
import { browserStorageConfig } from './data';
|
2022-06-28 20:53:58 +00:00
|
|
|
import type { BrowserStorageType } from './types';
|
2022-06-28 19:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fake window for unit testing
|
|
|
|
*/
|
|
|
|
type FakeWindow = Record<string, typeof localStorage>;
|
|
|
|
|
|
|
|
let _window: FakeWindow =
|
|
|
|
typeof window === 'undefined' ? {} : (window as unknown as FakeWindow);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get browser storage
|
|
|
|
*/
|
|
|
|
export function getBrowserStorage(
|
2022-06-28 20:53:58 +00:00
|
|
|
key: BrowserStorageType
|
2022-06-28 19:11:15 +00:00
|
|
|
): typeof localStorage | undefined {
|
|
|
|
const attr = key + 'Storage';
|
|
|
|
try {
|
|
|
|
if (
|
|
|
|
_window &&
|
|
|
|
_window[attr] &&
|
|
|
|
typeof _window[attr].length === 'number'
|
|
|
|
) {
|
|
|
|
return _window[attr];
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failed - mark as disabled
|
|
|
|
browserStorageConfig[key] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock window for unit testing
|
|
|
|
*/
|
|
|
|
export function mockWindow(fakeWindow: FakeWindow): void {
|
|
|
|
_window = fakeWindow;
|
|
|
|
}
|