-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
37 lines (36 loc) · 967 Bytes
/
utils.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
const { request } = require('node:https');
const { GITHUB_TOKEN } = process.env;
exports.fetch = (
endpoint,
options = { body: "" }
) => {
const body =
typeof options.body === 'string' ? options.body : JSON.stringify({ data: options.body });
delete options.body;
options.headers = {
...(options.headears || {}),
Authorization: `Bearer ${GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
"User-Agent": "PostmanRuntime/7.29.2"
};
return new Promise((resolve, reject) => {
let chunks = '';
const req = request(endpoint, options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => (chunks += chunk));
res.on('error', reject);
res.on('end', () => {
try {
if (res.statusCode >= 400) throw new Error(res.statusMessage);
const result = JSON.parse(chunks);
resolve(result);
} catch (err) {
reject(err);
}
});
});
req.on('error', reject);
if (body) req.write(body);
req.end();
});
}