@dtinsight/dt-utils
Version:
94 lines (93 loc) • 3.52 kB
TypeScript
/**
* 一个用于管理浏览器 Session 存储的工具类,提供类型安全的方法
*
* @category Storage
* @description
* SessionDB 为浏览器的 sessionStorage API 提供了一个包装器,具有以下附加功能:
* - 类型安全的数据存储和检索
* - 自动 JSON 序列化/反序列化
* - 批量操作支持
* - 可选择性清除带例外项
*
* @Methods
* | 方法名 | 描述 | 参数 | 返回值 |
* |------|------|------|--------|
* | `set(items: Record<string, any>)` | 向 sessionStorage 添加多个项目 | `items: Record<string, any>` - 键值对对象 | `void` |
* | `set(key: string, value: any)` | 通过键名在 sessionStorage 中存储单个数据值(如果 value 为 null/undefined,则删除该键) | `key: string` - 存储的唯一标识符 <br> `value: any` - 要存储的数据 | `void` |
* | `get` | 通过键名从 sessionStorage 中获取数据 | `key: string` - 要获取数据的唯一标识符 | `any`(可能是对象或字符串,找不到返回 null) |
* | `remove` | 通过键名从 sessionStorage 中删除数据 | `key: string | string[]` - 要删除数据的唯一标识符 | `void` |
* | `clear` | 清除 sessionStorage,可以选择性保留特定键 | `except?: string[]` - 可选的要保留的键数组 | `void` |
*
* @example
* ```typescript
* import { SessionDB } from 'dt-utils';
*
* // 存储简单值
* SessionDB.set('username', 'john_doe');
*
* // 存储对象
* SessionDB.set('userProfile', {
* id: 123,
* name: 'John Doe',
* preferences: { theme: 'dark' }
* });
*
* // 一次存储多个项目
* SessionDB.set({
* token: 'abc123',
* lastLogin: new Date().toISOString()
* });
*
* // 检索存储的数据
* const username = SessionDB.get('username'); // => 'john_doe'
* const profile = SessionDB.get('userProfile'); // => { id: 123, ... }
*
* // 删除特定项目
* SessionDB.remove('username');
* SessionDB.remove(['token', 'lastLogin']); // 删除多个项目
*
* // 清除所有项目,除了指定的键
* SessionDB.clear(['userProfile']); // 只保留 userProfile
* ```
*/
declare class SessionDB {
/**
* @hidden
*/
constructor();
/**
* @hidden
* 向 sessionStorage 添加多个项目
* @param {Record<string, any>} items - 一个对象,其中键是 sessionStorage 的键,值是要存储的值
*/
static set(items: Record<string, any>): void;
/**
* @hidden
* 通过键在 sessionStorage 中存储数据值
* @param {string} key - 存储数据的唯一标识符
* @param {any} value - 要存储的数据,可以是任何类型
*/
static set(key: string, value: any): void;
/**
* @hidden
* 通过键从 sessionStorage 检索数据
* @param {string} key - 要检索的数据的唯一标识符
* @return {any} - 返回存储的数据,可以是字符串或对象
*/
static get(key: string): any;
/**
* @hidden
* 通过键从 sessionStorage 删除数据
* @param {string | string[]} key - 要删除的数据的唯一标识符
*/
static remove(key: string | string[]): void;
/**
* @hidden
* 清除 sessionStorage,同时可以选择性地保留特定的键。
*
* @param {string[]} [except] - 要在 sessionStorage 中保留的键的可选数组。
* 如果提供了该参数,只有不在此数组中的键会被删除。
*/
static clear(except?: string[]): void;
}
export default SessionDB;