You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.8 KiB
51 lines
1.8 KiB
type CreateCommentRequestBody = {
|
|
parentId?: number | null;
|
|
guestDisplayName?: string;
|
|
guestEmail?: string;
|
|
guestIsAnonymous?: boolean;
|
|
body: unknown;
|
|
};
|
|
|
|
export type ParsedCreateCommentInput = {
|
|
parentId: number | null;
|
|
guestDisplayName?: string;
|
|
guestEmail?: string;
|
|
guestIsAnonymous?: boolean;
|
|
body: string;
|
|
};
|
|
|
|
export function parsePublicPostCreateCommentBody(body: CreateCommentRequestBody): ParsedCreateCommentInput {
|
|
if (typeof body.body !== "string") {
|
|
throw createError({ statusCode: 400, statusMessage: "无效请求" });
|
|
}
|
|
return {
|
|
parentId: parseParentId(body.parentId),
|
|
guestDisplayName: typeof body.guestDisplayName === "string" ? body.guestDisplayName : undefined,
|
|
guestEmail: typeof body.guestEmail === "string" ? body.guestEmail : undefined,
|
|
guestIsAnonymous: typeof body.guestIsAnonymous === "boolean" ? body.guestIsAnonymous : undefined,
|
|
body: body.body,
|
|
};
|
|
}
|
|
|
|
export function parseUnlistedCreateCommentBody(body: CreateCommentRequestBody): ParsedCreateCommentInput {
|
|
if (typeof body.body !== "string") {
|
|
throw createError({ statusCode: 400, statusMessage: "无效请求" });
|
|
}
|
|
return {
|
|
parentId: parseParentId(body.parentId),
|
|
guestDisplayName: typeof body.guestDisplayName === "string" ? body.guestDisplayName : undefined,
|
|
guestEmail: typeof body.guestEmail === "string" ? body.guestEmail : undefined,
|
|
guestIsAnonymous: typeof body.guestIsAnonymous === "boolean" ? body.guestIsAnonymous : undefined,
|
|
body: body.body,
|
|
};
|
|
}
|
|
|
|
function parseParentId(rawParentId: unknown): number | null {
|
|
if (rawParentId === undefined || rawParentId === null) {
|
|
return null;
|
|
}
|
|
if (typeof rawParentId === "number" && Number.isFinite(rawParentId)) {
|
|
return rawParentId;
|
|
}
|
|
throw createError({ statusCode: 400, statusMessage: "无效请求" });
|
|
}
|
|
|