Uecko_ERP/apps/server/archive/contexts/auth/domain/value-objects/token.ts

37 lines
789 B
TypeScript
Raw Normal View History

2025-05-02 21:43:51 +00:00
import { ValueObject } from "@/core/common/domain";
2025-05-09 10:45:32 +00:00
import { Result } from "@repo/rdx-utils";
2025-02-15 21:30:12 +00:00
import { z } from "zod";
2025-02-20 18:55:24 +00:00
interface TokenProps {
value: string;
}
export class Token extends ValueObject<TokenProps> {
2025-02-15 21:30:12 +00:00
static create(token: string): Result<Token, Error> {
const result = Token.validate(token);
if (!result.success) {
return Result.fail(new Error(result.error.errors[0].message));
}
2025-02-20 18:55:24 +00:00
return Result.ok(new Token({ value: result.data }));
2025-02-15 21:30:12 +00:00
}
private static validate(token: string) {
2025-02-16 19:30:20 +00:00
const schema = z.string().min(1, { message: "Invalid token string" });
2025-02-15 21:30:12 +00:00
return schema.safeParse(token);
}
2025-02-20 18:55:24 +00:00
getValue() {
return this.props.value;
}
toString() {
return this.props.value;
}
2025-05-04 20:06:57 +00:00
toPrimitive() {
return this.props.value;
}
2025-02-15 21:30:12 +00:00
}