UNPKG

2.43 kBJavaScriptView Raw
1const _ = require('lodash');
2
3module.exports = ({
4 Pdf,
5 ClientConfig,
6 router,
7 asyncMiddleware,
8 getConfig,
9 handleError,
10}) => {
11
12 router.get(
13 '/pdf/view.:ext?',
14 asyncMiddleware(async (req, res) => {
15 const pdf = Pdf(await getConfig(req.session.slug));
16
17 pdf.getPayload(req.query.template, req.query.id, req.session.role)
18 .then((payload) => {
19 pdf.getPdf(payload)
20 .then((pdf) => {
21 res.type('application/pdf');
22 res.status(200);
23 res.send(pdf);
24 }, handleError.bind(null, req, res));
25 }, handleError.bind(null, req, res));
26 })
27 );
28
29 router.get(
30 '/pdf/download.:ext?',
31 asyncMiddleware(async (req, res) => {
32 const pdf = Pdf(await getConfig(req.session.slug));
33
34 pdf.getPayload(req.query.template, req.query.id, req.session.role)
35 .then((payload) => {
36 pdf.getPdf(payload)
37 .then((pdf) => {
38 res.attachment(payload.fileName || 'download.pdf');
39 res.status(200);
40 res.send(pdf);
41 }, handleError.bind(null, req, res));
42 }, handleError.bind(null, req, res));
43 })
44 );
45
46 router.get(
47 '/pdf/payload.:ext?',
48 asyncMiddleware(async (req, res) => {
49 const pdf = Pdf(await getConfig(req.session.slug));
50
51 pdf.getPayload(req.query.template, req.query.id, req.session.role)
52 .then((payload) => {
53 res.status(200);
54 res.json(payload);
55 }, handleError.bind(null, req, res));
56 })
57 );
58
59 router.get(
60 '/pdf/submit.:ext?',
61 asyncMiddleware(async (req, res) => {
62 const config = await getConfig(req.session.slug);
63
64 const cc = ClientConfig(config);
65 const clientConfig = await cc.get();
66
67 const assetSlug = _.get(clientConfig, 'assets.slug', req.session.slug);
68
69 const pdf = Pdf(config);
70
71 pdf.getPayload(req.query.template, req.query.id, req.session.role)
72 .then((payload) => {
73 payload = JSON.stringify(payload).replace(/'/gi, '’');
74
75 res.status(200);
76 res.send(`
77 <body onload='form.submit()'>
78 <form id='form' method='POST' action='${config.assist.url}/${assetSlug}/pdf/download' target='_self'>
79 <input type='hidden' name='payload' value='${payload}' />
80 </form>
81 </body>
82 `);
83 }, handleError.bind(null, req, res));
84 })
85 );
86};