chore(deps): add unstorage and migrate ChatStorage to use SpAsyncUtil
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* UserStorage 完整实现(KV / localStorage)
|
||||
* UserStorage 完整实现(基于 unstorage 抽象)
|
||||
*
|
||||
* 对齐 Dart 端 `UserStorage`(lib/data/services/storage/user_storage.dart):
|
||||
* - 三个独立字段:User 对象 / userId / avatarUrl
|
||||
* - `clearUserData()` 批量清空
|
||||
*
|
||||
* 设计沿用 AuthStorage 模式:组合 `LocalStorage`(非继承)+ 单例 + Zod 校验。
|
||||
* 设计:内部使用 SpAsyncUtil 静态类 + Zod 校验;保持对外 API 完全兼容。
|
||||
*
|
||||
* 边界:
|
||||
* - `getUser` 走 `UserSchema` 解析:缺字段时 Zod `.default()` 自动兜底
|
||||
@@ -17,18 +17,13 @@
|
||||
|
||||
import { UserSchema, type UserData } from "@/data/schemas/user/user";
|
||||
import { type Result as ResultT } from "@/utils/result";
|
||||
import { SpAsyncUtil } from "@/utils/storage";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import type { IUserStorage } from "./iuser_storage";
|
||||
import { LocalStorage } from "@/utils/local-storage";
|
||||
|
||||
export class UserStorage implements IUserStorage {
|
||||
private readonly ls: LocalStorage;
|
||||
private static _instance: UserStorage | null = null;
|
||||
|
||||
constructor(localStorage?: LocalStorage) {
|
||||
this.ls = localStorage ?? LocalStorage.getInstance();
|
||||
}
|
||||
|
||||
static getInstance(): UserStorage {
|
||||
if (!UserStorage._instance) UserStorage._instance = new UserStorage();
|
||||
return UserStorage._instance;
|
||||
@@ -42,43 +37,43 @@ export class UserStorage implements IUserStorage {
|
||||
// ---- user object ----
|
||||
|
||||
getUser(): Promise<ResultT<UserData | null>> {
|
||||
return this.ls.getJson(StorageKeys.user, UserSchema);
|
||||
return SpAsyncUtil.getJson(StorageKeys.user, UserSchema);
|
||||
}
|
||||
|
||||
setUser(user: UserData): Promise<ResultT<void>> {
|
||||
return this.ls.setJson(StorageKeys.user, user, UserSchema);
|
||||
return SpAsyncUtil.setJson(StorageKeys.user, user, UserSchema);
|
||||
}
|
||||
|
||||
clearUser(): Promise<ResultT<void>> {
|
||||
return this.ls.remove(StorageKeys.user);
|
||||
return SpAsyncUtil.remove(StorageKeys.user);
|
||||
}
|
||||
|
||||
// ---- userId ----
|
||||
|
||||
getUserId(): Promise<ResultT<string | null>> {
|
||||
return this.ls.getString(StorageKeys.userId);
|
||||
return SpAsyncUtil.getString(StorageKeys.userId);
|
||||
}
|
||||
|
||||
setUserId(id: string): Promise<ResultT<void>> {
|
||||
return this.ls.setString(StorageKeys.userId, id);
|
||||
return SpAsyncUtil.setString(StorageKeys.userId, id);
|
||||
}
|
||||
|
||||
clearUserId(): Promise<ResultT<void>> {
|
||||
return this.ls.remove(StorageKeys.userId);
|
||||
return SpAsyncUtil.remove(StorageKeys.userId);
|
||||
}
|
||||
|
||||
// ---- avatarUrl ----
|
||||
|
||||
getAvatarUrl(): Promise<ResultT<string | null>> {
|
||||
return this.ls.getString(StorageKeys.userAvatar);
|
||||
return SpAsyncUtil.getString(StorageKeys.userAvatar);
|
||||
}
|
||||
|
||||
setAvatarUrl(url: string): Promise<ResultT<void>> {
|
||||
return this.ls.setString(StorageKeys.userAvatar, url);
|
||||
return SpAsyncUtil.setString(StorageKeys.userAvatar, url);
|
||||
}
|
||||
|
||||
clearAvatarUrl(): Promise<ResultT<void>> {
|
||||
return this.ls.remove(StorageKeys.userAvatar);
|
||||
return SpAsyncUtil.remove(StorageKeys.userAvatar);
|
||||
}
|
||||
|
||||
// ---- bulk clear ----
|
||||
|
||||
Reference in New Issue
Block a user