-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathoctokit.ts
65 lines (57 loc) · 1.8 KB
/
octokit.ts
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
import { Octokit as OctokitCore } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { paginateGraphQL } from "@octokit/plugin-paginate-graphql";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { VERSION } from "./version.js";
import type { EndpointDefaults } from "@octokit/types";
export { RequestError } from "@octokit/request-error";
export type {
PageInfoForward,
PageInfoBackward,
} from "@octokit/plugin-paginate-graphql";
export const Octokit = OctokitCore.plugin(
restEndpointMethods,
paginateRest,
paginateGraphQL,
retry,
throttling,
).defaults({
userAgent: `octokit.js/${VERSION}`,
throttle: {
onRateLimit,
onSecondaryRateLimit,
},
});
export type Octokit = InstanceType<typeof Octokit>;
// istanbul ignore next no need to test internals of the throttle plugin
function onRateLimit(
retryAfter: number,
options: Required<EndpointDefaults>,
octokit: InstanceType<typeof OctokitCore>,
) {
octokit.log.warn(
`Request quota exhausted for request ${options.method} ${options.url}`,
);
if (options.request.retryCount === 0) {
// only retries once
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
}
// istanbul ignore next no need to test internals of the throttle plugin
function onSecondaryRateLimit(
retryAfter: number,
options: Required<EndpointDefaults>,
octokit: InstanceType<typeof OctokitCore>,
) {
octokit.log.warn(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
);
if (options.request.retryCount === 0) {
// only retries once
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
}