Files
knowai/auth/storage-adapter.ts
tobegold574 6a81b7bb13
Some checks reported errors
continuous-integration/drone/push Build was killed
feat(image): 新建 knowai-core:1.0.0 镜像并完成推送
- 搭建 api、auth、utils 等逻辑模块
- 通过 tsc、eslint、vitest 测试验证

BREAKING CHANGE: 新镜像分支
2025-11-10 20:20:25 +08:00

56 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 存储适配器接口
* 提供统一的存储接口,用于非敏感数据的临时存储
* 注意此存储适配器不用于session管理session完全由服务器端控制
*/
import type { StorageAdapter } from './types';
/**
* 内存存储适配器
* 适用于非敏感数据的临时存储如UI状态、用户偏好设置等
*/
export class MemoryStorageAdapter implements StorageAdapter {
private storage: Record<string, string> = {};
getItem(key: string): string | null {
return this.storage[key] || null;
}
setItem(key: string, value: string): void {
this.storage[key] = value;
}
removeItem(key: string): void {
delete this.storage[key];
}
/**
* 清空所有存储项
*/
clear(): void {
this.storage = {};
}
/**
* 获取所有存储键
* @returns 存储键数组
*/
keys(): string[] {
return Object.keys(this.storage);
}
}
/**
* 创建存储适配器
* @param type 存储类型目前只支持memory
* @returns 存储适配器实例
*/
export function createStorageAdapter(type: 'memory' = 'memory'): StorageAdapter {
switch (type) {
case 'memory':
return new MemoryStorageAdapter();
default:
return new MemoryStorageAdapter();
}
}