refactor(api): 收口 server/client 数据解析层,消除 aiGenerationClient 的 as T 断言

This commit is contained in:
2026-06-15 15:06:41 +08:00
parent 8985deea0a
commit 6dd292207f
4 changed files with 396 additions and 23 deletions
+13 -3
View File
@@ -82,9 +82,19 @@ function parseStoredSession(raw: string | null): WebUserSession | null {
try {
const parsed = JSON.parse(raw) as unknown;
return isRecord(parsed) && typeof parsed.token === "string" && isRecord(parsed.user)
? (parsed as unknown as WebUserSession)
: null;
// Require token + a user object with at least an id, so a malformed/partial
// cached session does not get cast wholesale into WebUserSession and then
// crash UI code that reads user.id / user.username.
if (!isRecord(parsed) || typeof parsed.token !== "string" || !isRecord(parsed.user)) {
return null;
}
const user = parsed.user;
const userId = user.id ?? user.userId ?? user.user_id;
const username = user.username ?? user.name;
if (userId === undefined || typeof username !== "string" || !username.trim()) {
return null;
}
return parsed as unknown as WebUserSession;
} catch {
return null;
}