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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user