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
27 lines (23 loc) · 959 Bytes
/
Copy pathgithub.ts
File metadata and controls
27 lines (23 loc) · 959 Bytes
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
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: any) => {
// Check the remaining limit
const remainingRequests = 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 now = new Date();
const timeToWait = resetTime.getTime() - now.getTime();
console.log(`No remaining requests. Waiting for ${timeToWait}ms...`);
await wait(timeToWait);
}
return remainingRequests;
};