forked from w3c/webref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctokit.js
37 lines (32 loc) · 1.12 KB
/
octokit.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
/**
* Wrapper around Octokit to add throttling and avoid hitting rate limits
*/
const { throttling } = require("@octokit/plugin-throttling");
const Octokit = require("@octokit/rest").Octokit.plugin(throttling);
const MAX_RETRIES = 3;
module.exports = function (params) {
params = params || {};
const octoParams = Object.assign({
throttle: {
onRateLimit: (retryAfter, options) => {
if (options.request.retryCount < MAX_RETRIES) {
console.warn(`Rate limit exceeded, retrying after ${retryAfter} seconds`)
return true;
} else {
console.error(`Rate limit exceeded, giving up after ${MAX_RETRIES} retries`);
return false;
}
},
onSecondaryRateLimit: (retryAfter, options) => {
if (options.request.retryCount < MAX_RETRIES) {
console.warn(`Abuse detection triggered, retrying after ${retryAfter} seconds`)
return true;
} else {
console.error(`Abuse detection triggered, giving up after ${MAX_RETRIES} retries`);
return false;
}
}
}
}, params);
return new Octokit(octoParams);
}