summaryrefslogtreecommitdiff
path: root/source/github-helpers/api.ts
diff options
context:
space:
mode:
authorGravatar Federico <me@fregante.com> 2020-11-21 13:27:14 -0600
committerGravatar GitHub <noreply@github.com> 2020-11-21 13:27:14 -0600
commitc99595dc4755c99a6aa05a237c952bc1d9c08dfb (patch)
tree9244ead59ed89b09cb07e60f93d0271d2edfd724 /source/github-helpers/api.ts
parent1bedcd71886ac9c20bf91a6d48e428622ab6d1b1 (diff)
downloadrefined-github-c99595dc4755c99a6aa05a237c952bc1d9c08dfb.tar.gz
refined-github-c99595dc4755c99a6aa05a237c952bc1d9c08dfb.tar.zst
refined-github-c99595dc4755c99a6aa05a237c952bc1d9c08dfb.zip
Add `quick-fork-deletion` feature (#3734)
Diffstat (limited to 'source/github-helpers/api.ts')
-rw-r--r--source/github-helpers/api.ts13
1 files changed, 12 insertions, 1 deletions
diff --git a/source/github-helpers/api.ts b/source/github-helpers/api.ts
index 98b2ddf9..504e4755 100644
--- a/source/github-helpers/api.ts
+++ b/source/github-helpers/api.ts
@@ -51,6 +51,7 @@ interface RestResponse extends AnyObject {
export const escapeKey = (value: string | number): string => '_' + String(value).replace(/[ ./-]/g, '_');
export class RefinedGitHubAPIError extends Error {
+ response: AnyObject = {};
constructor(...messages: string[]) {
super(messages.join('\n'));
}
@@ -66,6 +67,14 @@ export async function expectToken(): Promise<string> {
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 the \`${scope}\` scope. It only includes \`${tokenScopes}\``);
+ }
+}
+
const api3 = pageDetect.isEnterprise() ?
`${location.origin}/api/v3/` :
'https://api.github.com/';
@@ -210,11 +219,13 @@ export async function getError(apiResponse: JsonObject): Promise<RefinedGitHubAP
);
}
- return new RefinedGitHubAPIError(
+ 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;
}