Skip to content

Commit 6f0492c

Browse files
Merge pull request #87 from Exabyte-io/feature/SOF-7138
feature/SOF-7138: feat: move fetch from github api function from MD
2 parents 047b604 + ca16b3e commit 6f0492c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/utils/github.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Fetches file metadata from a GitHub repository using the GitHub API.
3+
*
4+
* The GitHub API returns an array of file objects in the repository. Each file object includes details such as
5+
* the file name, download URL, Git data URL, HTML URL, file path, SHA hash, size, type, and metadata URL.
6+
*
7+
* @param {string} url - The URL of the GitHub API endpoint to fetch the files from.
8+
* @returns {Promise<Array<{name: string, download_url: string, git_url: string, html_url: string, path: string, sha: string, size: number, type: string, url: string}>>}
9+
* - A promise that resolves to an array of objects. Each object contains the properties `name`, `download_url`,
10+
* `git_url`, `html_url`, `path`, `sha`, `size`, `type`, and `url`.
11+
* @throws {Error} - Rethrows any error encountered during the fetch operation or data processing,
12+
* allowing the caller of the function to handle it as needed.
13+
*
14+
* @example
15+
* // Example URL for GitHub API
16+
* const url = "https://api.github.com/repos/username/repo/contents/path/to/directory";
17+
* fetchFiles(url).then(files => {
18+
* files.forEach(file => {
19+
* console.log(`File Name: ${file.name}, Download URL: ${file.download_url}`);
20+
* // Other properties can also be accessed here
21+
* });
22+
* }).catch(error => {
23+
* console.error("Error fetching files:", error);
24+
* });
25+
*/
26+
export async function fetchFilesFromGitHubAPI(url) {
27+
try {
28+
const response = await fetch(url);
29+
const data = await response.json();
30+
return data.map((file) => ({
31+
name: file.name,
32+
download_url: file.download_url,
33+
}));
34+
} catch (error) {
35+
console.error("Error fetching files:", error);
36+
throw error;
37+
}
38+
}

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { convertToCompactCSVArrayOfObjects, safeMakeArray } from "./array";
33
import { cloneClass, extendClass, extendClassStaticProps, extendThis } from "./class";
44
import { deepClone } from "./clone";
55
import { refreshCodeMirror } from "./codemirror";
6+
import { fetchFilesFromGitHubAPI } from "./github";
67
import {
78
createObjectPathFromFilePath,
89
formatFileSize,
@@ -92,4 +93,5 @@ export {
9293
createObjectPathFromFilePath,
9394
buildNamedEntitySchema,
9495
findPreviousVersion,
96+
fetchFilesFromGitHubAPI,
9597
};

0 commit comments

Comments
 (0)