refactor(data): move Result utility to utils and simplify API
Relocate the Result type from `@/data/result` to `@/utils/result` and replace the discriminated-union API (`kind`/`value`) with a simpler boolean-based API (`success`/`data`) across all repositories and storage classes for improved readability and consistency.
This commit is contained in:
@@ -26,7 +26,7 @@ import { RefreshTokenResponse } from "@/data/dto/auth/refresh_token_response";
|
||||
import { RegisterRequest } from "@/data/dto/auth/register_request";
|
||||
import { SendCodeRequest } from "@/data/dto/auth/send_code_request";
|
||||
import { User } from "@/data/dto/user/user";
|
||||
import { Result } from "@/data/result";
|
||||
import { Result } from "@/utils/result";
|
||||
import { AuthStorage } from "@/data/storage/auth/auth_storage";
|
||||
import type { IAuthStorage } from "@/data/storage/auth/iauth_storage";
|
||||
import { UserStorage } from "@/data/storage/user/user_storage";
|
||||
@@ -204,10 +204,10 @@ export class AuthRepository {
|
||||
*/
|
||||
async refreshToken(): Promise<Result<RefreshTokenResponse>> {
|
||||
const existing = await this.storage.getRefreshToken();
|
||||
if (existing.kind !== "success" || !existing.value) {
|
||||
if (!existing.success || !existing.data) {
|
||||
return Result.err(new Error("No refresh token available"));
|
||||
}
|
||||
const refreshToken = existing.value;
|
||||
const refreshToken = existing.data;
|
||||
return Result.wrap(async () => {
|
||||
const response = await this.api.refreshToken(
|
||||
RefreshTokenRequest.from({ refreshToken }),
|
||||
@@ -228,7 +228,7 @@ export class AuthRepository {
|
||||
return Result.wrap(async () => {
|
||||
const user = await this.api.getCurrentUser();
|
||||
const writeResult = await this.userStorage.setUser(user.toJson());
|
||||
if (writeResult.kind === "failure") {
|
||||
if (!writeResult.success) {
|
||||
console.warn(
|
||||
"[AuthRepository] failed to cache current user",
|
||||
writeResult.error,
|
||||
@@ -261,21 +261,21 @@ export class AuthRepository {
|
||||
*/
|
||||
private async _saveLoginData(data: LoginResponse): Promise<void> {
|
||||
const r1 = await this.storage.setLoginToken(data.token);
|
||||
if (r1.kind === "failure") {
|
||||
if (!r1.success) {
|
||||
console.warn("[AuthRepository] setLoginToken failed", r1.error);
|
||||
}
|
||||
if (data.refreshToken) {
|
||||
const r2 = await this.storage.setRefreshToken(data.refreshToken);
|
||||
if (r2.kind === "failure") {
|
||||
if (!r2.success) {
|
||||
console.warn("[AuthRepository] setRefreshToken failed", r2.error);
|
||||
}
|
||||
}
|
||||
const r3 = await this.userStorage.setUser(data.user.toJson());
|
||||
if (r3.kind === "failure") {
|
||||
if (!r3.success) {
|
||||
console.warn("[AuthRepository] setUser failed", r3.error);
|
||||
}
|
||||
const r4 = await this.userStorage.setUserId(data.user.id);
|
||||
if (r4.kind === "failure") {
|
||||
if (!r4.success) {
|
||||
console.warn("[AuthRepository] setUserId failed", r4.error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import { ChatHistoryResponse } from "@/data/dto/chat/chat_history_response";
|
||||
import { ChatMessage } from "@/data/dto/chat/chat_message";
|
||||
import { ChatSendResponse } from "@/data/dto/chat/chat_send_response";
|
||||
import { SendMessageRequest } from "@/data/dto/chat/send_message_request";
|
||||
import { Result } from "@/data/result";
|
||||
import { Result } from "@/utils/result";
|
||||
import { LocalChatStorage } from "@/data/storage/chat/local_chat_storage";
|
||||
import { LocalMessage } from "@/data/storage/chat/local_message";
|
||||
|
||||
@@ -72,7 +72,7 @@ export class ChatRepository {
|
||||
messages: readonly ChatMessage[],
|
||||
): Promise<Result<void>> {
|
||||
const cleared = await this.localStorage.clearAll();
|
||||
if (cleared.kind === "failure") {
|
||||
if (!cleared.success) {
|
||||
return Result.err(cleared.error);
|
||||
}
|
||||
return this._mapLocalStorageResult(
|
||||
@@ -83,10 +83,10 @@ export class ChatRepository {
|
||||
/** 读取所有本地消息,按 dbId 升序。 */
|
||||
async getLocalMessages(): Promise<Result<ChatMessage[]>> {
|
||||
const result = await this.localStorage.getAllMessages();
|
||||
if (result.kind === "failure") {
|
||||
if (!result.success) {
|
||||
return Result.err(result.error);
|
||||
}
|
||||
return Result.ok(result.value.map((lm) => this._localToChat(lm)));
|
||||
return Result.ok(result.data.map((lm) => this._localToChat(lm)));
|
||||
}
|
||||
|
||||
/** 清空本地消息。 */
|
||||
@@ -126,20 +126,14 @@ export class ChatRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 storage 层 `{kind, value/error}` 形态的 `Result<T>` 桥接到仓库层
|
||||
* `{success, data/error}` 形态。错误统一用 `Error` 包装。
|
||||
* 把 storage 层 `Result<T>` 透传:仓库与 storage 现在共享同一全局
|
||||
* `Result<T>`(来自 `@/utils/result`),不需要任何形状转换。
|
||||
* 该方法保留仅为未来万一又出现差异化时方便快速插入适配。
|
||||
*/
|
||||
private async _mapLocalStorageResult<T>(
|
||||
promise: Promise<
|
||||
| { readonly kind: "success"; readonly value: T }
|
||||
| { readonly kind: "failure"; readonly error: unknown }
|
||||
>,
|
||||
promise: Promise<Result<T>>,
|
||||
): Promise<Result<T>> {
|
||||
const r = await promise;
|
||||
if (r.kind === "success") {
|
||||
return Result.ok(r.value);
|
||||
}
|
||||
return Result.err(r.error);
|
||||
return await promise;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
import { MetricsApi, metricsApi } from "@/data/api";
|
||||
import { AppEvent } from "@/data/dto/metrics/app_event";
|
||||
import { PwaEvent } from "@/data/dto/metrics/pwa_event";
|
||||
import { Result } from "@/data/result";
|
||||
import { Result } from "@/utils/result";
|
||||
|
||||
export class MetricsRepository {
|
||||
constructor(private readonly api: MetricsApi) {}
|
||||
|
||||
@@ -14,7 +14,7 @@ import { CreditsHistoryData } from "@/data/dto/user/credits_history_data";
|
||||
import { UpdateProfileRequest } from "@/data/dto/user/update_profile_request";
|
||||
import { User } from "@/data/dto/user/user";
|
||||
import { UserStatsResponse } from "@/data/dto/user/user_stats_response";
|
||||
import { Result } from "@/data/result";
|
||||
import { Result } from "@/utils/result";
|
||||
|
||||
export class UserRepository {
|
||||
constructor(private readonly api: UserApi) {}
|
||||
|
||||
Reference in New Issue
Block a user