feat: setup barrelsby for automatic barrel file generation

Add barrelsby as a dev dependency with a `generate-barrels` npm script and a `barrelesby.json` config targeting `./src`. This automates creation of barrel/index files to simplify imports across the project.
This commit is contained in:
2026-06-08 13:07:37 +08:00
parent 14d6e7c41e
commit c22b90c7f4
41 changed files with 2165 additions and 1 deletions
+35
View File
@@ -0,0 +1,35 @@
/**
* 头像数据模型 - 对应后端 AvatarData schema
* 原始 Dart: AvatarData (lib/data/models/user/user.dart)
*
* 注:原始 Dart 中 `avatarUrl` 为 `String?`,按"全部非空"约定兜底为空串。
*/
export class AvatarData {
readonly avatarUrl: string;
readonly userId: string;
constructor(params: { avatarUrl?: string; userId: string }) {
this.avatarUrl = params.avatarUrl ?? "";
this.userId = params.userId;
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
avatarUrl: this.avatarUrl,
userId: this.userId,
};
}
static fromJson(json: Record<string, unknown>): AvatarData {
const userId = json.userId;
if (typeof userId !== "string") {
throw new Error("AvatarData.userId is required and must be a string");
}
return new AvatarData({
userId,
avatarUrl:
typeof json.avatarUrl === "string" ? json.avatarUrl : undefined,
});
}
}
+35
View File
@@ -0,0 +1,35 @@
/**
* 积分数据模型 - 对应后端 CreditsData schema
* 原始 Dart: CreditsData (lib/data/models/user/user.dart)
*/
export class CreditsData {
readonly userId: string;
readonly dolBalance: number;
constructor(params: { userId: string; dolBalance: number }) {
this.userId = params.userId;
this.dolBalance = params.dolBalance;
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
userId: this.userId,
dolBalance: this.dolBalance,
};
}
static fromJson(json: Record<string, unknown>): CreditsData {
const userId = json.userId;
const dolBalance = json.dolBalance;
if (typeof userId !== "string") {
throw new Error("CreditsData.userId is required and must be a string");
}
if (typeof dolBalance !== "number") {
throw new Error(
"CreditsData.dolBalance is required and must be a number"
);
}
return new CreditsData({ userId, dolBalance });
}
}
@@ -0,0 +1,57 @@
/**
* 积分历史记录模型 - 对应后端 CreditsHistoryData schema
* 原始 Dart: CreditsHistoryData (lib/data/models/user/user.dart)
*
* 注:原始 Dart 中 `records` 为 `List<Map<String, dynamic>>`
* 迁移为 `Record<string, unknown>[]`。
*/
export class CreditsHistoryData {
readonly records: Record<string, unknown>[];
readonly total: number;
readonly limit: number;
readonly offset: number;
constructor(params: {
records: Record<string, unknown>[];
total: number;
limit: number;
offset: number;
}) {
this.records = params.records;
this.total = params.total;
this.limit = params.limit;
this.offset = params.offset;
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
records: this.records,
total: this.total,
limit: this.limit,
offset: this.offset,
};
}
static fromJson(json: Record<string, unknown>): CreditsHistoryData {
const requireNumber = (key: string): number => {
const v = json[key];
if (typeof v !== "number") {
throw new Error(
`CreditsHistoryData.${key} is required and must be a number`
);
}
return v;
};
const rawRecords = json.records;
const records: Record<string, unknown>[] = Array.isArray(rawRecords)
? (rawRecords as Record<string, unknown>[])
: [];
return new CreditsHistoryData({
records,
total: requireNumber("total"),
limit: requireNumber("limit"),
offset: requireNumber("offset"),
});
}
}
+9
View File
@@ -0,0 +1,9 @@
// user 子目录 barrel export
export { AvatarData } from "./avatar_data";
export { CreditsData } from "./credits_data";
export { CreditsHistoryData } from "./credits_history_data";
export { PersonalityTraits } from "./personality_traits";
export { RecentMemory } from "./recent_memory";
export { UpdateProfileRequest } from "./update_profile_request";
export { User } from "./user";
export { UserStatsResponse } from "./user_stats_response";
@@ -0,0 +1,48 @@
/**
* 个性特征模型
* 原始 Dart: PersonalityTraits (lib/data/models/user/user.dart)
*
* 注:所有 double 字段在 Dart 中使用 @Default(0.5)。
*/
export class PersonalityTraits {
readonly cheerful: number;
readonly caring: number;
readonly playful: number;
readonly serious: number;
readonly romantic: number;
constructor(params: {
cheerful?: number;
caring?: number;
playful?: number;
serious?: number;
romantic?: number;
}) {
this.cheerful = params.cheerful ?? 0.5;
this.caring = params.caring ?? 0.5;
this.playful = params.playful ?? 0.5;
this.serious = params.serious ?? 0.5;
this.romantic = params.romantic ?? 0.5;
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
cheerful: this.cheerful,
caring: this.caring,
playful: this.playful,
serious: this.serious,
romantic: this.romantic,
};
}
static fromJson(json: Record<string, unknown>): PersonalityTraits {
return new PersonalityTraits({
cheerful: typeof json.cheerful === "number" ? json.cheerful : undefined,
caring: typeof json.caring === "number" ? json.caring : undefined,
playful: typeof json.playful === "number" ? json.playful : undefined,
serious: typeof json.serious === "number" ? json.serious : undefined,
romantic: typeof json.romantic === "number" ? json.romantic : undefined,
});
}
}
+59
View File
@@ -0,0 +1,59 @@
/**
* 最近记忆模型
* 原始 Dart: RecentMemory (lib/data/models/user/recent_memory.dart)
*
* 注:原始 Dart 中 `createdAt` 为 `String?`,按"全部非空"约定兜底为空串。
*/
export class RecentMemory {
readonly type: string;
readonly content: string;
readonly importance: number;
readonly createdAt: string;
constructor(params: {
type: string;
content: string;
importance: number;
createdAt?: string;
}) {
this.type = params.type;
this.content = params.content;
this.importance = params.importance;
this.createdAt = params.createdAt ?? "";
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
type: this.type,
content: this.content,
importance: this.importance,
createdAt: this.createdAt,
};
}
static fromJson(json: Record<string, unknown>): RecentMemory {
const requireString = (key: string): string => {
const v = json[key];
if (typeof v !== "string") {
throw new Error(
`RecentMemory.${key} is required and must be a string`
);
}
return v;
};
const importance = json.importance;
if (typeof importance !== "number") {
throw new Error(
"RecentMemory.importance is required and must be a number"
);
}
return new RecentMemory({
type: requireString("type"),
content: requireString("content"),
importance,
createdAt:
typeof json.createdAt === "string" ? json.createdAt : undefined,
});
}
}
@@ -0,0 +1,47 @@
/**
* 更新个人资料请求
* 原始 Dart: UpdateProfileRequest (lib/data/models/user/user.dart)
*
* 注:原始 Dart 中所有字段为 `String?`,按"全部非空"约定兜底为空串。
*/
export class UpdateProfileRequest {
readonly username: string;
readonly nickname: string;
readonly preferredLanguage: string;
readonly currentMood: string;
constructor(params: {
username?: string;
nickname?: string;
preferredLanguage?: string;
currentMood?: string;
}) {
this.username = params.username ?? "";
this.nickname = params.nickname ?? "";
this.preferredLanguage = params.preferredLanguage ?? "";
this.currentMood = params.currentMood ?? "";
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
username: this.username,
nickname: this.nickname,
preferredLanguage: this.preferredLanguage,
currentMood: this.currentMood,
};
}
static fromJson(json: Record<string, unknown>): UpdateProfileRequest {
return new UpdateProfileRequest({
username: typeof json.username === "string" ? json.username : undefined,
nickname: typeof json.nickname === "string" ? json.nickname : undefined,
preferredLanguage:
typeof json.preferredLanguage === "string"
? json.preferredLanguage
: undefined,
currentMood:
typeof json.currentMood === "string" ? json.currentMood : undefined,
});
}
}
+128
View File
@@ -0,0 +1,128 @@
/**
* 用户模型 - 对应后端 UserData schema
* 原始 Dart: User (lib/data/models/user/user.dart)
*
* 注:原始 Dart 中 `email`/`personalityTraits`/`preferredLanguage`/`createdAt`/
* `lastMessageAt`/`avatarUrl` 为可空字段,按"全部非空"约定统一兜底:
* - 字符串字段 → 空串 ""
* - PersonalityTraits → 默认实例(全 0.5
*/
import { PersonalityTraits } from "./personality_traits";
export class User {
readonly id: string;
readonly username: string;
readonly email: string;
readonly platform: string;
readonly intimacy: number;
readonly dolBalance: number;
readonly relationshipStage: string;
readonly currentMood: string;
readonly personalityTraits: PersonalityTraits;
readonly preferredLanguage: string;
readonly createdAt: string;
readonly lastMessageAt: string;
readonly avatarUrl: string;
readonly loginProvider: string;
readonly isGuest: boolean;
constructor(params: {
id: string;
username: string;
email?: string;
platform?: string;
intimacy?: number;
dolBalance?: number;
relationshipStage?: string;
currentMood?: string;
personalityTraits?: PersonalityTraits;
preferredLanguage?: string;
createdAt?: string;
lastMessageAt?: string;
avatarUrl?: string;
loginProvider?: string;
isGuest?: boolean;
}) {
this.id = params.id;
this.username = params.username;
this.email = params.email ?? "";
this.platform = params.platform ?? "web";
this.intimacy = params.intimacy ?? 0;
this.dolBalance = params.dolBalance ?? 0;
this.relationshipStage = params.relationshipStage ?? "密友";
this.currentMood = params.currentMood ?? "happy";
this.personalityTraits = params.personalityTraits ?? new PersonalityTraits({});
this.preferredLanguage = params.preferredLanguage ?? "";
this.createdAt = params.createdAt ?? "";
this.lastMessageAt = params.lastMessageAt ?? "";
this.avatarUrl = params.avatarUrl ?? "";
this.loginProvider = params.loginProvider ?? "email";
this.isGuest = params.isGuest ?? false;
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
id: this.id,
username: this.username,
email: this.email,
platform: this.platform,
intimacy: this.intimacy,
dolBalance: this.dolBalance,
relationshipStage: this.relationshipStage,
currentMood: this.currentMood,
personalityTraits: this.personalityTraits.toJson(),
preferredLanguage: this.preferredLanguage,
createdAt: this.createdAt,
lastMessageAt: this.lastMessageAt,
avatarUrl: this.avatarUrl,
loginProvider: this.loginProvider,
isGuest: this.isGuest,
};
}
static fromJson(json: Record<string, unknown>): User {
const requireString = (key: string): string => {
const v = json[key];
if (typeof v !== "string") {
throw new Error(`User.${key} is required and must be a string`);
}
return v;
};
return new User({
id: requireString("id"),
username: requireString("username"),
email: typeof json.email === "string" ? json.email : undefined,
platform: typeof json.platform === "string" ? json.platform : undefined,
intimacy: typeof json.intimacy === "number" ? json.intimacy : undefined,
dolBalance:
typeof json.dolBalance === "number" ? json.dolBalance : undefined,
relationshipStage:
typeof json.relationshipStage === "string"
? json.relationshipStage
: undefined,
currentMood:
typeof json.currentMood === "string" ? json.currentMood : undefined,
personalityTraits: json.personalityTraits
? PersonalityTraits.fromJson(
json.personalityTraits as Record<string, unknown>
)
: undefined,
preferredLanguage:
typeof json.preferredLanguage === "string"
? json.preferredLanguage
: undefined,
createdAt:
typeof json.createdAt === "string" ? json.createdAt : undefined,
lastMessageAt:
typeof json.lastMessageAt === "string" ? json.lastMessageAt : undefined,
avatarUrl:
typeof json.avatarUrl === "string" ? json.avatarUrl : undefined,
loginProvider:
typeof json.loginProvider === "string"
? json.loginProvider
: undefined,
isGuest: typeof json.isGuest === "boolean" ? json.isGuest : undefined,
});
}
}
+156
View File
@@ -0,0 +1,156 @@
/**
* 用户统计响应
* 原始 Dart: UserStatsResponse (lib/data/models/user/user_stats_response.dart)
*
* 注:原始 Dart 中 `lastMessageAt`/`accountCreatedAt`/`currentMood` 为 `String?`
* `personalityTraits` 为 `PersonalityTraits?`,按"全部非空"约定兜底:
* - 字符串 → ""
* - PersonalityTraits → 默认实例
* - recentMemories 缺失或 null 时使用 []
*/
import { PersonalityTraits } from "./personality_traits";
import { RecentMemory } from "./recent_memory";
export class UserStatsResponse {
readonly userId: string;
readonly username: string;
readonly platform: string;
readonly intimacy: number;
readonly relationshipStage: string;
readonly dolBalance: number;
readonly totalMessages: number;
readonly lastMessageAt: string;
readonly accountCreatedAt: string;
readonly currentMood: string;
readonly personalityTraits: PersonalityTraits;
readonly recentMemories: RecentMemory[];
readonly promptTokensTotal: number;
readonly completionTokensTotal: number;
readonly cacheHitTokensTotal: number;
readonly tokensTotal: number;
constructor(params: {
userId: string;
username: string;
platform: string;
intimacy: number;
relationshipStage: string;
dolBalance: number;
totalMessages: number;
lastMessageAt?: string;
accountCreatedAt?: string;
currentMood?: string;
personalityTraits?: PersonalityTraits;
recentMemories?: RecentMemory[];
promptTokensTotal?: number;
completionTokensTotal?: number;
cacheHitTokensTotal?: number;
tokensTotal?: number;
}) {
this.userId = params.userId;
this.username = params.username;
this.platform = params.platform;
this.intimacy = params.intimacy;
this.relationshipStage = params.relationshipStage;
this.dolBalance = params.dolBalance;
this.totalMessages = params.totalMessages;
this.lastMessageAt = params.lastMessageAt ?? "";
this.accountCreatedAt = params.accountCreatedAt ?? "";
this.currentMood = params.currentMood ?? "";
this.personalityTraits =
params.personalityTraits ?? new PersonalityTraits({});
this.recentMemories = params.recentMemories ?? [];
this.promptTokensTotal = params.promptTokensTotal ?? 0;
this.completionTokensTotal = params.completionTokensTotal ?? 0;
this.cacheHitTokensTotal = params.cacheHitTokensTotal ?? 0;
this.tokensTotal = params.tokensTotal ?? 0;
Object.freeze(this);
}
toJson(): Record<string, unknown> {
return {
userId: this.userId,
username: this.username,
platform: this.platform,
intimacy: this.intimacy,
relationshipStage: this.relationshipStage,
dolBalance: this.dolBalance,
totalMessages: this.totalMessages,
lastMessageAt: this.lastMessageAt,
accountCreatedAt: this.accountCreatedAt,
currentMood: this.currentMood,
personalityTraits: this.personalityTraits.toJson(),
recentMemories: this.recentMemories.map((m) => m.toJson()),
promptTokensTotal: this.promptTokensTotal,
completionTokensTotal: this.completionTokensTotal,
cacheHitTokensTotal: this.cacheHitTokensTotal,
tokensTotal: this.tokensTotal,
};
}
static fromJson(json: Record<string, unknown>): UserStatsResponse {
const requireString = (key: string): string => {
const v = json[key];
if (typeof v !== "string") {
throw new Error(
`UserStatsResponse.${key} is required and must be a string`
);
}
return v;
};
const requireNumber = (key: string): number => {
const v = json[key];
if (typeof v !== "number") {
throw new Error(
`UserStatsResponse.${key} is required and must be a number`
);
}
return v;
};
const rawMemories = json.recentMemories;
const recentMemories: RecentMemory[] = Array.isArray(rawMemories)
? (rawMemories as Record<string, unknown>[]).map((m) =>
RecentMemory.fromJson(m)
)
: [];
return new UserStatsResponse({
userId: requireString("userId"),
username: requireString("username"),
platform: requireString("platform"),
intimacy: requireNumber("intimacy"),
relationshipStage: requireString("relationshipStage"),
dolBalance: requireNumber("dolBalance"),
totalMessages: requireNumber("totalMessages"),
lastMessageAt:
typeof json.lastMessageAt === "string"
? json.lastMessageAt
: undefined,
accountCreatedAt:
typeof json.accountCreatedAt === "string"
? json.accountCreatedAt
: undefined,
currentMood:
typeof json.currentMood === "string" ? json.currentMood : undefined,
personalityTraits: json.personalityTraits
? PersonalityTraits.fromJson(
json.personalityTraits as Record<string, unknown>
)
: undefined,
recentMemories,
promptTokensTotal:
typeof json.promptTokensTotal === "number"
? json.promptTokensTotal
: undefined,
completionTokensTotal:
typeof json.completionTokensTotal === "number"
? json.completionTokensTotal
: undefined,
cacheHitTokensTotal:
typeof json.cacheHitTokensTotal === "number"
? json.cacheHitTokensTotal
: undefined,
tokensTotal:
typeof json.tokensTotal === "number" ? json.tokensTotal : undefined,
});
}
}