Фронт

This commit is contained in:
Халимов Рустам
2026-03-27 16:04:46 +03:00
parent 28fb8c25de
commit 030ae1e4e4
19 changed files with 234 additions and 174 deletions

View File

@@ -0,0 +1,67 @@
/**
* Глобальная нормализация данных пользователя.
* Приводит данные от разных модулей (Profiles, Auth, Message) к единому виду.
*
* ВАЖНО: Мы не импортируем здесь типы из types.ts, чтобы избежать круговых зависимостей.
*/
export function normalizeUser(user: any): any {
if (!user || typeof user !== 'object') return user;
const result = { ...user };
// 1. Приведение username (Auth -> userName, Profiles -> userName)
if (!result.userName && result.username) {
result.userName = result.username;
}
if (!result.username && result.userName) {
result.username = result.userName;
}
// 2. Приведение avatar (Auth -> avatar, Profiles -> avatarUrl)
if (!result.avatarUrl && result.avatar) {
result.avatarUrl = result.avatar;
}
if (!result.avatar && result.avatarUrl) {
result.avatar = result.avatarUrl;
}
return result;
}
/**
* Рекурсивная нормализация любого объекта/массива на наличие полей пользователя.
*/
export function deepNormalize(obj: any): any {
if (!obj || typeof obj !== 'object') {
return obj;
}
// Если это File, Blob или FormData - не трогаем
if (obj instanceof Blob || obj instanceof FormData) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(deepNormalize);
}
// Создаем копию для мутации
const result = { ...obj };
// Если это объект, похожий на пользователя (есть id и userName/username)
if (result.id && (result.userName || result.username)) {
const normalized = normalizeUser(result);
// Продолжаем рекурсию по остальным полям (например, если у пользователя есть вложенные объекты)
for (const key in normalized) {
normalized[key] = deepNormalize(normalized[key]);
}
return normalized;
}
// Рекурсивно проходим по всем полям
for (const key in result) {
result[key] = deepNormalize(result[key]);
}
return result;
}