-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
42 lines (39 loc) · 1.12 KB
/
index.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
var request = require('request');
exports.slugify = slugify;
exports.get = get;
function get(url, { query, auth = null } = {}) {
var options = {
url: url,
qs: query,
headers: {
'accept': 'application/vnd.github.v3+json',
'User-Agent': 'Node.js'
}
};
if (auth) {
options.headers['Authorization'] = auth;
}
return new Promise(function(resolve, reject) {
request(options, function(error, res, body) {
if (res.statusCode == 200) {
resolve(JSON.parse(body));
} else if (+res.headers['x-ratelimit-remaining'] == 0) {
var date = new Date(+res.headers['x-ratelimit-reset']*1000);
reject('Rate limit util ' + date);
} else {
reject('Error code ' + res.statusCode);
}
});
});
}
// ref: https://gist.github.com/codeguy/6684588
function slugify(text) {
return text
.toString()
.toLowerCase()
.normalize('NFD')
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-');
}