get [template].ts to work

This commit is contained in:
Thomas Ruoff
2021-02-14 22:07:42 +01:00
parent d7478b5e9c
commit 81db4b5d0e

View File

@@ -3,42 +3,42 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { brief as briefTemplate } from '../../../../lib/templates' import { brief as briefTemplate } from '../../../../lib/templates'
import renderer from '../../../../lib/renderer' import renderer from '../../../../lib/renderer'
import * as store from '../../../../lib/store' import * as store from '../../../../lib/store'
import { IDocProps } from '../../../../interfaces/IDocProps'
const TEMPLATES : { [key: string]: (options: object) => string; } = { const TEMPLATES: { [key: string]: (options: IDocProps) => string } = {
brief: briefTemplate brief: briefTemplate,
}; }
const handler = async (req: NextApiRequest, res: NextApiResponse) => { const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== 'POST') { if (req.method !== 'POST') {
res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' }) res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
return; return
} }
try { try {
const { const {
query: { template: templateArg }, query: { template: templateArg },
} = req } = req
const options = req.body; const options = req.body
const templateName = Array.isArray(templateArg) ? templateArg[0] : templateArg; const templateName = Array.isArray(templateArg) ? templateArg[0] : templateArg
const template = TEMPLATES[templateName]; const template = TEMPLATES[templateName]
if (!template) { if (!template) {
res.status(404).json({ statusCode: 404, message: 'Template not availabe' }) res.status(404).json({ statusCode: 404, message: 'Template not availabe' })
return; return
} }
const texDoc = template(options) const texDoc = template(options)
const id = await renderer(texDoc) const id = await renderer(texDoc)
const storeData = Object.assign({}, options, { const storeData = Object.assign({}, options, {
id, id,
created: new Date().toISOString() created: new Date().toISOString(),
}); })
await store.add(storeData) await store.add(storeData)
res.status(200).json({ id: id }); res.status(200).json({ id: id })
} catch (err) { } catch (err) {
console.error('Error:', err, 'for', req.url); console.error('Error:', err, 'for', req.url)
res.status(500).json({ error: err.toString() }); res.status(500).json({ error: err.toString() })
} }
} }