-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.ts
46 lines (41 loc) · 1.11 KB
/
request.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
async function githubApiUrl() {
const defaultUrl = "https://api.github.com";
const envAccess = { name: "env", variable: "GITHUB_API_URL" } as const;
const permissions = await Deno.permissions.query(envAccess);
if (permissions.state == "granted") {
return Deno.env.get("GITHUB_API_URL") ?? defaultUrl;
}
return defaultUrl;
}
export async function appRequest(
jwt: string,
endpoint: string,
// deno-lint-ignore no-explicit-any
params?: Record<string, any>,
) {
const apiUrl = await githubApiUrl();
const response = await fetch(`${apiUrl}/${endpoint}`, {
headers: {
authorization: `bearer ${jwt}`,
accept: "application/vnd.github.v3+json",
},
...params,
});
return await response.json();
}
export function listInstallations(jwt: string) {
return appRequest(jwt, "app/installations");
}
export function createInstallationToken(
jwt: string,
installationId: string,
...repositories: string[]
) {
const body = {
repositories,
};
return appRequest(jwt, `app/installations/${installationId}/access_tokens`, {
method: "POST",
body: JSON.stringify(body),
});
}