2022-11-12 02:59:33 -05:00
|
|
|
import { withMethods } from "@lib/api-middleware/with-methods"
|
2022-11-10 02:11:36 -05:00
|
|
|
import { getHtmlFromFile } from "@lib/server/get-html-from-drift-file"
|
|
|
|
import { parseQueryParam } from "@lib/server/parse-query-param"
|
2022-11-09 21:38:05 -05:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
|
2022-11-12 04:57:30 -05:00
|
|
|
export default withMethods(
|
2022-11-12 20:11:05 -05:00
|
|
|
["POST"],
|
2022-11-12 04:57:30 -05:00
|
|
|
async (req: NextApiRequest, res: NextApiResponse) => {
|
2022-11-12 20:11:05 -05:00
|
|
|
const body = req.body
|
|
|
|
const content = parseQueryParam(body.content)
|
|
|
|
const title = parseQueryParam(body.title) || "Untitled"
|
2022-11-09 21:38:05 -05:00
|
|
|
|
2022-11-12 20:11:05 -05:00
|
|
|
if (!content || !title) {
|
|
|
|
return res.status(400).json({ error: "Missing arguments" })
|
2022-11-12 04:57:30 -05:00
|
|
|
}
|
2022-11-09 21:38:05 -05:00
|
|
|
|
2022-11-12 20:11:05 -05:00
|
|
|
const renderedHTML = await getHtmlFromFile({
|
|
|
|
title,
|
|
|
|
content
|
|
|
|
})
|
2022-11-09 21:38:05 -05:00
|
|
|
|
2022-11-12 20:11:05 -05:00
|
|
|
res.setHeader("Content-Type", "text/plain")
|
|
|
|
res.setHeader("Cache-Control", "public, max-age=4800")
|
|
|
|
res.status(200).write(renderedHTML)
|
|
|
|
res.end()
|
|
|
|
return
|
2022-11-09 21:38:05 -05:00
|
|
|
}
|
2022-11-12 04:57:30 -05:00
|
|
|
)
|