Prettify everything
This commit is contained in:
@ -1,15 +1,18 @@
|
||||
require("isomorphic-fetch")
|
||||
const invariant = require("invariant")
|
||||
const gunzip = require("gunzip-maybe")
|
||||
const ndjson = require("ndjson")
|
||||
require("isomorphic-fetch");
|
||||
const invariant = require("invariant");
|
||||
const gunzip = require("gunzip-maybe");
|
||||
const ndjson = require("ndjson");
|
||||
|
||||
const CloudflareAPIURL = "https://api.cloudflare.com"
|
||||
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
|
||||
const CloudflareKey = process.env.CLOUDFLARE_KEY
|
||||
const CloudflareAPIURL = "https://api.cloudflare.com";
|
||||
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL;
|
||||
const CloudflareKey = process.env.CLOUDFLARE_KEY;
|
||||
|
||||
invariant(CloudflareEmail, "Missing the $CLOUDFLARE_EMAIL environment variable")
|
||||
invariant(
|
||||
CloudflareEmail,
|
||||
"Missing the $CLOUDFLARE_EMAIL environment variable"
|
||||
);
|
||||
|
||||
invariant(CloudflareKey, "Missing the $CLOUDFLARE_KEY environment variable")
|
||||
invariant(CloudflareKey, "Missing the $CLOUDFLARE_KEY environment variable");
|
||||
|
||||
function get(path, headers) {
|
||||
return fetch(`${CloudflareAPIURL}/client/v4${path}`, {
|
||||
@ -17,49 +20,49 @@ function get(path, headers) {
|
||||
"X-Auth-Email": CloudflareEmail,
|
||||
"X-Auth-Key": CloudflareKey
|
||||
})
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function getJSON(path, headers) {
|
||||
return get(path, headers)
|
||||
.then(res => {
|
||||
return res.json()
|
||||
return res.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
console.error(`CloudflareAPI.getJSON failed at ${path}`)
|
||||
console.error(data)
|
||||
throw new Error("Failed to getJSON from Cloudflare")
|
||||
console.error(`CloudflareAPI.getJSON failed at ${path}`);
|
||||
console.error(data);
|
||||
throw new Error("Failed to getJSON from Cloudflare");
|
||||
}
|
||||
|
||||
return data.result
|
||||
})
|
||||
return data.result;
|
||||
});
|
||||
}
|
||||
|
||||
function getZones(domains) {
|
||||
return Promise.all(
|
||||
(Array.isArray(domains) ? domains : [domains]).map(domain => {
|
||||
return getJSON(`/zones?name=${domain}`)
|
||||
return getJSON(`/zones?name=${domain}`);
|
||||
})
|
||||
).then(results => {
|
||||
return results.reduce((memo, zones) => {
|
||||
return memo.concat(zones)
|
||||
})
|
||||
})
|
||||
return memo.concat(zones);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function reduceResults(target, values) {
|
||||
Object.keys(values).forEach(key => {
|
||||
const value = values[key]
|
||||
const value = values[key];
|
||||
|
||||
if (typeof value === "object" && value) {
|
||||
target[key] = reduceResults(target[key] || {}, value)
|
||||
target[key] = reduceResults(target[key] || {}, value);
|
||||
} else if (typeof value === "number") {
|
||||
target[key] = (target[key] || 0) + values[key]
|
||||
target[key] = (target[key] || 0) + values[key];
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return target
|
||||
return target;
|
||||
}
|
||||
|
||||
function getZoneAnalyticsDashboard(zones, since, until) {
|
||||
@ -69,29 +72,31 @@ function getZoneAnalyticsDashboard(zones, since, until) {
|
||||
`/zones/${
|
||||
zone.id
|
||||
}/analytics/dashboard?since=${since.toISOString()}&until=${until.toISOString()}`
|
||||
)
|
||||
);
|
||||
})
|
||||
).then(results => {
|
||||
return results.reduce(reduceResults)
|
||||
})
|
||||
return results.reduce(reduceResults);
|
||||
});
|
||||
}
|
||||
|
||||
function getJSONStream(path, headers) {
|
||||
const acceptGzipHeaders = Object.assign({}, headers, {
|
||||
"Accept-Encoding": "gzip"
|
||||
})
|
||||
});
|
||||
|
||||
return get(path, acceptGzipHeaders)
|
||||
.then(res => {
|
||||
return res.body.pipe(gunzip())
|
||||
return res.body.pipe(gunzip());
|
||||
})
|
||||
.then(stream => {
|
||||
return stream.pipe(ndjson.parse())
|
||||
})
|
||||
return stream.pipe(ndjson.parse());
|
||||
});
|
||||
}
|
||||
|
||||
function getLogs(zoneId, startTime, endTime) {
|
||||
return getJSONStream(`/zones/${zoneId}/logs/requests?start=${startTime}&end=${endTime}`)
|
||||
return getJSONStream(
|
||||
`/zones/${zoneId}/logs/requests?start=${startTime}&end=${endTime}`
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@ -101,4 +106,4 @@ module.exports = {
|
||||
getZoneAnalyticsDashboard,
|
||||
getJSONStream,
|
||||
getLogs
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user