refactor(data): replace schema classes with readonly models
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 发送消息请求
|
||||
*
|
||||
*
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -10,48 +10,22 @@ import {
|
||||
stringOrEmpty,
|
||||
} from "../../nullable-defaults";
|
||||
|
||||
export const SendMessageRequestSchema = z.object({
|
||||
characterId: z.string().min(1),
|
||||
message: stringOrEmpty,
|
||||
image: stringOrEmpty,
|
||||
imageId: stringOrEmpty,
|
||||
imageThumbUrl: stringOrEmpty,
|
||||
imageMediumUrl: stringOrEmpty,
|
||||
imageOriginalUrl: stringOrEmpty,
|
||||
imageWidth: numberOrZero,
|
||||
imageHeight: numberOrZero,
|
||||
useWebSocket: booleanOrFalse,
|
||||
});
|
||||
export const SendMessageRequestSchema = z
|
||||
.object({
|
||||
characterId: z.string().min(1),
|
||||
message: stringOrEmpty,
|
||||
image: stringOrEmpty,
|
||||
imageId: stringOrEmpty,
|
||||
imageThumbUrl: stringOrEmpty,
|
||||
imageMediumUrl: stringOrEmpty,
|
||||
imageOriginalUrl: stringOrEmpty,
|
||||
imageWidth: numberOrZero,
|
||||
imageHeight: numberOrZero,
|
||||
useWebSocket: booleanOrFalse,
|
||||
})
|
||||
.readonly();
|
||||
|
||||
export type SendMessageRequestInput = z.input<typeof SendMessageRequestSchema>;
|
||||
export type SendMessageRequestData = z.output<typeof SendMessageRequestSchema>;
|
||||
|
||||
export class SendMessageRequest {
|
||||
declare readonly characterId: string;
|
||||
declare readonly message: string;
|
||||
declare readonly image: string;
|
||||
declare readonly imageId: string;
|
||||
declare readonly imageThumbUrl: string;
|
||||
declare readonly imageMediumUrl: string;
|
||||
declare readonly imageOriginalUrl: string;
|
||||
declare readonly imageWidth: number;
|
||||
declare readonly imageHeight: number;
|
||||
|
||||
private constructor(input: SendMessageRequestInput) {
|
||||
const data = SendMessageRequestSchema.parse(input);
|
||||
Object.assign(this, data);
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
static from(input: SendMessageRequestInput): SendMessageRequest {
|
||||
return new SendMessageRequest(input);
|
||||
}
|
||||
|
||||
static fromJson(json: unknown): SendMessageRequest {
|
||||
return SendMessageRequest.from(json as SendMessageRequestInput);
|
||||
}
|
||||
|
||||
toJson(): SendMessageRequestData {
|
||||
return SendMessageRequestSchema.parse(this);
|
||||
}
|
||||
}
|
||||
export type SendMessageRequest = SendMessageRequestData;
|
||||
|
||||
Reference in New Issue
Block a user