-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
30 lines (27 loc) · 809 Bytes
/
index.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
const { renderGoogleChart } = require('./lib/render');
async function renderGoogleChartHttp(req, res) {
let data = req.query;
if (req.method === 'POST') {
data = req.body;
}
try {
const packages = data.packages ? data.packages.split(',') : ['corechart'];
const opts = {
packages,
width: data.width,
height: data.height,
mapsApiKey: data.mapsApiKey,
};
const buf = await renderGoogleChart(data.code, opts);
res.type('image/png').send(buf);
} catch (err) {
if (err.toString().indexOf('TimeoutError') > -1) {
return res.status(500).send('Your chart was not rendered because it loaded too slowly.');
}
res.status(500).send(err.toString());
}
}
module.exports = {
renderHttp: renderGoogleChartHttp,
render: renderGoogleChart,
};