aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/installation/fetch_latest_artifact_metadata.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/installation/fetch_latest_artifact_metadata.ts')
-rw-r--r--editors/code/src/installation/fetch_latest_artifact_metadata.ts47
1 files changed, 47 insertions, 0 deletions
diff --git a/editors/code/src/installation/fetch_latest_artifact_metadata.ts b/editors/code/src/installation/fetch_latest_artifact_metadata.ts
new file mode 100644
index 000000000..f07431aac
--- /dev/null
+++ b/editors/code/src/installation/fetch_latest_artifact_metadata.ts
@@ -0,0 +1,47 @@
1import fetch from "node-fetch";
2import { GithubRepo, ArtifactMetadata } from "./interfaces";
3
4const GITHUB_API_ENDPOINT_URL = "https://api.github.com";
5
6export interface FetchLatestArtifactMetadataOpts {
7 repo: GithubRepo;
8 artifactFileName: string;
9}
10
11export async function fetchLatestArtifactMetadata(
12 opts: FetchLatestArtifactMetadataOpts
13): Promise<null | ArtifactMetadata> {
14
15 const repoOwner = encodeURIComponent(opts.repo.owner);
16 const repoName = encodeURIComponent(opts.repo.name);
17
18 const apiEndpointPath = `/repos/${repoOwner}/${repoName}/releases/latest`;
19 const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath;
20
21 // We skip runtime type checks for simplicity (here we cast from `any` to `Release`)
22
23 const response: GithubRelease = await fetch(requestUrl, {
24 headers: { Accept: "application/vnd.github.v3+json" }
25 })
26 .then(res => res.json());
27
28 const artifact = response.assets.find(artifact => artifact.name === opts.artifactFileName);
29
30 return !artifact ? null : {
31 releaseName: response.name,
32 downloadUrl: artifact.browser_download_url
33 };
34
35 // Noise denotes tremendous amount of data that we are not using here
36 interface GithubRelease {
37 name: string;
38 assets: Array<{
39 browser_download_url: string;
40
41 [noise: string]: unknown;
42 }>;
43
44 [noise: string]: unknown;
45 }
46
47}