-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfromGist.js
30 lines (27 loc) · 865 Bytes
/
fromGist.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 got = require("got")
const toMachine = require("./xstate/toMachine")
const basicAuth = process.env.GITHUB_USER && process.env.GITHUB_TOKEN && "Basic "
+ Buffer.from(
process.env.GITHUB_USER
+ ":" + process.env.GITHUB_TOKEN
).toString("base64")
// ac90dc3ae16d407369c6341f29c2b10c
async function fromGist(gistId) {
try {
const { body } = await got(gistId, {
baseUrl: "https://api.github.com/gists/",
json: true,
headers: basicAuth && {
"Authorization": basicAuth
}
})
const machineJs = body.files && body.files["machine.js"]
if (machineJs && machineJs.content) {
const machine = toMachine(machineJs.content)
return machine
}
} catch (err) {
throw new Error(`Gist ${gistId} could not be downloaded (${err.statusCode} ${err.statusMessage})`)
}
}
module.exports = fromGist