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:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import { LocalStorage } from "../local_storage";
|
||||
import { Result, type Result as ResultT } from "../result";
|
||||
import { Result, type Result as ResultT } from "@/utils/result";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import type { IAuthStorage } from "./iauth_storage";
|
||||
|
||||
@@ -47,8 +47,8 @@ export class AuthStorage implements IAuthStorage {
|
||||
}
|
||||
async hasLoginToken(): Promise<ResultT<boolean>> {
|
||||
const r = await this.getLoginToken();
|
||||
if (r.kind === "failure") return Result.failure(r.error);
|
||||
return Result.success(r.value !== null && r.value.length > 0);
|
||||
if (!r.success) return Result.err(r.error);
|
||||
return Result.ok(r.data !== null && r.data.length > 0);
|
||||
}
|
||||
clearLoginToken(): Promise<ResultT<void>> {
|
||||
return this.ls.remove(StorageKeys.loginToken);
|
||||
@@ -64,8 +64,8 @@ export class AuthStorage implements IAuthStorage {
|
||||
}
|
||||
async hasGuestToken(): Promise<ResultT<boolean>> {
|
||||
const r = await this.getGuestToken();
|
||||
if (r.kind === "failure") return Result.failure(r.error);
|
||||
return Result.success(r.value !== null && r.value.length > 0);
|
||||
if (!r.success) return Result.err(r.error);
|
||||
return Result.ok(r.data !== null && r.data.length > 0);
|
||||
}
|
||||
clearGuestToken(): Promise<ResultT<void>> {
|
||||
return this.ls.remove(StorageKeys.guestToken);
|
||||
@@ -107,9 +107,9 @@ export class AuthStorage implements IAuthStorage {
|
||||
|
||||
async clearAuthData(): Promise<ResultT<void>> {
|
||||
const r1 = await this.clearLoginToken();
|
||||
if (r1.kind === "failure") return r1;
|
||||
if (!r1.success) return r1;
|
||||
const r2 = await this.clearGuestToken();
|
||||
if (r2.kind === "failure") return r2;
|
||||
if (!r2.success) return r2;
|
||||
return this.clearRefreshToken();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
* - 所有方法返回 `Promise<Result<T>>`,与 Dart `Future<Result<T>>` 对齐
|
||||
*/
|
||||
|
||||
import type { Result } from "../result";
|
||||
import type { Result } from "@/utils/result";
|
||||
|
||||
export interface IAuthStorage {
|
||||
getLoginToken(): Promise<Result<string | null>>;
|
||||
|
||||
Reference in New Issue
Block a user