1
0

index.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { getStorageShortName } from '/@/utils/env';
  2. import { createStorage as create, CreateStorageParams } from './storageCache';
  3. import { enableStorageEncryption } from '/@/settings/encryptionSetting';
  4. import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
  5. export type Options = Partial<CreateStorageParams>;
  6. const createOptions = (storage: Storage, options: Options = {}): Options => {
  7. return {
  8. // No encryption in debug mode
  9. hasEncrypt: enableStorageEncryption,
  10. storage,
  11. prefixKey: getStorageShortName(),
  12. ...options,
  13. };
  14. };
  15. export const WebStorage = create(createOptions(sessionStorage));
  16. export const createStorage = (storage: Storage = sessionStorage, options: Options = {}) => {
  17. return create(createOptions(storage, options));
  18. };
  19. export const createSessionStorage = (options: Options = {}) => {
  20. return createStorage(sessionStorage, { ...options, timeout: DEFAULT_CACHE_TIME });
  21. };
  22. export const createLocalStorage = (options: Options = {}) => {
  23. return createStorage(localStorage, { ...options, timeout: DEFAULT_CACHE_TIME });
  24. };
  25. export default WebStorage;