blog/frontend/src/lib/auth/adapter/gateway/userResponseDto.ts
2025-10-15 11:56:25 +08:00

38 lines
841 B
TypeScript

import { User } from '$lib/auth/domain/entity/user';
import z from 'zod';
export const userResponseSchema = z.object({
id: z.int32(),
displayed_name: z.string(),
email: z.email(),
});
export class UserResponseDto {
readonly id: number;
readonly displayedName: string;
readonly email: string;
private constructor(props: { id: number; displayedName: string; email: string }) {
this.id = props.id;
this.displayedName = props.displayedName;
this.email = props.email;
}
static fromJson(json: unknown): UserResponseDto {
const parsedJson = userResponseSchema.parse(json);
return new UserResponseDto({
id: parsedJson.id,
displayedName: parsedJson.displayed_name,
email: parsedJson.email,
});
}
toEntity(): User {
return new User({
id: this.id,
name: this.displayedName,
email: this.email,
});
}
}