Skip to content

Commit

Permalink
feat(projects): add prefix to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 6, 2024
1 parent 2246f78 commit 0423cb1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ VITE_SERVICE_EXPIRED_TOKEN_CODES=9999,9998

# when the route mode is static, the defined super role
VITE_STATIC_SUPER_ROLE=R_SUPER

# Used to differentiate storage across different domains
VITE_STORAGE_PREFIX=SOY_
10 changes: 5 additions & 5 deletions packages/utils/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import localforage from 'localforage';
/** The storage type */
export type StorageType = 'local' | 'session';

export function createStorage<T extends object>(type: StorageType) {
export function createStorage<T extends object>(type: StorageType, storagePrefix: string) {
const stg = type === 'session' ? window.sessionStorage : window.localStorage;

const storage = {
Expand All @@ -16,15 +16,15 @@ export function createStorage<T extends object>(type: StorageType) {
set<K extends keyof T>(key: K, value: T[K]) {
const json = JSON.stringify(value);

stg.setItem(key as string, json);
stg.setItem(`${storagePrefix}${key as string}`, json);
},
/**
* Get session
*
* @param key Session key
*/
get<K extends keyof T>(key: K): T[K] | null {
const json = stg.getItem(key as string);
const json = stg.getItem(`${storagePrefix}${key as string}`);
if (json) {
let storageData: T[K] | null = null;

Expand All @@ -37,12 +37,12 @@ export function createStorage<T extends object>(type: StorageType) {
}
}

stg.removeItem(key as string);
stg.removeItem(`${storagePrefix}${key as string}`);

return null;
},
remove(key: keyof T) {
stg.removeItem(key as string);
stg.removeItem(`${storagePrefix}${key as string}`);
},
clear() {
stg.clear();
Expand Down
2 changes: 2 additions & 0 deletions src/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ declare namespace Env {
* @link https://docs.iconify.design/api/providers.html
*/
readonly VITE_ICONIFY_URL?: string;
/** Used to differentiate storage across different domains */
readonly VITE_STORAGE_PREFIX?: string;
}
}
6 changes: 4 additions & 2 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createLocalforage, createStorage } from '@sa/utils';

export const localStg = createStorage<StorageType.Local>('local');
const storagePrefix = import.meta.env.VITE_STORAGE_PREFIX || '';

export const sessionStg = createStorage<StorageType.Session>('session');
export const localStg = createStorage<StorageType.Local>('local', storagePrefix);

export const sessionStg = createStorage<StorageType.Session>('session', storagePrefix);

export const localforage = createLocalforage<StorageType.Local>('local');

0 comments on commit 0423cb1

Please sign in to comment.