fix(projects): Modify the naming of storagePrefix

This commit is contained in:
恕瑞玛的皇帝 2024-04-25 10:39:34 +08:00
parent e660942367
commit 81afac449b
2 changed files with 8 additions and 8 deletions

View File

@ -3,7 +3,7 @@ import localforage from 'localforage';
/** The storage type */ /** The storage type */
export type StorageType = 'local' | 'session'; export type StorageType = 'local' | 'session';
export function createStorage<T extends object>(type: StorageType, StoragePrefix: string) { export function createStorage<T extends object>(type: StorageType, storagePrefix: string) {
const stg = type === 'session' ? window.sessionStorage : window.localStorage; const stg = type === 'session' ? window.sessionStorage : window.localStorage;
const storage = { const storage = {
@ -16,7 +16,7 @@ export function createStorage<T extends object>(type: StorageType, StoragePrefix
set<K extends keyof T>(key: K, value: T[K]) { set<K extends keyof T>(key: K, value: T[K]) {
const json = JSON.stringify(value); const json = JSON.stringify(value);
stg.setItem(`${StoragePrefix}${key as string}`, json); stg.setItem(`${storagePrefix}${key as string}`, json);
}, },
/** /**
* Get session * Get session
@ -24,7 +24,7 @@ export function createStorage<T extends object>(type: StorageType, StoragePrefix
* @param key Session key * @param key Session key
*/ */
get<K extends keyof T>(key: K): T[K] | null { get<K extends keyof T>(key: K): T[K] | null {
const json = stg.getItem(`${StoragePrefix}${key as string}`); const json = stg.getItem(`${storagePrefix}${key as string}`);
if (json) { if (json) {
let storageData: T[K] | null = null; let storageData: T[K] | null = null;
@ -37,12 +37,12 @@ export function createStorage<T extends object>(type: StorageType, StoragePrefix
} }
} }
stg.removeItem(`${StoragePrefix}${key as string}`); stg.removeItem(`${storagePrefix}${key as string}`);
return null; return null;
}, },
remove(key: keyof T) { remove(key: keyof T) {
stg.removeItem(`${StoragePrefix}${key as string}`); stg.removeItem(`${storagePrefix}${key as string}`);
}, },
clear() { clear() {
stg.clear(); stg.clear();

View File

@ -1,9 +1,9 @@
import { createLocalforage, createStorage } from '@sa/utils'; import { createLocalforage, createStorage } from '@sa/utils';
const StoragePrefix = import.meta.env.VITE_STORAGE_PREFIX || ''; const storagePrefix = import.meta.env.VITE_STORAGE_PREFIX || '';
export const localStg = createStorage<StorageType.Local>('local', StoragePrefix); export const localStg = createStorage<StorageType.Local>('local', storagePrefix);
export const sessionStg = createStorage<StorageType.Session>('session', StoragePrefix); export const sessionStg = createStorage<StorageType.Session>('session', storagePrefix);
export const localforage = createLocalforage<StorageType.Local>('local'); export const localforage = createLocalforage<StorageType.Local>('local');