-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapi.js
49 lines (38 loc) · 1.02 KB
/
api.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const delay = require('delay')
const r2 = require('r2')
const token = process.env.ACCESS_TOKEN
const headers = {
Authorization: `bearer ${token}`
}
async function fetch(query, variables) {
let data
do {
try {
const response = await r2.post('https://api.github.com/graphql', {
headers,
json: {query, variables}
}).response
const json = await response.json()
if (json.data) {
data = json.data
} else {
if (json.message) {
process.stderr.write(json.message + '\n')
} else {
process.stderr.write(JSON.stringify(json) + '\n')
}
if (response.headers.has('Retry-After')) {
const retryAfter = parseInt(response.headers.get('Retry-After'))
await delay(retryAfter * 1000)
} else {
await delay(60 * 1000)
}
}
} catch (err) {
process.stderr.write(JSON.stringify(json) + '\n')
await delay(5 * 1000)
}
} while (!data)
return data
}
module.exports = {fetch}