refactor(api): 收口 server/client 数据解析层,消除 aiGenerationClient 的 as T 断言
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user