mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 14:37:21 +01:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from 'next'
|
|
|
|
import { brief as briefTemplate } from '../../../lib/templates'
|
|
import renderer from '../../../lib/renderer'
|
|
import { IDocProps } from '../../../interfaces/IDocProps'
|
|
import { getId } from '../../../lib/id'
|
|
|
|
const TEMPLATES: { [key: string]: (options: IDocProps) => string } = {
|
|
brief: briefTemplate,
|
|
}
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
if (req.method !== 'POST') {
|
|
res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
|
|
return
|
|
}
|
|
|
|
try {
|
|
const {
|
|
query: { template: templateArg },
|
|
} = req
|
|
const templateData = req.body
|
|
const templateName = Array.isArray(templateArg) ? templateArg[0] : templateArg
|
|
const template = TEMPLATES[templateName]
|
|
|
|
if (!template) {
|
|
res.status(404).json({ statusCode: 404, message: 'Template not specified' })
|
|
return
|
|
}
|
|
|
|
const id = getId(templateData)
|
|
const texDoc = template(templateData)
|
|
await renderer(id, texDoc)
|
|
const storeData = { ...templateData, id, created: new Date().toISOString(), template: templateName }
|
|
res.status(200).json({ id: id, data: storeData })
|
|
} catch (err: any) {
|
|
console.error('Error:', err, 'for', req.url)
|
|
res.status(500).json({ error: err.toString() })
|
|
}
|
|
}
|
|
|
|
export default handler
|