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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/*
These will throw `RefinedGitHubAPIError` if something goes wrong or if it's a 404.
Probably don't catch them so they will appear in the console
next to the name of the feature that caused them.
Usage:
import * as api from '../github-helpers/api.js';
const user = await api.v3(`/users/${username}`);
const repositoryCommits = await api.v3('commits'); // Without a leading `/`, this is equivalent to `/repo/$current-repository/commits`
const data = await api.v4('{user(login: "user") {name}}');
Returns:
a Promise that resolves into an object.
If the response body is empty, you'll receive an object like {status: 200}
The second argument is an options object,
it lets you define accept error HTTP codes as a valid response, like:
{
ignoreHTTPStatus: true
}
so the call will not throw an error but it will return as usual.
*/
import mem from 'mem';
import * as pageDetect from 'github-url-detection';
import {JsonObject, AsyncReturnType} from 'type-fest';
import features from '../feature-manager.js';
import {getRepo} from './index.js';
import optionsStorage from '../options-storage.js';
type JsonError = {
message: string;
};
type GraphQLResponse = {
message?: string;
data?: JsonObject;
errors?: JsonError[];
};
type RestResponse = {
httpStatus: number;
headers: Headers;
ok: boolean;
} & AnyObject;
export const escapeKey = (...keys: Array<string | number>): string => '_' + String(keys).replace(/[^a-z\d]/gi, '_');
export class RefinedGitHubAPIError extends Error {
response: AnyObject = {};
constructor(...messages: string[]) {
super(messages.join('\n'));
}
}
const settings = optionsStorage.getAll();
export async function expectToken(): Promise<string> {
const {personalToken} = await settings;
if (!personalToken) {
throw new Error('Personal token required for this feature');
}
return personalToken;
}
export async function expectTokenScope(scope: string): Promise<void> {
const {headers} = await v3('/');
const tokenScopes = headers.get('X-OAuth-Scopes')!;
if (!tokenScopes.split(', ').includes(scope)) {
throw new Error('The token you provided does not have ' + (tokenScopes ? `the \`${scope}\` scope. It only includes \`${tokenScopes}\`.` : 'any scope.'));
}
}
const api3 = pageDetect.isEnterprise()
? `${location.origin}/api/v3/`
: 'https://api.github.com/';
const api4 = pageDetect.isEnterprise()
? `${location.origin}/api/graphql`
: 'https://api.github.com/graphql';
type GHRestApiOptions = {
ignoreHTTPStatus?: boolean;
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
body?: JsonObject;
headers?: HeadersInit;
json?: boolean;
};
type GHGraphQLApiOptions = {
allowErrors?: boolean;
};
const v3defaults: GHRestApiOptions = {
ignoreHTTPStatus: false,
method: 'GET',
body: undefined,
json: true,
};
const v4defaults: GHGraphQLApiOptions = {
allowErrors: false,
};
export const v3 = mem(async (
query: string,
options: GHRestApiOptions = v3defaults,
): Promise<RestResponse> => {
const {ignoreHTTPStatus, method, body, headers, json} = {...v3defaults, ...options};
const {personalToken} = await settings;
if (!query.startsWith('https')) {
query = query.startsWith('/') ? query.slice(1) : ['repos', getRepo()!.nameWithOwner, query].filter(Boolean).join('/');
}
const url = new URL(query, api3);
features.log.http(url);
const response = await fetch(url.href, {
method,
body: body && JSON.stringify(body),
headers: {
'User-Agent': 'Refined GitHub',
Accept: 'application/vnd.github.v3+json',
...headers,
...personalToken && {Authorization: `token ${personalToken}`},
},
});
const textContent = await response.text();
const apiResponse = json ? JSON.parse(textContent) : {textContent};
if (response.ok || ignoreHTTPStatus) {
return Object.assign(apiResponse, {
httpStatus: response.status,
headers: response.headers,
ok: response.ok,
});
}
throw await getError(apiResponse);
}, {
cacheKey: JSON.stringify,
});
export const v3paginated = async function * (
query: string,
options?: GHRestApiOptions,
): AsyncGenerator<AsyncReturnType<typeof v3>> {
while (true) {
// eslint-disable-next-line no-await-in-loop
const response = await v3(query, options);
yield response;
const match = /<([^>]+)>; rel="next"/.exec(response.headers.get('link')!);
if (match) {
query = match[1]!;
} else {
return;
}
}
};
export const v4 = mem(async (
query: string,
options: GHGraphQLApiOptions = v4defaults,
): Promise<AnyObject> => {
const personalToken = await expectToken();
if (/^(query )?{/.test(query.trimStart())) {
throw new TypeError('`query` should only be whatβs inside \'query {...}\', like \'user(login: "foo") { name }\', but is \n' + query);
}
query = query.replace('repository() {', () => `repository(owner: "${getRepo()!.owner}", name: "${getRepo()!.name}") {`);
features.log.http(`{
${query}
}`);
const response = await fetch(api4, {
headers: {
'User-Agent': 'Refined GitHub',
Authorization: `bearer ${personalToken}`,
Accept: 'application/vnd.github.merge-info-preview+json',
},
method: 'POST',
body: JSON.stringify({query: query.trimStart().startsWith('mutation') ? query : `{${query}}`}),
});
const apiResponse: GraphQLResponse = await response.json();
const {
data = {},
errors = [],
} = apiResponse;
if (errors.length > 0 && !options.allowErrors) {
throw new RefinedGitHubAPIError('GraphQL:', ...errors.map(error => error.message));
}
if (response.ok) {
return data;
}
throw await getError(apiResponse as JsonObject);
}, {
cacheKey([query, options]) {
// `repository()` uses global state and must be handled explicitly
// https://github.com/refined-github/refined-github/issues/5821
// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864
const key = [query, options];
if (query.includes('repository() {')) {
key.push(getRepo()?.nameWithOwner);
}
return JSON.stringify(key);
},
});
export async function getError(apiResponse: JsonObject): Promise<RefinedGitHubAPIError> {
const {personalToken} = await settings;
if ((apiResponse.message as string)?.includes('API rate limit exceeded')) {
return new RefinedGitHubAPIError(
'Rate limit exceeded.',
personalToken
? 'It may be time for a walk! π π'
: 'Set your token in the options or take a walk! π π',
);
}
if (apiResponse.message === 'Bad credentials') {
return new RefinedGitHubAPIError(
'The token seems to be incorrect or expired. Update it in the options.',
);
}
const error = new RefinedGitHubAPIError(
'Unable to fetch.',
personalToken
? 'Ensure that your token has access to this repo.'
: 'Maybe adding a token in the options will fix this issue.',
JSON.stringify(apiResponse, null, '\t'), // Beautify
);
error.response = apiResponse;
return error;
}
|