This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 1.95 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* A simple NodeJS example using async generators.
*/
'use strict';
const debug = require('debug')('search');
const parse = require('parse-link-header');
const request = require('request-promise-native');
// GitHub code search result generator.
async function *search(q) {
const get = request.defaults({
method: 'GET',
json: true, // parse the result as JSON
resolveWithFullResponse: true,
headers: {
// see https://developer.github.com/v3/#user-agent-required
'User-Agent': 'js async generator demo',
},
});
// setup the first URL to be requested. The followings requests will use
// the rel="next" URL found in the response Link HTTP header, see
// https://developer.github.com/v3/guides/traversing-with-pagination/
let url = new URL('https://api.github.com/search/code');
url.searchParams.set('q', q);
// Main loop going through the paginated results.
while (url) {
debug(`requesting ${url}`);
const rsp = await get({ url });
if (rsp.statusCode !== 200 /* OK */) {
throw new Error(`expected 200 OK but got ${rsp.statusCode}`);
}
debug(`yielding ${rsp.body.items.length} items`);
yield* rsp.body.items;
const link = parse(rsp.headers.link);
if (link && link.next) {
// Setup the next page URL for the next loop iteration.
url = new URL(link.next.url);
} else {
// We've reached the last page, exit the loop.
url = null;
}
}
}
// await may only be used from inside an async function.
async function main() {
// You may change the search query, but beware of GitHub search rate limit
// https://developer.github.com/v3/search/#rate-limit
for await (const { score, name, repository } of search('OpenBSD+user:kAworu')) {
console.log(`→ ${score.toFixed(2)}: ${name} (in ${repository.full_name})`);
}
}
main();