|
| 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 | +} |
0 commit comments