-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.js
35 lines (31 loc) · 1.06 KB
/
http.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
const log = require('@vladmandic/pilogger');
const globalHeaders = {
'content-type': 'application/json',
'user-agent': 'nodejs/fetch',
};
const http = async (url, headers = {}) => {
try {
const res = await fetch(url, { method: 'GET', headers: { ...globalHeaders, ...headers } })
if (res.status !== 200) {
const limit = res.headers.get('x-ratelimit-remaining');
if (limit && parseInt(limit) === 0) log.error({ ratelimit: url, date: new Date(1000 * parseInt(res.headers.get('x-ratelimit-reset'))) });
else log.error({ code: res.status, test: res.statusText, url })
return {};
}
const text = await res.text();
const json = JSON.parse(text);
return json;
} catch (e) {
log.error({ exception: e, url })
return {};
}
};
const head = async (url, headers = {}) => {
let json = {};
const res = await fetch(url, { method: 'GET', headers: { ...globalHeaders, ...headers } })
if (res.status !== 200) return {};
for (const h of res.headers) json[h[0]] = h[1];
return json;
};
exports.http = http;
exports.head = head;