From 05aac4fabd18f330e9446c7b79796c51da650d0a Mon Sep 17 00:00:00 2001 From: Rakesh9100 Date: Fri, 13 Dec 2024 11:40:32 +0530 Subject: [PATCH] Handling the API limit exceed for fetching the contributors --- .gitignore | 22 +++++++++++ assets/js_files/contributor.js | 65 +++++++++++++++++-------------- netlify/functions/contributors.js | 41 +++++++++++++++++++ package.json | 15 +++++++ 4 files changed, 114 insertions(+), 29 deletions(-) create mode 100644 .gitignore create mode 100644 netlify/functions/contributors.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..85829d64 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Node.js dependencies +node_modules/ +package-lock.json + +# Logs and temp files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.env + +# Netlify functions build +.netlify/functions/ + +# VSCode settings (if applicable) +.vscode/ + +# MacOS system files +.DS_Store + +# Linux/Mac temporary files +*.swp +*.swo \ No newline at end of file diff --git a/assets/js_files/contributor.js b/assets/js_files/contributor.js index 354854a4..b5fc248e 100644 --- a/assets/js_files/contributor.js +++ b/assets/js_files/contributor.js @@ -1,39 +1,43 @@ const cont = document.getElementById('contributor'); -const owner = 'Rakesh9100'; -const repoName = 'Beautiify'; async function fetchContributors(pageNumber) { const perPage = 100; - const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${pageNumber}&per_page=${perPage}`; + const apiUrl = '/.netlify/functions/contributors'; // Netlify serverless function path - const response = await fetch(url); + const response = await fetch(`${apiUrl}?page=${pageNumber}&per_page=${perPage}`); + if (!response.ok) { - throw new Error(`Failed to fetch contributors data. Status code: ${response.status}`); + throw new Error(`Failed to fetch the contributors data. Status code: ${response.status}`); } const contributorsData = await response.json(); return contributorsData; } -// Function to fetch all contributors async function fetchAllContributors() { let allContributors = []; let pageNumber = 1; + const maxPages = 10; // Limiting the number of pages to avoid overload (can be adjusted) try { - while (true) { - const contributorsData = await fetchContributors(pageNumber); - if (contributorsData.length === 0) { - break; - } - allContributors = allContributors.concat(contributorsData); - pageNumber++; + // Fetch all contributors in parallel using Promise.all() + const fetchPromises = []; + + // Fetch data for multiple pages concurrently + for (let i = 1; i <= maxPages; i++) { + fetchPromises.push(fetchContributors(i)); } - for (let contributor of allContributors) { - if (contributor.login === owner) { - continue; - } + const contributorsArray = await Promise.all(fetchPromises); + + // Combine all the results + contributorsArray.forEach(contributorsData => { + allContributors = allContributors.concat(contributorsData); + }); + + // Display contributor cards + allContributors.forEach((contributor) => { + if (contributor.login === 'Rakesh9100') return; // Skip owner const contributorCard = document.createElement('div'); contributorCard.classList.add('contributor-card'); @@ -48,22 +52,25 @@ async function fetchAllContributors() { loginLink.appendChild(avatarImg); // Fetch detailed info for the name - const contributorDetails = await fetch(contributor.url); - const contributorData = await contributorDetails.json(); - const displayName = contributorData.name || contributor.login; + fetch(contributor.url) + .then(contributorDetails => contributorDetails.json()) + .then(contributorData => { + const displayName = contributorData.name || contributor.login; - const nameDiv = document.createElement('div'); - nameDiv.classList.add('contributor-name'); - nameDiv.textContent = displayName; + const nameDiv = document.createElement('div'); + nameDiv.classList.add('contributor-name'); + nameDiv.textContent = displayName; - contributorCard.appendChild(loginLink); - contributorCard.appendChild(nameDiv); + contributorCard.appendChild(loginLink); + contributorCard.appendChild(nameDiv); - cont.appendChild(contributorCard); - } + cont.appendChild(contributorCard); + }) + .catch(error => console.error('Error fetching the contributor details:', error)); + }); } catch (error) { - console.error(error); + console.error('Error fetching the contributors:', error); } } -fetchAllContributors(); +fetchAllContributors(); \ No newline at end of file diff --git a/netlify/functions/contributors.js b/netlify/functions/contributors.js new file mode 100644 index 00000000..ed084bfe --- /dev/null +++ b/netlify/functions/contributors.js @@ -0,0 +1,41 @@ +// Use dynamic import for node-fetch +const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); + +exports.handler = async function(event, context) { + const token = process.env.GITHUB_TOKEN; // Access the token from the environment variables + const page = event.queryStringParameters.page || 1; + const perPage = event.queryStringParameters.per_page || 100; + const owner = 'Rakesh9100'; + const repoName = 'Beautiify'; + + const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${page}&per_page=${perPage}`; + + try { + const response = await fetch(url, { + method: 'GET', + headers: { + 'Authorization': `token ${token}` // Use the token in the request header + } + }); + + if (!response.ok) { + return { + statusCode: response.status, + body: `Failed to fetch the contributors data. Status code: ${response.status}` + }; + } + + const contributorsData = await response.json(); + + return { + statusCode: 200, + body: JSON.stringify(contributorsData) + }; + } catch (error) { + console.error(error); + return { + statusCode: 500, + body: JSON.stringify({ message: 'Error fetching the contributors' }) + }; + } +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 00000000..2d05474f --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "beautiify", + "version": "1.0.0", + "description": "Fetch and display GitHub contributors.", + "main": "script.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": ["github", "contributors", "fetch"], + "author": "", + "license": "ISC", + "dependencies": { + "node-fetch": "^3.3.2" + } +}