From 0d0dabaacefdf0a2874e1f994ba3c835a6ab1633 Mon Sep 17 00:00:00 2001 From: chenhang Date: Thu, 18 Jun 2026 11:58:50 +0800 Subject: [PATCH] refactor(utils): add barrel exports --- barrelsby.json | 4 +--- src/app/chat/chat-screen.tsx | 4 +--- src/app/chat/components/browser-hint-overlay.tsx | 2 +- src/app/chat/components/pwa-install-overlay.tsx | 5 +---- src/app/splash/splash-screen.tsx | 2 +- src/core/net/config/api_config.ts | 2 +- .../net/interceptor/auth_refresh_interceptor.ts | 3 +-- src/data/dto/auth/index.ts | 2 +- src/data/dto/chat/guest_chat_quota.ts | 2 +- src/data/repositories/auth_repository.ts | 2 +- src/data/repositories/chat_repository.ts | 2 +- .../repositories/interfaces/iauth_repository.ts | 2 +- .../repositories/interfaces/ichat_repository.ts | 2 +- .../interfaces/imetrics_repository.ts | 2 +- .../repositories/interfaces/iuser_repository.ts | 2 +- src/data/repositories/metrics_repository.ts | 2 +- src/data/repositories/user_repository.ts | 2 +- src/data/storage/app/app_storage.ts | 3 +-- src/data/storage/auth/auth_storage.ts | 3 +-- src/data/storage/auth/iauth_storage.ts | 2 +- .../storage/chat/__tests__/chat_storage.test.ts | 3 +-- src/data/storage/chat/chat_storage.ts | 4 +--- src/data/storage/chat/ichat_storage.ts | 2 +- src/data/storage/chat/local_chat_storage.ts | 2 +- src/data/storage/user/iuser_storage.ts | 2 +- src/data/storage/user/user_storage.ts | 3 +-- src/integrations/media-recorder.ts | 2 +- src/stores/auth/auth-actors.ts | 5 +---- src/stores/chat/chat-machine.actors.ts | 2 +- src/stores/chat/chat-machine.helpers.ts | 3 +-- src/stores/chat/chat-machine.ts | 2 +- src/stores/user/user-machine.actors.ts | 2 +- src/stores/user/user-machine.helpers.ts | 2 +- src/utils/index.ts | 15 +++++++++++++++ 34 files changed, 48 insertions(+), 51 deletions(-) create mode 100644 src/utils/index.ts diff --git a/barrelsby.json b/barrelsby.json index 488172d2..1ae7e1e5 100644 --- a/barrelsby.json +++ b/barrelsby.json @@ -30,9 +30,7 @@ "./src/app/subscription/components", "./src/hooks", "./src/integrations", - "./src/models/auth", - "./src/models/chat", - "./src/models/user", + "./src/utils", "./src/stores/auth", "./src/stores/chat", "./src/stores/sidebar", diff --git a/src/app/chat/chat-screen.tsx b/src/app/chat/chat-screen.tsx index cec8fb9d..bb92d3a7 100644 --- a/src/app/chat/chat-screen.tsx +++ b/src/app/chat/chat-screen.tsx @@ -26,9 +26,7 @@ import type { LoginStatus } from "@/data/dto/auth"; import { AuthStorage } from "@/data/storage/auth/auth_storage"; import { UserStorage } from "@/data/storage/user/user_storage"; import { useChatDispatch, useChatState } from "@/stores/chat/chat-context"; -import { AppEnvUtil } from "@/utils/app-env"; -import { BrowserDetector } from "@/utils/browser-detect"; -import { UrlLauncherUtil } from "@/utils/url-launcher-util"; +import { AppEnvUtil, BrowserDetector, UrlLauncherUtil } from "@/utils"; import { ROUTE_BUILDERS, ROUTES } from "@/router/routes"; import { MobileShell } from "@/app/_components/core"; diff --git a/src/app/chat/components/browser-hint-overlay.tsx b/src/app/chat/components/browser-hint-overlay.tsx index 1cb82743..066b41f8 100644 --- a/src/app/chat/components/browser-hint-overlay.tsx +++ b/src/app/chat/components/browser-hint-overlay.tsx @@ -14,7 +14,7 @@ */ import { useState } from "react"; -import { BrowserDetector } from "@/utils/browser-detect"; +import { BrowserDetector } from "@/utils"; import styles from "./browser-hint-overlay.module.css"; diff --git a/src/app/chat/components/pwa-install-overlay.tsx b/src/app/chat/components/pwa-install-overlay.tsx index fdeac0ad..477a7a0d 100644 --- a/src/app/chat/components/pwa-install-overlay.tsx +++ b/src/app/chat/components/pwa-install-overlay.tsx @@ -14,10 +14,7 @@ */ import { useEffect } from "react"; -import { BrowserDetector } from "@/utils/browser-detect"; -import { AppEnvUtil } from "@/utils/app-env"; -import { SpAsyncUtil } from "@/utils/storage"; -import { pwaUtil } from "@/utils/pwa"; +import { BrowserDetector, AppEnvUtil, SpAsyncUtil, pwaUtil } from "@/utils"; import { PwaInstallDialog } from "./pwa-install-dialog"; import styles from "./pwa-install-overlay.module.css"; diff --git a/src/app/splash/splash-screen.tsx b/src/app/splash/splash-screen.tsx index ac19cd2b..e4883c48 100644 --- a/src/app/splash/splash-screen.tsx +++ b/src/app/splash/splash-screen.tsx @@ -8,7 +8,7 @@ import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context"; import { useUserDispatch } from "@/stores/user/user-context"; import { AuthStorage } from "@/data/storage/auth/auth_storage"; import { ROUTES } from "@/router/routes"; -import { pwaUtil } from "@/utils/pwa"; +import { pwaUtil } from "@/utils"; import { SplashBackground, diff --git a/src/core/net/config/api_config.ts b/src/core/net/config/api_config.ts index c3ec69a7..85e0f7d4 100644 --- a/src/core/net/config/api_config.ts +++ b/src/core/net/config/api_config.ts @@ -4,7 +4,7 @@ * 通过环境变量配置后端服务地址与超时时间。 * 原始 Dart: lib/core/net/service_manager.dart */ -import { AppEnvUtil, type AppEnv } from "@/utils/app-env"; +import { AppEnvUtil, type AppEnv } from "@/utils"; export type { AppEnv }; diff --git a/src/core/net/interceptor/auth_refresh_interceptor.ts b/src/core/net/interceptor/auth_refresh_interceptor.ts index c0895aec..72a7a025 100644 --- a/src/core/net/interceptor/auth_refresh_interceptor.ts +++ b/src/core/net/interceptor/auth_refresh_interceptor.ts @@ -8,8 +8,7 @@ import type { FetchHook } from "ofetch"; import { ApiPath } from "../../../data/services/api/api_path"; import { ApiError, ErrorCode } from "../../../data/services/api/api_result"; import { AuthStorage } from "@/data/storage/auth/auth_storage"; -import { deviceIdentifier } from "@/utils/device_identifier"; -import { Result } from "@/utils/result"; +import { deviceIdentifier, Result } from "@/utils"; /** * 不需要 auth token 刷新的路径 diff --git a/src/data/dto/auth/index.ts b/src/data/dto/auth/index.ts index 3f018ccf..09a9e406 100644 --- a/src/data/dto/auth/index.ts +++ b/src/data/dto/auth/index.ts @@ -11,9 +11,9 @@ export * from "./fb_id_login_request"; export * from "./google_login_request"; export * from "./guest_login_request"; export * from "./guest_login_response"; -export * from "./login_status"; export * from "./login_request"; export * from "./login_response"; +export * from "./login_status"; export * from "./logout_response"; export * from "./refresh_token_request"; export * from "./refresh_token_response"; diff --git a/src/data/dto/chat/guest_chat_quota.ts b/src/data/dto/chat/guest_chat_quota.ts index 19e63efd..5d10639b 100644 --- a/src/data/dto/chat/guest_chat_quota.ts +++ b/src/data/dto/chat/guest_chat_quota.ts @@ -6,7 +6,7 @@ import { type GuestChatQuotaInput, type GuestChatQuotaData, } from "@/data/schemas/chat/guest_chat_quota"; -import { AppEnvUtil } from "@/utils/app-env"; +import { AppEnvUtil } from "@/utils"; export class GuestChatQuota { // 静态常量 diff --git a/src/data/repositories/auth_repository.ts b/src/data/repositories/auth_repository.ts index 22656eb9..8fbe7524 100644 --- a/src/data/repositories/auth_repository.ts +++ b/src/data/repositories/auth_repository.ts @@ -28,7 +28,7 @@ import { SendCodeRequest, } from "@/data/dto/auth"; import { User } from "@/data/dto/user"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; import type { IAuthRepository } from "@/data/repositories/interfaces"; import { AuthStorage, type IAuthStorage } from "@/data/storage/auth"; import { UserStorage, type IUserStorage } from "@/data/storage/user"; diff --git a/src/data/repositories/chat_repository.ts b/src/data/repositories/chat_repository.ts index a3540cfa..7d5224b4 100644 --- a/src/data/repositories/chat_repository.ts +++ b/src/data/repositories/chat_repository.ts @@ -22,7 +22,7 @@ import { ChatSendResponse, SendMessageRequest, } from "@/data/dto/chat"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; import type { IChatRepository } from "@/data/repositories/interfaces"; import { LocalChatStorage, LocalMessage } from "@/data/storage/chat"; diff --git a/src/data/repositories/interfaces/iauth_repository.ts b/src/data/repositories/interfaces/iauth_repository.ts index a3038cfb..5764b46a 100644 --- a/src/data/repositories/interfaces/iauth_repository.ts +++ b/src/data/repositories/interfaces/iauth_repository.ts @@ -13,7 +13,7 @@ * 原始 Dart: lib/data/repositories/auth_repository_impl.dart */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; import type { GuestLoginResponse, LoginResponse, diff --git a/src/data/repositories/interfaces/ichat_repository.ts b/src/data/repositories/interfaces/ichat_repository.ts index 5ba54115..2074ea88 100644 --- a/src/data/repositories/interfaces/ichat_repository.ts +++ b/src/data/repositories/interfaces/ichat_repository.ts @@ -11,7 +11,7 @@ * 原始 Dart: lib/data/repositories/chat_repository_impl.dart */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; import type { ChatHistoryResponse, ChatMessage, diff --git a/src/data/repositories/interfaces/imetrics_repository.ts b/src/data/repositories/interfaces/imetrics_repository.ts index 070289c8..45e95f68 100644 --- a/src/data/repositories/interfaces/imetrics_repository.ts +++ b/src/data/repositories/interfaces/imetrics_repository.ts @@ -9,7 +9,7 @@ * 原始 Dart: lib/data/repositories/metrics_repository_impl.dart */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; export interface IMetricsRepository { /** 上报 PWA 事件。自动注入当前秒级时间戳。 */ diff --git a/src/data/repositories/interfaces/iuser_repository.ts b/src/data/repositories/interfaces/iuser_repository.ts index 185bd659..ba2d96b6 100644 --- a/src/data/repositories/interfaces/iuser_repository.ts +++ b/src/data/repositories/interfaces/iuser_repository.ts @@ -9,7 +9,7 @@ * 原始 Dart: lib/data/repositories/user_repository_impl.dart */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; import type { CreditsData, CreditsHistoryData, diff --git a/src/data/repositories/metrics_repository.ts b/src/data/repositories/metrics_repository.ts index ab9c0703..d0b4bbf4 100644 --- a/src/data/repositories/metrics_repository.ts +++ b/src/data/repositories/metrics_repository.ts @@ -9,7 +9,7 @@ */ import { MetricsApi, metricsApi } from "@/data/services/api"; import { AppEvent, PwaEvent } from "@/data/dto/metrics"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; import type { IMetricsRepository } from "@/data/repositories/interfaces"; export class MetricsRepository implements IMetricsRepository { diff --git a/src/data/repositories/user_repository.ts b/src/data/repositories/user_repository.ts index 3e17bda1..ed039e6f 100644 --- a/src/data/repositories/user_repository.ts +++ b/src/data/repositories/user_repository.ts @@ -16,7 +16,7 @@ import { User, UserStatsResponse, } from "@/data/dto/user"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; import type { IUserRepository } from "@/data/repositories/interfaces"; export class UserRepository implements IUserRepository { diff --git a/src/data/storage/app/app_storage.ts b/src/data/storage/app/app_storage.ts index f823eae8..5adf64e4 100644 --- a/src/data/storage/app/app_storage.ts +++ b/src/data/storage/app/app_storage.ts @@ -16,8 +16,7 @@ * - `recordXxx(today)` 写入 todayString,后续 `canXxx` 在同一天返回 `false`。 */ -import { Result, type Result as ResultT } from "@/utils/result"; -import { SpAsyncUtil } from "@/utils/storage"; +import { Result, type Result as ResultT, SpAsyncUtil } from "@/utils"; import { StorageKeys } from "../storage_keys"; export class AppStorage { diff --git a/src/data/storage/auth/auth_storage.ts b/src/data/storage/auth/auth_storage.ts index 872c2b63..60f2307c 100644 --- a/src/data/storage/auth/auth_storage.ts +++ b/src/data/storage/auth/auth_storage.ts @@ -14,8 +14,7 @@ * - `clearAuthData` 顺序清理:loginToken → guestToken → refreshToken,任一失败立即返回 */ -import { Result, type Result as ResultT } from "@/utils/result"; -import { SpAsyncUtil } from "@/utils/storage"; +import { Result, type Result as ResultT, SpAsyncUtil } from "@/utils"; import { StorageKeys } from "../storage_keys"; import type { IAuthStorage } from "./iauth_storage"; diff --git a/src/data/storage/auth/iauth_storage.ts b/src/data/storage/auth/iauth_storage.ts index 871e3630..11cae11d 100644 --- a/src/data/storage/auth/iauth_storage.ts +++ b/src/data/storage/auth/iauth_storage.ts @@ -9,7 +9,7 @@ * - 所有方法返回 `Promise>`,与 Dart `Future>` 对齐 */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; export interface IAuthStorage { getLoginToken(): Promise>; diff --git a/src/data/storage/chat/__tests__/chat_storage.test.ts b/src/data/storage/chat/__tests__/chat_storage.test.ts index fa642548..b3207491 100644 --- a/src/data/storage/chat/__tests__/chat_storage.test.ts +++ b/src/data/storage/chat/__tests__/chat_storage.test.ts @@ -4,8 +4,7 @@ import memoryDriver from "unstorage/drivers/memory"; import { GuestChatQuota } from "@/data/dto/chat/guest_chat_quota"; import { StorageKeys } from "@/data/storage/storage_keys"; -import { todayString } from "@/utils/date"; -import { SpAsyncUtil } from "@/utils/storage"; +import { todayString, SpAsyncUtil } from "@/utils"; import { ChatStorage } from "../chat_storage"; diff --git a/src/data/storage/chat/chat_storage.ts b/src/data/storage/chat/chat_storage.ts index f455c3a9..2e0f7aa5 100644 --- a/src/data/storage/chat/chat_storage.ts +++ b/src/data/storage/chat/chat_storage.ts @@ -7,9 +7,7 @@ import { import { GuestChatQuota as GuestChatQuotaDto } from "@/data/dto/chat/guest_chat_quota"; import { z } from "zod"; -import { type Result as ResultT, Result } from "@/utils/result"; -import { todayString } from "@/utils/date"; -import { SpAsyncUtil } from "@/utils/storage"; +import { type Result as ResultT, Result, todayString, SpAsyncUtil } from "@/utils"; import { StorageKeys } from "../storage_keys"; import type { IChatStorage } from "./ichat_storage"; diff --git a/src/data/storage/chat/ichat_storage.ts b/src/data/storage/chat/ichat_storage.ts index 62a64382..4d743595 100644 --- a/src/data/storage/chat/ichat_storage.ts +++ b/src/data/storage/chat/ichat_storage.ts @@ -11,7 +11,7 @@ * 跨天判断逻辑(`needsReset`)保留在 `GuestChatQuota` 类上。 */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; import type { GuestChatQuotaData } from "@/data/schemas/chat/guest_chat_quota"; export interface IChatStorage { diff --git a/src/data/storage/chat/local_chat_storage.ts b/src/data/storage/chat/local_chat_storage.ts index a782c56b..306001d2 100644 --- a/src/data/storage/chat/local_chat_storage.ts +++ b/src/data/storage/chat/local_chat_storage.ts @@ -15,7 +15,7 @@ * 数据量大时考虑升级 schema 加索引。 */ -import { Result, type Result as ResultT } from "@/utils/result"; +import { Result, type Result as ResultT } from "@/utils"; import { LocalChatDB } from "./local_chat_db"; import { LocalMessage } from "./local_message"; diff --git a/src/data/storage/user/iuser_storage.ts b/src/data/storage/user/iuser_storage.ts index c0f7b9ae..8b72f508 100644 --- a/src/data/storage/user/iuser_storage.ts +++ b/src/data/storage/user/iuser_storage.ts @@ -11,7 +11,7 @@ * 具体序列化由实现层通过 Zod schema 校验后转换为 `User` 类实例。 */ -import type { Result } from "@/utils/result"; +import type { Result } from "@/utils"; import type { UserData } from "@/data/schemas/user/user"; export interface IUserStorage { diff --git a/src/data/storage/user/user_storage.ts b/src/data/storage/user/user_storage.ts index eb9280ac..fb079eda 100644 --- a/src/data/storage/user/user_storage.ts +++ b/src/data/storage/user/user_storage.ts @@ -16,8 +16,7 @@ */ import { UserSchema, type UserData } from "@/data/schemas/user/user"; -import { type Result as ResultT } from "@/utils/result"; -import { SpAsyncUtil } from "@/utils/storage"; +import { type Result as ResultT, SpAsyncUtil } from "@/utils"; import { StorageKeys } from "../storage_keys"; import type { IUserStorage } from "./iuser_storage"; diff --git a/src/integrations/media-recorder.ts b/src/integrations/media-recorder.ts index 318040fa..4896636f 100644 --- a/src/integrations/media-recorder.ts +++ b/src/integrations/media-recorder.ts @@ -9,7 +9,7 @@ * * 注:Web Speech API 在 Chrome / Edge 完整支持;Safari / Firefox 仅录音。 */ -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; export interface SpeechRecognitionResultEvent { transcript: string; diff --git a/src/stores/auth/auth-actors.ts b/src/stores/auth/auth-actors.ts index f144abc6..fbe3489d 100644 --- a/src/stores/auth/auth-actors.ts +++ b/src/stores/auth/auth-actors.ts @@ -9,10 +9,7 @@ import { authRepository } from "@/data/repositories/auth_repository"; import type { IAuthRepository } from "@/data/repositories/interfaces"; import { AuthStorage } from "@/data/storage/auth/auth_storage"; import { UserStorage } from "@/data/storage/user/user_storage"; -import { deviceIdentifier } from "@/utils/device_identifier"; -import { fetchFacebookUserData } from "@/utils/facebook-graph"; -import { Logger } from "@/utils/logger"; -import { Result } from "@/utils/result"; +import { deviceIdentifier, fetchFacebookUserData, Logger, Result } from "@/utils"; import { readGuestId } from "./auth-helpers"; diff --git a/src/stores/chat/chat-machine.actors.ts b/src/stores/chat/chat-machine.actors.ts index cef62d26..7807edeb 100644 --- a/src/stores/chat/chat-machine.actors.ts +++ b/src/stores/chat/chat-machine.actors.ts @@ -5,7 +5,7 @@ import { fromPromise, fromCallback } from "xstate"; import { createChatWebSocket, ChatWebSocket } from "@/core/net/chat-websocket"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; import { PAGE_SIZE, diff --git a/src/stores/chat/chat-machine.helpers.ts b/src/stores/chat/chat-machine.helpers.ts index 62cb08ba..49429090 100644 --- a/src/stores/chat/chat-machine.helpers.ts +++ b/src/stores/chat/chat-machine.helpers.ts @@ -25,8 +25,7 @@ import { chatRepository } from "@/data/repositories/chat_repository"; import type { IChatRepository } from "@/data/repositories/interfaces"; import { ChatStorage } from "@/data/storage/chat/chat_storage"; import { ChatSendResponse } from "@/data/dto/chat/chat_send_response"; -import { formatDate } from "@/utils/date"; -import { Result } from "@/utils/result"; +import { formatDate, Result } from "@/utils"; // ============================================================ // Constants diff --git a/src/stores/chat/chat-machine.ts b/src/stores/chat/chat-machine.ts index 8e6b6816..15788325 100644 --- a/src/stores/chat/chat-machine.ts +++ b/src/stores/chat/chat-machine.ts @@ -33,7 +33,7 @@ import { setup, assign } from "xstate"; import { ChatStorage } from "@/data/storage/chat/chat_storage"; -import { formatDate, todayString } from "@/utils/date"; +import { formatDate, todayString } from "@/utils"; import { ChatState, initialState } from "./chat-state"; diff --git a/src/stores/user/user-machine.actors.ts b/src/stores/user/user-machine.actors.ts index 00c6d56d..15c9d714 100644 --- a/src/stores/user/user-machine.actors.ts +++ b/src/stores/user/user-machine.actors.ts @@ -21,7 +21,7 @@ import type { IAuthRepository, IUserRepository, } from "@/data/repositories/interfaces"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; import { type InitData, readInitData, toView, userStorage } from "./user-machine.helpers"; diff --git a/src/stores/user/user-machine.helpers.ts b/src/stores/user/user-machine.helpers.ts index eaa9f901..4fe9b073 100644 --- a/src/stores/user/user-machine.helpers.ts +++ b/src/stores/user/user-machine.helpers.ts @@ -15,7 +15,7 @@ import type { UserView } from "@/data/dto/user"; import { UserStorage } from "@/data/storage/user/user_storage"; -import { Result } from "@/utils/result"; +import { Result } from "@/utils"; // ============================================================ // Storage singleton diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 00000000..42003f51 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,15 @@ +/** + * @file Automatically generated by barrelsby. + */ + +export * from "./app-env"; +export * from "./browser-detect"; +export * from "./date"; +export * from "./device_identifier"; +export * from "./facebook-graph"; +export * from "./logger"; +export * from "./platform-detect"; +export * from "./pwa"; +export * from "./result"; +export * from "./storage"; +export * from "./url-launcher-util";