-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
getAppAccessToken.ts
61 lines (51 loc) · 1.64 KB
/
getAppAccessToken.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
import { getJWT } from '../../lib/jwt';
import { GITHUB_REPO_INSTALLATION_URL, GITHUB_ACCESS_TOKEN_URL } from '../config';
import { getCachedAccessToken, setCachedAccessToken } from '../supabase/cachedAccessToken';
interface GAccessToken {
token: string;
expires_at: string;
permissions: {
discussions: string;
metadata: string;
};
repository_selection: string;
repositories?: Array<unknown>;
}
function getHeaders() {
return {
Authorization: `Bearer ${getJWT()}`,
Accept: 'application/vnd.github.v3+json',
};
}
async function getInstallationId(repoWithOwner: string): Promise<number> {
const { id } = await fetch(GITHUB_REPO_INSTALLATION_URL(repoWithOwner), {
headers: getHeaders(),
}).then((response) => response.json());
return id;
}
export async function getAppAccessToken(repoWithOwner: string): Promise<string> {
const installationId = await getInstallationId(repoWithOwner);
if (!installationId)
throw {
message: 'giscus is not installed on this repository',
documentation_url:
'https://docs.github.com/en/rest/reference/apps#get-a-repository-installation-for-the-authenticated-app',
};
const cachedToken = await getCachedAccessToken(installationId);
if (cachedToken) return cachedToken;
const response = await fetch(GITHUB_ACCESS_TOKEN_URL(installationId), {
method: 'POST',
headers: getHeaders(),
});
if (!response.ok)
throw {
message: 'Failed fetching access token',
};
const { token, expires_at }: GAccessToken = await response.json();
await setCachedAccessToken({
installation_id: installationId,
token,
expires_at,
});
return token;
}