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