refactor(data): split models into separate dto and schema modules
Reorganize the data layer by separating DTOs (Data Transfer Objects) from Zod validation schemas into distinct directory structures. Previously, all model types were imported from a single `@/data/models` barrel file, mixing request/response classes with validation schemas. Changes: - Update API files (auth, chat, metrics, user) to import DTOs from specific paths under `@/data/dto/*` instead of the models barrel - Move user schema to `@/data/schemas/user/user` for type-only imports - Create dedicated Zod schema files under `@/data/schemas/*` for auth and chat domain models (e.g., AppleLoginRequestSchema, SendMessageRequestSchema, SyncMessageSchema, SttDataSchema) - Improve discoverability and tree-shaking by co-locating schemas with their respective domain areas This is a non-functional refactor that lays the groundwork for clearer separation between transport-layer types and runtime validation.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Apple 登录请求 DTO
|
||||
*/
|
||||
import {
|
||||
AppleLoginRequestSchema,
|
||||
type AppleLoginRequestInput,
|
||||
type AppleLoginRequestData,
|
||||
} from "@/data/schemas/auth/apple_login_request";
|
||||
|
||||
export class AppleLoginRequest {
|
||||
declare readonly identityToken: string;
|
||||
declare readonly platform: string;
|
||||
|
||||
private constructor(input: AppleLoginRequestInput) {
|
||||
const data = AppleLoginRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: AppleLoginRequestInput): AppleLoginRequest {
|
||||
return new AppleLoginRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): AppleLoginRequest {
|
||||
return AppleLoginRequest.from(json as AppleLoginRequestInput);
|
||||
}
|
||||
|
||||
toJson(): AppleLoginRequestData {
|
||||
return AppleLoginRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Facebook 登录请求 DTO
|
||||
*/
|
||||
import {
|
||||
FacebookLoginRequestSchema,
|
||||
type FacebookLoginRequestInput,
|
||||
type FacebookLoginRequestData,
|
||||
} from "@/data/schemas/auth/facebook_login_request";
|
||||
|
||||
export class FacebookLoginRequest {
|
||||
declare readonly accessToken: string;
|
||||
declare readonly platform: string;
|
||||
declare readonly guestId: string;
|
||||
|
||||
private constructor(input: FacebookLoginRequestInput) {
|
||||
const data = FacebookLoginRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: FacebookLoginRequestInput): FacebookLoginRequest {
|
||||
return new FacebookLoginRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): FacebookLoginRequest {
|
||||
return FacebookLoginRequest.from(json as FacebookLoginRequestInput);
|
||||
}
|
||||
|
||||
toJson(): FacebookLoginRequestData {
|
||||
return FacebookLoginRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Facebook ID 登录请求 DTO
|
||||
*/
|
||||
import {
|
||||
FbIdLoginRequestSchema,
|
||||
type FbIdLoginRequestInput,
|
||||
type FbIdLoginRequestData,
|
||||
} from "@/data/schemas/auth/fb_id_login_request";
|
||||
|
||||
export class FbIdLoginRequest {
|
||||
declare readonly fbId: string;
|
||||
declare readonly avatarUrl: string;
|
||||
|
||||
private constructor(input: FbIdLoginRequestInput) {
|
||||
const data = FbIdLoginRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: FbIdLoginRequestInput): FbIdLoginRequest {
|
||||
return new FbIdLoginRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): FbIdLoginRequest {
|
||||
return FbIdLoginRequest.from(json as FbIdLoginRequestInput);
|
||||
}
|
||||
|
||||
toJson(): FbIdLoginRequestData {
|
||||
return FbIdLoginRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Google 登录请求 DTO
|
||||
*/
|
||||
import {
|
||||
GoogleLoginRequestSchema,
|
||||
type GoogleLoginRequestInput,
|
||||
type GoogleLoginRequestData,
|
||||
} from "@/data/schemas/auth/google_login_request";
|
||||
|
||||
export class GoogleLoginRequest {
|
||||
declare readonly idToken: string;
|
||||
declare readonly platform: string;
|
||||
declare readonly guestId: string;
|
||||
|
||||
private constructor(input: GoogleLoginRequestInput) {
|
||||
const data = GoogleLoginRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: GoogleLoginRequestInput): GoogleLoginRequest {
|
||||
return new GoogleLoginRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): GoogleLoginRequest {
|
||||
return GoogleLoginRequest.from(json as GoogleLoginRequestInput);
|
||||
}
|
||||
|
||||
toJson(): GoogleLoginRequestData {
|
||||
return GoogleLoginRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 游客登录请求 DTO
|
||||
*/
|
||||
import {
|
||||
GuestLoginRequestSchema,
|
||||
type GuestLoginRequestInput,
|
||||
type GuestLoginRequestData,
|
||||
} from "@/data/schemas/auth/guest_login_request";
|
||||
|
||||
export class GuestLoginRequest {
|
||||
declare readonly deviceId: string;
|
||||
|
||||
private constructor(input: GuestLoginRequestInput) {
|
||||
const data = GuestLoginRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: GuestLoginRequestInput): GuestLoginRequest {
|
||||
return new GuestLoginRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): GuestLoginRequest {
|
||||
return GuestLoginRequest.from(json as GuestLoginRequestInput);
|
||||
}
|
||||
|
||||
toJson(): GuestLoginRequestData {
|
||||
return GuestLoginRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 游客登录响应 DTO
|
||||
*/
|
||||
import {
|
||||
GuestLoginResponseSchema,
|
||||
type GuestLoginResponseInput,
|
||||
type GuestLoginResponseData,
|
||||
} from "@/data/schemas/auth/guest_login_response";
|
||||
import { User } from "@/data/dto/user/user";
|
||||
|
||||
export class GuestLoginResponse {
|
||||
declare readonly token: string;
|
||||
declare readonly deviceId: string;
|
||||
declare readonly userId: string;
|
||||
declare readonly isDeviceUser: boolean;
|
||||
declare readonly expiresIn: number;
|
||||
declare readonly user: User;
|
||||
|
||||
private constructor(input: GuestLoginResponseInput) {
|
||||
const parsed = GuestLoginResponseSchema.parse(input);
|
||||
Object.assign(this, {
|
||||
...parsed,
|
||||
user: parsed.user ? User.fromJson(parsed.user) : User.empty(),
|
||||
});
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: GuestLoginResponseInput): GuestLoginResponse {
|
||||
return new GuestLoginResponse(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): GuestLoginResponse {
|
||||
return GuestLoginResponse.from(json as GuestLoginResponseInput);
|
||||
}
|
||||
|
||||
toJson(): GuestLoginResponseData {
|
||||
const data = GuestLoginResponseSchema.parse(this);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* 登录请求 DTO
|
||||
*/
|
||||
import {
|
||||
LoginRequestSchema,
|
||||
type LoginRequestInput,
|
||||
type LoginRequestData,
|
||||
} from "@/data/schemas/auth/login_request";
|
||||
|
||||
export class LoginRequest {
|
||||
declare readonly email: string;
|
||||
declare readonly username: string;
|
||||
declare readonly password: string;
|
||||
declare readonly platform: string;
|
||||
declare readonly guestId: string;
|
||||
|
||||
private constructor(input: LoginRequestInput) {
|
||||
const data = LoginRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: LoginRequestInput): LoginRequest {
|
||||
return new LoginRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): LoginRequest {
|
||||
return LoginRequest.from(json as LoginRequestInput);
|
||||
}
|
||||
|
||||
toJson(): LoginRequestData {
|
||||
return LoginRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 登录响应 DTO
|
||||
*/
|
||||
import {
|
||||
LoginResponseSchema,
|
||||
type LoginResponseInput,
|
||||
type LoginResponseData,
|
||||
} from "@/data/schemas/auth/login_response";
|
||||
import { User } from "@/data/dto/user/user";
|
||||
|
||||
export class LoginResponse {
|
||||
declare readonly user: User;
|
||||
declare readonly token: string;
|
||||
declare readonly refreshToken: string;
|
||||
|
||||
private constructor(input: LoginResponseInput) {
|
||||
const data = LoginResponseSchema.parse(input);
|
||||
Object.assign(this, {
|
||||
...data,
|
||||
user: User.fromJson(data.user),
|
||||
});
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: LoginResponseInput): LoginResponse {
|
||||
return new LoginResponse(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): LoginResponse {
|
||||
return LoginResponse.from(json as LoginResponseInput);
|
||||
}
|
||||
|
||||
toJson(): LoginResponseData {
|
||||
return LoginResponseSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 退出登录响应 DTO
|
||||
*/
|
||||
import {
|
||||
LogoutResponseSchema,
|
||||
type LogoutResponseInput,
|
||||
type LogoutResponseData,
|
||||
} from "@/data/schemas/auth/logout_response";
|
||||
|
||||
export class LogoutResponse {
|
||||
declare readonly success: boolean;
|
||||
|
||||
private constructor(input: LogoutResponseInput) {
|
||||
const data = LogoutResponseSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: LogoutResponseInput): LogoutResponse {
|
||||
return new LogoutResponse(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): LogoutResponse {
|
||||
return LogoutResponse.from(json as LogoutResponseInput);
|
||||
}
|
||||
|
||||
toJson(): LogoutResponseData {
|
||||
return LogoutResponseSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 刷新 Token 请求 DTO
|
||||
*/
|
||||
import {
|
||||
RefreshTokenRequestSchema,
|
||||
type RefreshTokenRequestInput,
|
||||
type RefreshTokenRequestData,
|
||||
} from "@/data/schemas/auth/refresh_token_request";
|
||||
|
||||
export class RefreshTokenRequest {
|
||||
declare readonly refreshToken: string;
|
||||
|
||||
private constructor(input: RefreshTokenRequestInput) {
|
||||
const data = RefreshTokenRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: RefreshTokenRequestInput): RefreshTokenRequest {
|
||||
return new RefreshTokenRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): RefreshTokenRequest {
|
||||
return RefreshTokenRequest.from(json as RefreshTokenRequestInput);
|
||||
}
|
||||
|
||||
toJson(): RefreshTokenRequestData {
|
||||
return RefreshTokenRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 刷新 Token 响应 DTO
|
||||
*/
|
||||
import {
|
||||
RefreshTokenResponseSchema,
|
||||
type RefreshTokenResponseInput,
|
||||
type RefreshTokenResponseData,
|
||||
} from "@/data/schemas/auth/refresh_token_response";
|
||||
|
||||
export class RefreshTokenResponse {
|
||||
declare readonly token: string;
|
||||
declare readonly refreshToken: string;
|
||||
declare readonly userId: string;
|
||||
|
||||
private constructor(input: RefreshTokenResponseInput) {
|
||||
const data = RefreshTokenResponseSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: RefreshTokenResponseInput): RefreshTokenResponse {
|
||||
return new RefreshTokenResponse(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): RefreshTokenResponse {
|
||||
return RefreshTokenResponse.from(json as RefreshTokenResponseInput);
|
||||
}
|
||||
|
||||
toJson(): RefreshTokenResponseData {
|
||||
return RefreshTokenResponseSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* 注册请求 DTO
|
||||
*/
|
||||
import {
|
||||
RegisterRequestSchema,
|
||||
type RegisterRequestInput,
|
||||
type RegisterRequestData,
|
||||
} from "@/data/schemas/auth/register_request";
|
||||
|
||||
export class RegisterRequest {
|
||||
declare readonly username: string;
|
||||
declare readonly email: string;
|
||||
declare readonly password: string;
|
||||
declare readonly platform: string;
|
||||
declare readonly guestId: string;
|
||||
|
||||
private constructor(input: RegisterRequestInput) {
|
||||
const data = RegisterRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: RegisterRequestInput): RegisterRequest {
|
||||
return new RegisterRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): RegisterRequest {
|
||||
return RegisterRequest.from(json as RegisterRequestInput);
|
||||
}
|
||||
|
||||
toJson(): RegisterRequestData {
|
||||
return RegisterRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 发送验证码请求 DTO
|
||||
*/
|
||||
import {
|
||||
SendCodeRequestSchema,
|
||||
type SendCodeRequestInput,
|
||||
type SendCodeRequestData,
|
||||
} from "@/data/schemas/auth/send_code_request";
|
||||
|
||||
export class SendCodeRequest {
|
||||
declare readonly email: string;
|
||||
|
||||
private constructor(input: SendCodeRequestInput) {
|
||||
const data = SendCodeRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: SendCodeRequestInput): SendCodeRequest {
|
||||
return new SendCodeRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): SendCodeRequest {
|
||||
return SendCodeRequest.from(json as SendCodeRequestInput);
|
||||
}
|
||||
|
||||
toJson(): SendCodeRequestData {
|
||||
return SendCodeRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user