refactor(models): migrate 28 data models to Zod schema-driven pattern
This commit is contained in:
@@ -1,57 +1,39 @@
|
||||
/**
|
||||
* 积分历史记录模型 - 对应后端 CreditsHistoryData schema
|
||||
* 原始 Dart: CreditsHistoryData (lib/data/models/user/user.dart)
|
||||
*
|
||||
* 注:原始 Dart 中 `records` 为 `List<Map<String, dynamic>>`,
|
||||
* 迁移为 `Record<string, unknown>[]`。
|
||||
*/
|
||||
export class CreditsHistoryData {
|
||||
readonly records: Record<string, unknown>[];
|
||||
readonly total: number;
|
||||
readonly limit: number;
|
||||
readonly offset: number;
|
||||
import { z } from "zod";
|
||||
|
||||
constructor(params: {
|
||||
records: Record<string, unknown>[];
|
||||
total: number;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}) {
|
||||
this.records = params.records;
|
||||
this.total = params.total;
|
||||
this.limit = params.limit;
|
||||
this.offset = params.offset;
|
||||
export const CreditsHistoryDataSchema = z.object({
|
||||
records: z.array(z.record(z.string(), z.unknown())),
|
||||
total: z.number(),
|
||||
limit: z.number(),
|
||||
offset: z.number(),
|
||||
});
|
||||
|
||||
export type CreditsHistoryDataInput = z.input<typeof CreditsHistoryDataSchema>;
|
||||
export type CreditsHistoryDataData = z.output<typeof CreditsHistoryDataSchema>;
|
||||
|
||||
export class CreditsHistoryData {
|
||||
declare readonly records: Record<string, unknown>[];
|
||||
declare readonly total: number;
|
||||
declare readonly limit: number;
|
||||
declare readonly offset: number;
|
||||
|
||||
private constructor(data: CreditsHistoryDataData) {
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
toJson(): Record<string, unknown> {
|
||||
return {
|
||||
records: this.records,
|
||||
total: this.total,
|
||||
limit: this.limit,
|
||||
offset: this.offset,
|
||||
};
|
||||
static from(input: CreditsHistoryDataInput): CreditsHistoryData {
|
||||
return new CreditsHistoryData(CreditsHistoryDataSchema.parse(input));
|
||||
}
|
||||
|
||||
static fromJson(json: Record<string, unknown>): CreditsHistoryData {
|
||||
const requireNumber = (key: string): number => {
|
||||
const v = json[key];
|
||||
if (typeof v !== "number") {
|
||||
throw new Error(
|
||||
`CreditsHistoryData.${key} is required and must be a number`
|
||||
);
|
||||
}
|
||||
return v;
|
||||
};
|
||||
const rawRecords = json.records;
|
||||
const records: Record<string, unknown>[] = Array.isArray(rawRecords)
|
||||
? (rawRecords as Record<string, unknown>[])
|
||||
: [];
|
||||
return new CreditsHistoryData({
|
||||
records,
|
||||
total: requireNumber("total"),
|
||||
limit: requireNumber("limit"),
|
||||
offset: requireNumber("offset"),
|
||||
});
|
||||
static fromJson(json: unknown): CreditsHistoryData {
|
||||
return CreditsHistoryData.from(json as CreditsHistoryDataInput);
|
||||
}
|
||||
|
||||
toJson(): CreditsHistoryDataData {
|
||||
return CreditsHistoryDataSchema.parse(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user