forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
28 lines (24 loc) · 1.14 KB
/
Copy pathgithub.ts
File metadata and controls
28 lines (24 loc) · 1.14 KB
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
import { wait } from "../helpers";
// explain how this works
/**
* Checks the rate limit for the GitHub API and waits if necessary
* @param headers The headers of the response
* @returns The remaining requests
* @example
* const remainingRequests = await checkRateLimitGit(headers);
* console.log(`Remaining requests: ${remainingRequests}`);
**/
export const checkRateLimitGit = async (headers: { "x-ratelimit-remaining"?: string; "x-ratelimit-reset"?: string }) => {
// Check the remaining limit
const remainingRequests = headers["x-ratelimit-remaining"] ? parseInt(headers["x-ratelimit-remaining"]) : 0;
// If there are no more remaining requests for this hour, we wait for the reset time
if (remainingRequests === 0) {
// const resetTime = new Date(parseInt(headers["x-ratelimit-reset"]! || "0") * 1000);
const resetTime = new Date((headers["x-ratelimit-reset"] ? parseInt(headers["x-ratelimit-reset"]) : 0) * 1000);
const now = new Date();
const timeToWait = resetTime.getTime() - now.getTime();
console.log(`No remaining requests. Waiting for ${timeToWait}ms...`);
await wait(timeToWait);
}
return remainingRequests;
};