Created
June 6, 2024 09:36
-
-
Save dinhmai74/092156318bd5a73181db8f9dcfd7b393 to your computer and use it in GitHub Desktop.
integrate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createHeadlessEditor } from '@lexical/headless' // <= make sure this package is installed | |
import { $generateNodesFromDOM } from '@lexical/html' | |
import { | |
defaultEditorConfig, | |
defaultEditorFeatures, | |
getEnabledNodes, | |
sanitizeServerEditorConfig, | |
} from '@payloadcms/richtext-lexical' | |
import { JSDOM } from 'jsdom' | |
import { $getRoot, $getSelection } from 'lexical' | |
import { headers } from 'next/headers' | |
import type { PayloadHandler } from 'payload/config' | |
import { Post } from 'src/payload-types' | |
import * as z from 'zod' | |
import { getEditorFeatures } from '../getEditorFeatures' | |
const inputSchema = z.object({ | |
title: z.string(), | |
content: z.string(), | |
authorId: z.coerce | |
.string() | |
.transform((v) => v.toString()) | |
.optional(), | |
excerpt: z.string().optional(), | |
feature_image: z.string().optional(), | |
slug: z.string(), | |
categories: z.string().array().optional(), | |
status: z.string().optional().default('draft'), | |
}) | |
export const createPost: PayloadHandler = async (req): Promise<Response> => { | |
const { payload } = req | |
try { | |
const headersList = headers() | |
const auth = await payload.auth({ | |
req, | |
headers: headersList, | |
}) | |
if (!auth.user) { | |
return Response.json({ error: 'Unauthorized' }, { status: 401 }) | |
} | |
const body = await req.json() | |
const input = await inputSchema.parse(body) | |
const yourEditorConfig = defaultEditorConfig | |
yourEditorConfig.features = [...defaultEditorFeatures, ...getEditorFeatures()] | |
const editorConfig = await sanitizeServerEditorConfig(yourEditorConfig, payload.config) | |
const headlessEditor = createHeadlessEditor({ | |
nodes: getEnabledNodes({ | |
editorConfig, | |
}), | |
}) | |
headlessEditor.update( | |
() => { | |
const dom = new JSDOM(input.content) | |
const nodes = $generateNodesFromDOM(headlessEditor, dom.window.document) | |
$getRoot().select() | |
const selection = $getSelection() | |
selection.insertNodes(nodes) | |
}, | |
{ discrete: true }, | |
) | |
const editorJSON = headlessEditor.getEditorState().toJSON() | |
await payload.logger.info('creating post') | |
const statusMap: Record<string, Post['_status']> = { | |
publish: 'published', | |
published: 'published', | |
draft: 'draft', | |
} | |
const result = await payload.create({ | |
collection: 'posts', | |
data: { | |
title: input.title, | |
slug: input.slug, | |
authors: [input.authorId], | |
categories: input.categories, | |
meta: { | |
title: input.title, | |
description: input.excerpt, | |
image: input.feature_image, | |
}, | |
_status: statusMap[input.status] ?? 'draft', | |
content: { | |
root: editorJSON.root, | |
}, | |
}, | |
}) | |
return Response.json({ success: true, data: result }) | |
} catch (error: unknown) { | |
const message = error instanceof Error ? error.message : 'Unknown error' | |
payload.logger.error(message) | |
return Response.json({ error: message }, { status: 500 }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment