|
| 1 | +import React from 'react'; |
| 2 | +import { NextApiRequest, NextApiResponse } from 'next'; |
| 3 | +import ReactDOMServer from 'react-dom/server'; |
| 4 | +import { Provider } from 'react-redux'; |
| 5 | +import { apiErrorHandler } from 'utils/api-error-handler'; |
| 6 | +import { ContentProps } from 'types/content-props/_content-common'; |
| 7 | +import { PageBase } from 'components/PageBase'; |
| 8 | +import { mockStore } from 'store/store'; |
| 9 | + |
| 10 | +type Body = { |
| 11 | + contentProps?: ContentProps; |
| 12 | +}; |
| 13 | + |
| 14 | +// TODO: add decorator, next.js scripts, css, etc |
| 15 | + |
| 16 | +const postHandler = async (req: NextApiRequest, res: NextApiResponse) => |
| 17 | + apiErrorHandler(req, res, async () => { |
| 18 | + if (req.headers.secret !== process.env.SERVICE_SECRET) { |
| 19 | + return res.status(401).send({ message: 'Unauthorized' }); |
| 20 | + } |
| 21 | + |
| 22 | + if (req.method !== 'POST') { |
| 23 | + return res.status(405).send({ message: 'Method not allowed' }); |
| 24 | + } |
| 25 | + |
| 26 | + if (!req.body?.contentProps) { |
| 27 | + return res.status(400).send({ message: 'Invalid request' }); |
| 28 | + } |
| 29 | + |
| 30 | + const html = ReactDOMServer.renderToStaticMarkup( |
| 31 | + <Provider store={mockStore}> |
| 32 | + <PageBase content={(req.body as Body).contentProps as ContentProps} /> |
| 33 | + </Provider> |
| 34 | + ); |
| 35 | + |
| 36 | + return res.setHeader('Content-Type', 'text/html').send(html); |
| 37 | + }); |
| 38 | + |
| 39 | +export default postHandler; |
0 commit comments