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,43 +3,43 @@ 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 {
const {
query: { template: templateArg },
} = req
const options = 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 availabe' })
return
} }
try { const texDoc = template(options)
const { const id = await renderer(texDoc)
query: { template: templateArg }, const storeData = Object.assign({}, options, {
} = req id,
const options = req.body; created: new Date().toISOString(),
const templateName = Array.isArray(templateArg) ? templateArg[0] : templateArg; })
const template = TEMPLATES[templateName]; await store.add(storeData)
res.status(200).json({ id: id })
if (!template) { } catch (err) {
res.status(404).json({ statusCode: 404, message: 'Template not availabe' }) console.error('Error:', err, 'for', req.url)
return; res.status(500).json({ error: err.toString() })
} }
const texDoc = template(options)
const id = await renderer(texDoc)
const storeData = Object.assign({}, options, {
id,
created: new Date().toISOString()
});
await store.add(storeData)
res.status(200).json({ id: id });
} catch (err) {
console.error('Error:', err, 'for', req.url);
res.status(500).json({ error: err.toString() });
}
} }
export default handler export default handler