unpkg/scripts/show-log.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-11-26 05:19:55 +00:00
require("isomorphic-fetch")
const invariant = require("invariant")
2017-05-25 05:23:53 +00:00
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
const CloudflareKey = process.env.CLOUDFLARE_KEY
const RayID = process.argv[2]
2017-11-26 05:19:55 +00:00
invariant(CloudflareEmail, "Missing the $CLOUDFLARE_EMAIL environment variable")
2017-05-25 05:23:53 +00:00
2017-11-26 05:19:55 +00:00
invariant(CloudflareKey, "Missing the $CLOUDFLARE_KEY environment variable")
2017-05-25 05:23:53 +00:00
2017-11-26 05:19:55 +00:00
invariant(RayID, "Missing the RAY_ID argument; use `heroku run node show-log.js RAY_ID`")
2017-05-25 05:23:53 +00:00
2017-11-08 19:07:48 +00:00
function getZones(domain) {
return fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, {
2017-11-26 05:19:55 +00:00
method: "GET",
2017-05-25 05:23:53 +00:00
headers: {
2017-11-26 05:19:55 +00:00
"X-Auth-Email": CloudflareEmail,
"X-Auth-Key": CloudflareKey
2017-05-25 05:23:53 +00:00
}
2017-11-08 19:07:48 +00:00
})
.then(res => res.json())
2017-05-25 05:23:53 +00:00
.then(data => data.result)
2017-11-08 19:07:48 +00:00
}
2017-05-25 05:23:53 +00:00
2017-11-08 19:07:48 +00:00
function getLog(zoneId, rayId) {
2017-11-26 05:19:55 +00:00
return fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/requests/${rayId}`, {
method: "GET",
headers: {
"X-Auth-Email": CloudflareEmail,
"X-Auth-Key": CloudflareKey
2017-05-25 05:23:53 +00:00
}
2017-11-26 05:19:55 +00:00
}).then(res => (res.status === 404 ? "NOT FOUND" : res.json()))
2017-11-08 19:07:48 +00:00
}
2017-05-25 05:23:53 +00:00
2017-11-26 05:19:55 +00:00
getZones("unpkg.com").then(zones => {
2017-05-25 05:23:53 +00:00
getLog(zones[0].id, RayID).then(entry => {
console.log(entry)
})
})