refactor(data): replace schema classes with readonly models
This commit is contained in:
@@ -1,30 +1,31 @@
|
||||
import { ExceptionHandler } from "@/core/errors";
|
||||
import { ApiError, AuthApi, authApi, ErrorCode } from "@/data/services/api";
|
||||
import type { IAuthRepository } from "@/data/repositories/interfaces";
|
||||
import {
|
||||
FacebookIdentityRequest,
|
||||
FacebookPsidLoginRequest,
|
||||
FacebookLoginRequest,
|
||||
FbIdLoginRequest,
|
||||
GoogleLoginRequest,
|
||||
GuestLoginRequest,
|
||||
FacebookIdentityRequestSchema,
|
||||
FacebookLoginRequestSchema,
|
||||
FacebookPsidLoginRequestSchema,
|
||||
FbIdLoginRequestSchema,
|
||||
GoogleLoginRequestSchema,
|
||||
GuestLoginRequestSchema,
|
||||
GuestLoginResponse,
|
||||
LoginRequest,
|
||||
LoginRequestSchema,
|
||||
LoginResponse,
|
||||
LoginResponseSchema,
|
||||
LoginStatus,
|
||||
type LoginStatus as LoginStatusT,
|
||||
RefreshTokenRequest,
|
||||
RefreshTokenRequestSchema,
|
||||
RefreshTokenResponse,
|
||||
RegisterRequest,
|
||||
RegisterRequestSchema,
|
||||
type LoginStatus as LoginStatusT,
|
||||
} from "@/data/schemas/auth";
|
||||
import { User } from "@/data/schemas/user";
|
||||
import { AppEnvUtil } from "@/utils/app-env";
|
||||
import { PlatformDetector } from "@/utils/platform-detect";
|
||||
import { Result } from "@/utils/result";
|
||||
import { Logger } from "@/utils/logger";
|
||||
import { deviceIdentifier } from "@/utils/device_identifier";
|
||||
import type { IAuthRepository } from "@/data/repositories/interfaces";
|
||||
import { ApiError, AuthApi, authApi, ErrorCode } from "@/data/services/api";
|
||||
import { AuthStorage, type IAuthStorage } from "@/data/storage/auth";
|
||||
import { UserStorage, type IUserStorage } from "@/data/storage/user";
|
||||
import { AppEnvUtil } from "@/utils/app-env";
|
||||
import { deviceIdentifier } from "@/utils/device_identifier";
|
||||
import { Logger } from "@/utils/logger";
|
||||
import { PlatformDetector } from "@/utils/platform-detect";
|
||||
import { Result } from "@/utils/result";
|
||||
import { createLazySingleton } from "./lazy_singleton";
|
||||
|
||||
const log = new Logger("DataRepositoriesAuthRepository");
|
||||
@@ -52,7 +53,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
}): Promise<Result<void>> {
|
||||
return Result.wrap(async () => {
|
||||
await this.api.register(
|
||||
RegisterRequest.from(
|
||||
RegisterRequestSchema.parse(
|
||||
withTestAccountFlag({
|
||||
username: input.username,
|
||||
email: input.email,
|
||||
@@ -76,7 +77,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
return Result.wrap(async () => {
|
||||
const email = input.email?.trim() ?? "";
|
||||
const response = await this.api.emailLogin(
|
||||
LoginRequest.from(
|
||||
LoginRequestSchema.parse(
|
||||
withTestAccountFlag({
|
||||
email,
|
||||
username: input.username,
|
||||
@@ -152,7 +153,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
});
|
||||
return Result.wrap(async () => {
|
||||
const response = await this.api.guestLogin(
|
||||
GuestLoginRequest.from(withTestAccountFlag({ deviceId })),
|
||||
GuestLoginRequestSchema.parse(withTestAccountFlag({ deviceId })),
|
||||
);
|
||||
log.debug("[AuthRepository.guestLogin] API call SUCCESS (Zod parsed)", {
|
||||
hasToken: !!response.token,
|
||||
@@ -190,7 +191,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
}): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(LoginStatus.Google, () =>
|
||||
this.api.googleLogin(
|
||||
GoogleLoginRequest.from(
|
||||
GoogleLoginRequestSchema.parse(
|
||||
withTestAccountFlag({
|
||||
idToken: input.idToken,
|
||||
platform: getAuthPlatform(),
|
||||
@@ -210,7 +211,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
}): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(LoginStatus.Facebook, () =>
|
||||
this.api.facebookLogin(
|
||||
FacebookLoginRequest.from(
|
||||
FacebookLoginRequestSchema.parse(
|
||||
withTestAccountFlag({
|
||||
accessToken: input.accessToken,
|
||||
platform: getAuthPlatform(),
|
||||
@@ -230,7 +231,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
}): Promise<Result<LoginResponse>> {
|
||||
return this._socialLogin(LoginStatus.Facebook, () =>
|
||||
this.api.facebookIdLogin(
|
||||
FbIdLoginRequest.from(
|
||||
FbIdLoginRequestSchema.parse(
|
||||
withTestAccountFlag({
|
||||
fbId: input.asid,
|
||||
avatarUrl: input.avatarUrl ?? "",
|
||||
@@ -250,12 +251,12 @@ export class AuthRepository implements IAuthRepository {
|
||||
|
||||
return Result.wrap(async () => {
|
||||
const response = await this.api.bindFacebookIdentity(
|
||||
FacebookIdentityRequest.from({
|
||||
FacebookIdentityRequestSchema.parse({
|
||||
asid: input.asid ?? "",
|
||||
psid: input.psid ?? "",
|
||||
}),
|
||||
);
|
||||
const data = response.toJson();
|
||||
const data = response;
|
||||
await this._saveFacebookIdentity({
|
||||
asid: data.fbAsid,
|
||||
psid: data.fbPsid,
|
||||
@@ -271,13 +272,13 @@ export class AuthRepository implements IAuthRepository {
|
||||
}): Promise<Result<LoginStatusT>> {
|
||||
return Result.wrap(async () => {
|
||||
const response = await this.api.facebookPsidLogin(
|
||||
FacebookPsidLoginRequest.from({
|
||||
FacebookPsidLoginRequestSchema.parse({
|
||||
psid: input.psid,
|
||||
deviceId: input.deviceId ?? "",
|
||||
bindToGuest: input.bindToGuest ?? true,
|
||||
}),
|
||||
);
|
||||
const data = response.toJson();
|
||||
const data = response;
|
||||
await this._saveFacebookIdentity({
|
||||
asid: data.fbAsid,
|
||||
psid: data.fbPsid,
|
||||
@@ -303,7 +304,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
}
|
||||
|
||||
await this._saveLoginData(
|
||||
LoginResponse.from({
|
||||
LoginResponseSchema.parse({
|
||||
token: data.token,
|
||||
refreshToken: data.refreshToken,
|
||||
user: data.user ?? { id: data.userId },
|
||||
@@ -330,7 +331,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
);
|
||||
}
|
||||
const response = await this.api.refreshToken(
|
||||
RefreshTokenRequest.from({ refreshToken: existing.data }),
|
||||
RefreshTokenRequestSchema.parse({ refreshToken: existing.data }),
|
||||
);
|
||||
const tokenResult = await this.storage.setLoginToken(response.token);
|
||||
if (Result.isErr(tokenResult)) throw tokenResult.error;
|
||||
@@ -404,7 +405,7 @@ export class AuthRepository implements IAuthRepository {
|
||||
|
||||
private async _cacheUser(user: User): Promise<void> {
|
||||
try {
|
||||
const userResult = await this.userStorage.setUser(user.toJson());
|
||||
const userResult = await this.userStorage.setUser(user);
|
||||
if (Result.isErr(userResult)) {
|
||||
log.warn("[AuthRepository] setUser failed", userResult.error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user