generated from MrClemRkz/pdf-generate-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
66 lines (57 loc) · 2.48 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { check } = require('express-validator');
module.exports.register = function(app) {
app.get('/', (req, res) => res.send(`Welcome! Please see check <a href="https://github.com/MrClemRkz/pdf-generate-service">here</a> if you're looking for the api docs`))
app.post('/generate', [
check('url').isURL({require_valid_protocol: true, allow_protocol_relative_urls: false, require_tld: true}).optional(),
check('html').optional(),
check('file').optional(),
check('url_html_file').custom((value, {req}) => {
if (!req.body.html && !req.body.url && !req.body.file) {
throw new Error("Must provide either url, html or file data");
}
return true;
}),
check('scale').custom((value, {req}) => {
if (!value) {
return true;
}
if (Number.isNaN(value) || value < 0.1 || value > 2) {
throw new Error("scale must be between 0.1 and 2");
}
return true;
}),
check('displayHeaderFooter').isBoolean().optional(),
check('headerTemplate').optional(),
check('footerTemplate').optional(),
check('printBackground').isBoolean().optional(),
check('landscape').isBoolean().optional(),
check('pageRanges').custom((value, {req}) => {
if (!value) {
return true; // optional
}
const pageRanges = value.split('-');
if (Number.isNaN(pageRanges[0]) || Number.isNaN(pageRanges[1])) {
throw new Error("Invalid page range. Must be like 2-5");
}
return true;
}),
check('format').custom((value, {req}) => {
if (!value) {
return true;
}
const options = ['Letter', 'Legal', 'Tabloid', 'Ledger', 'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6'];
if (options.indexOf(value) === -1) {
throw new Error(`format must be one of ${options.join(',')}`);
}
return true;
}),
check('width').isNumeric().optional(),
check('height').isNumeric().optional(),
check('margin.top').isNumeric().optional(),
check('margin.right').isNumeric().optional(),
check('margin.bottom').isNumeric().optional(),
check('margin.left').isNumeric().optional(),
check('preferCSSPageSize').isBoolean().optional()
], require('./handlers/generate'));
return app;
}