2019-07-09 22:43:45 +00:00
|
|
|
import fetch from 'isomorphic-fetch';
|
2017-08-12 16:14:46 +00:00
|
|
|
|
2018-12-17 17:38:05 +00:00
|
|
|
const cloudflareURL = 'https://api.cloudflare.com/client/v4';
|
2018-04-04 05:32:32 +00:00
|
|
|
const cloudflareEmail = process.env.CLOUDFLARE_EMAIL;
|
|
|
|
const cloudflareKey = process.env.CLOUDFLARE_KEY;
|
2017-08-12 16:14:46 +00:00
|
|
|
|
2019-07-10 20:24:27 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2019-07-10 00:21:25 +00:00
|
|
|
if (!cloudflareEmail) {
|
|
|
|
throw new Error('Missing the $CLOUDFLARE_EMAIL environment variable');
|
|
|
|
}
|
2017-08-12 16:14:46 +00:00
|
|
|
|
2019-07-10 00:21:25 +00:00
|
|
|
if (!cloudflareKey) {
|
|
|
|
throw new Error('Missing the $CLOUDFLARE_KEY environment variable');
|
|
|
|
}
|
|
|
|
}
|
2017-08-12 16:14:46 +00:00
|
|
|
|
2019-07-09 23:48:00 +00:00
|
|
|
function get(path, headers) {
|
2018-06-02 05:15:41 +00:00
|
|
|
return fetch(`${cloudflareURL}${path}`, {
|
2017-08-12 16:14:46 +00:00
|
|
|
headers: Object.assign({}, headers, {
|
2018-12-17 17:38:05 +00:00
|
|
|
'X-Auth-Email': cloudflareEmail,
|
|
|
|
'X-Auth-Key': cloudflareKey
|
2017-08-12 16:14:46 +00:00
|
|
|
})
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-08-12 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 23:48:00 +00:00
|
|
|
function getJSON(path, headers) {
|
2017-11-08 16:57:15 +00:00
|
|
|
return get(path, headers)
|
2017-11-08 18:14:46 +00:00
|
|
|
.then(res => {
|
2018-02-18 02:00:56 +00:00
|
|
|
return res.json();
|
2017-11-08 16:57:15 +00:00
|
|
|
})
|
2017-11-08 18:14:46 +00:00
|
|
|
.then(data => {
|
2017-11-08 16:57:15 +00:00
|
|
|
if (!data.success) {
|
2019-01-06 00:50:05 +00:00
|
|
|
console.error(`cloudflare.getJSON failed at ${path}`);
|
2018-02-18 02:00:56 +00:00
|
|
|
console.error(data);
|
2018-12-17 17:38:05 +00:00
|
|
|
throw new Error('Failed to getJSON from Cloudflare');
|
2017-11-08 16:57:15 +00:00
|
|
|
}
|
2017-08-22 15:31:33 +00:00
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
return data.result;
|
|
|
|
});
|
2017-08-12 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export function getZones(domains) {
|
2017-08-22 15:31:33 +00:00
|
|
|
return Promise.all(
|
2018-05-21 20:35:14 +00:00
|
|
|
(Array.isArray(domains) ? domains : [domains]).map(domain =>
|
|
|
|
getJSON(`/zones?name=${domain}`)
|
|
|
|
)
|
|
|
|
).then(results => results.reduce((memo, zones) => memo.concat(zones)));
|
2017-08-17 18:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
function reduceResults(target, values) {
|
|
|
|
Object.keys(values).forEach(key => {
|
2018-02-18 02:00:56 +00:00
|
|
|
const value = values[key];
|
2017-08-22 15:31:33 +00:00
|
|
|
|
2018-12-17 17:38:05 +00:00
|
|
|
if (typeof value === 'object' && value) {
|
2018-02-18 02:00:56 +00:00
|
|
|
target[key] = reduceResults(target[key] || {}, value);
|
2018-12-17 17:38:05 +00:00
|
|
|
} else if (typeof value === 'number') {
|
2018-02-18 02:00:56 +00:00
|
|
|
target[key] = (target[key] || 0) + values[key];
|
2017-08-22 15:31:33 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-08-22 15:31:33 +00:00
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
return target;
|
2017-08-22 15:31:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export function getZoneAnalyticsDashboard(zones, since, until) {
|
2017-08-22 15:31:33 +00:00
|
|
|
return Promise.all(
|
2017-11-08 18:14:46 +00:00
|
|
|
(Array.isArray(zones) ? zones : [zones]).map(zone => {
|
2017-11-08 16:57:15 +00:00
|
|
|
return getJSON(
|
|
|
|
`/zones/${
|
|
|
|
zone.id
|
|
|
|
}/analytics/dashboard?since=${since.toISOString()}&until=${until.toISOString()}`
|
2018-02-18 02:00:56 +00:00
|
|
|
);
|
2017-08-22 15:31:33 +00:00
|
|
|
})
|
2018-05-21 20:35:14 +00:00
|
|
|
).then(results => results.reduce(reduceResults));
|
2017-08-17 18:24:40 +00:00
|
|
|
}
|