/
index.ts
105 lines (89 loc) 路 2.65 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { graphql } from '@octokit/graphql'
import { enterpriseCompatibility } from '@octokit/plugin-enterprise-compatibility'
import { retry } from '@octokit/plugin-retry'
import { throttling } from '@octokit/plugin-throttling'
import { Octokit } from '@octokit/rest'
import { addGraphQL } from './graphql'
import { addLogging, Logger } from './logging'
import { addPagination } from './pagination'
export const ProbotOctokit = Octokit
.plugin([throttling, retry, enterpriseCompatibility])
/**
* the [@octokit/rest Node.js module](https://github.com/octokit/rest.js),
* which wraps the [GitHub API](https://developer.github.com/v3/) and allows
* you to do almost anything programmatically that you can do through a web
* browser.
* @see {@link https://github.com/octokit/rest.js}
*/
export function GitHubAPI (options: Options = { Octokit: ProbotOctokit } as any) {
const octokit = new options.Octokit(Object.assign(options, {
throttle: Object.assign({
onAbuseLimit: (retryAfter: number) => {
options.logger.warn(`Abuse limit hit, retrying in ${retryAfter} seconds`)
return true
},
onRateLimit: (retryAfter: number) => {
options.logger.warn(`Rate limit hit, retrying in ${retryAfter} seconds`)
return true
}
}, options.throttle)
})) as GitHubAPI
addPagination(octokit)
addLogging(octokit, options.logger)
addGraphQL(octokit)
return octokit
}
export interface Options extends Octokit.Options {
debug?: boolean
logger: Logger
Octokit: Octokit.Static
}
export interface RequestOptions {
baseUrl?: string
method?: string
url?: string
headers?: any
query?: string
variables?: Variables
data?: any
}
export interface Result {
headers: {
status: string
}
}
export interface OctokitError extends Error {
status: number
}
interface Paginate extends Octokit.Paginate {
(
responsePromise: Promise<Octokit.AnyResponse>,
callback?: (response: Octokit.AnyResponse, done: () => void) => any
): Promise<any[]>
}
type Graphql = (query: string, variables?: Variables, headers?: Headers) => ReturnType<typeof graphql>
export interface GitHubAPI extends Octokit {
paginate: Paginate
graphql: Graphql
/**
* @deprecated `.query()` is deprecated, use `.graphql()` instead
*/
query: Graphql
}
export interface GraphQlQueryResponse {
data: { [ key: string ]: any } | null
errors?: [{
message: string
path: [string]
extensions: { [ key: string ]: any }
locations: [{
line: number,
column: number
}]
}]
}
export interface Headers {
[key: string]: string
}
export interface Variables { [key: string]: any }
export { GraphQLError } from './graphql'