CoastalCommitsPastes/client/pages/api/file/get-html.ts

29 lines
805 B
TypeScript
Raw Normal View History

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"
export default withMethods(
["POST"],
async (req: NextApiRequest, res: NextApiResponse) => {
const body = req.body
const content = parseQueryParam(body.content)
const title = parseQueryParam(body.title) || "Untitled"
2022-11-09 21:38:05 -05:00
if (!content) {
return res.status(400).json({ error: "Missing content" })
}
2022-11-09 21:38:05 -05:00
const renderedHTML = await getHtmlFromFile({
title,
content
})
2022-11-09 21:38: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
}
)