start over with nextjs

This commit is contained in:
Thomas Ruoff
2021-02-25 00:31:28 +01:00
parent 3a77e7f22b
commit 214d95d3d7
68 changed files with 3047 additions and 18576 deletions

View File

@@ -0,0 +1,45 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { brief as briefTemplate } from '../../../../lib/templates'
import renderer from '../../../../lib/renderer'
import * as store from '../../../../lib/store'
const TEMPLATES : { [key: string]: (options: object) => 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 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;
}
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