UNPKG

github-folder-tree

Version:

github-folder-tree is a React custom hook that allows you to fetch and process the contents of a GitHub folder. It retrieves information about the files and subfolders in the specified folder, including their names, file types, download URLs, SHA hashes,

74 lines 3.47 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import axios from 'axios'; export const fetchFolderData = (folderUrl, setError, setLog, apiKey) => __awaiter(void 0, void 0, void 0, function* () { const contentIndex = folderUrl.indexOf('contents/') + 'contents/'.length; const decodedUrl = decodeURIComponent(contentIndex > 0 ? folderUrl.substring(contentIndex) : folderUrl); setLog(`Fetching data from ${decodedUrl}`); const options = {}; if (apiKey) { options.headers = { Authorization: `Bearer ${apiKey}`, }; } const { data: response } = yield axios.get(folderUrl, options); setLog(`Data fetched from ${decodedUrl}`); return response; }); export const processFolderContents = (folder, setError, setLog, path, apiKey) => __awaiter(void 0, void 0, void 0, function* () { const filePromises = folder.map((item) => __awaiter(void 0, void 0, void 0, function* () { try { if (item.type === 'file') { setLog(`Processing ${item.name}`); const extension = item.name.split('.').pop() || 'unknown'; const sizeInKB = Math.round(parseInt(item.size) / 1024); let size; if (sizeInKB >= 1024) { const sizeInMB = (sizeInKB / 1024).toFixed(2); size = sizeInMB + ' MB'; } else { size = sizeInKB + ' KB'; } return { name: item.name, file_type: extension, download_url: item.download_url, sha: item.sha, size: size, path: item.path, }; } else if (item.type === 'dir') { setLog(`Processing ${item.name}`); const subFolder = yield fetchFolderData(item.url, setError, setLog, apiKey); if (subFolder !== null) { setLog(`Processing ${item.name}`); const subFolderFiles = yield processFolderContents(subFolder, setError, setLog, item.path, apiKey); setLog(`Processed ${item.name}`); return subFolderFiles; } else { setLog(`Skipping ${item.name} due to error in fetching subfolder data`); return null; } } } catch (error) { setError(`Error processing ${item.name}: ${error.message}`); return null; } })); const files = yield Promise.all(filePromises); const flattenedFiles = files.flat(); setLog('Processing complete'); return flattenedFiles.filter((file) => file !== null); }); //# sourceMappingURL=apiUtils.js.map