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.
 
 
 
 

36 lines
1.1 KiB

import { createPost } from "#server/service/posts";
import { visibilitySchema } from "#server/constants/visibility";
import { isUniqueConstraintViolation } from "#server/utils/db-unique-constraint";
export default defineWrappedResponseHandler(async (event) => {
const user = await event.context.auth.requireUser();
const body = await readBody<{
title: string;
slug: string;
bodyMarkdown: string;
excerpt: string;
coverUrl?: string | null;
tagsJson?: string;
publishedAt?: string | null;
visibility: string;
}>(event);
try {
const row = await createPost(user.id, {
title: body.title,
slug: body.slug,
bodyMarkdown: body.bodyMarkdown,
excerpt: body.excerpt,
coverUrl: body.coverUrl,
tagsJson: body.tagsJson,
publishedAt: body.publishedAt ? new Date(body.publishedAt) : new Date(),
visibility: visibilitySchema.parse(body.visibility),
});
return R.success({ post: row });
} catch (e) {
if (isUniqueConstraintViolation(e)) {
throw createError({ statusCode: 409, statusMessage: "slug 已存在" });
}
throw e;
}
});