From f7019626a235879eb09b13f9cf9febf076890c02 Mon Sep 17 00:00:00 2001 From: MICHAEL JACKSON Date: Wed, 24 May 2017 22:23:53 -0700 Subject: [PATCH] Add script to show a single log entry --- scripts/show-log.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts/show-log.js diff --git a/scripts/show-log.js b/scripts/show-log.js new file mode 100644 index 0000000..366e951 --- /dev/null +++ b/scripts/show-log.js @@ -0,0 +1,46 @@ +require('isomorphic-fetch') +const invariant = require('invariant') + +const CloudflareEmail = process.env.CLOUDFLARE_EMAIL +const CloudflareKey = process.env.CLOUDFLARE_KEY +const RayID = process.argv[2] + +invariant( + CloudflareEmail, + 'Missing the $CLOUDFLARE_EMAIL environment variable' +) + +invariant( + CloudflareKey, + 'Missing the $CLOUDFLARE_KEY environment variable' +) + +invariant( + RayID, + 'Missing the RAY_ID argument; use `heroku run node show-log.js RAY_ID`' +) + +const getZones = (domain) => + fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, { + method: 'GET', + headers: { + 'X-Auth-Email': CloudflareEmail, + 'X-Auth-Key': CloudflareKey + } + }).then(res => res.json()) + .then(data => data.result) + +const getLog = (zoneId, rayId) => + fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/requests/${rayId}`, { + method: 'GET', + headers: { + 'X-Auth-Email': CloudflareEmail, + 'X-Auth-Key': CloudflareKey + } + }).then(res => res.status === 404 ? 'NOT FOUND' : res.json()) + +getZones('unpkg.com').then(zones => { + getLog(zones[0].id, RayID).then(entry => { + console.log(entry) + }) +})