-
Notifications
You must be signed in to change notification settings - Fork 8
/
example.js
71 lines (59 loc) · 1.31 KB
/
example.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const papi = require('./lib');
/**
* GitHub API client
*/
class GitHub extends papi.Client {
constructor(opts) {
opts = opts || {};
if (!opts.baseUrl) {
opts.baseUrl = 'https://api.github.com';
}
if (!opts.headers) {
opts.headers = {};
}
if (!opts.headers.accept) {
opts.headers.accept = 'application/vnd.github.v3+json';
}
if (!opts.headers['user-agent']) {
opts.headers['user-agent'] = 'PapiGitHub/0.1.0';
}
if (opts.tags) {
opts.tags = ['github'].concat(opts.tags);
} else {
opts.tags = ['github'];
}
if (!opts.timeout) {
opts.timeout = 60 * 1000;
}
super(opts);
if (opts.debug) {
this.on('log', console.log);
}
}
/**
* Get user gists
*/
async gists(username) {
const opts = {
path: '/users/{username}/gists',
params: { username: username },
};
const res = await this._get(opts);
return res.body;
}
}
// Print gists for user `silas`
async function main() {
const github = new GitHub({ debug: true });
const gists = await github.gists('silas');
console.log('----');
gists.forEach(function(gist) {
if (gist.description) console.log(gist.description);
});
}
if (require.main === module) {
main();
} else {
module.exports = GitHub;
}